5

For simplicity, I have a project of which classes/files can be divided into two groups B and C.
C usually depends on (#include) some B.
Let's say C1,C2,... and C5 depend on B1, nothing special here:-

B1.h

class B1{/** some code */}; 

B1.cpp

#include "B1.h"
/** some code  */

C1.h

class C1{/** some code  */};

C1.cpp

#include "C1.h"
#include "B1.h"
/** some code  */

C2.h

class C2{/** some code  */};

C2.cpp

#include "C2.h"
#include "B1.h"
/** some code  */

Problem

When I modify B1.h and press F5, both C1.cpp and C2.cpp should be recompiled.
But in my case, it is not - VS recompiled only C1.cpp.

I have to rebuild the whole project to indirectly force C2.cpp to be recompiled, otherwise this error will occur:-

a copy of B1.h was found in d:...\B1.h, but the current source code is different from the version built into d:...\B1.h.

Note: Two locations d:\...\B1.h are exactly the same.

After months of coding, this issue just happened to me yesterday.
The issue happens only for this certain file (B1.h), after an exhaustive bug hunting.
Other Bi are OK.

What I have tried

(similar to answers for this question)

  • Delete everything in bin folder (both debug & release) - the #X folder (see below)
  • Clean project & rebuild (It is just a one-time cure.)
  • Refactor (automatic rename) B1 to something else (also the file name)
  • Remove the file, create a new file B1.h, then copy source code from old B1.h.
  • Restart my computer several times
  • Remove everything in Visual Studio's Temp folder (%Temp%)
  • Remove all .suo,.user,.ncb,.sbr,.log,.sdf,.pdb related to my project
  • Make sure C2.cpp's date modified time and system time correct.
  • (Edit) Make sure stdafx.h doesn't include any files that is a part of my project, e.g. B,C, and others that include B or C (in)directly.
  • (Edit) Make sure stdafx.h is not #include-ed by any files, except from project setting.
    Thank Ari0nhh, user975989 and TripeHound.
  • (Edit) Make sure, when full rebuild, C2.cpp really includes B1.h (look at show include's console).
  • (Edit) Repairing VS2015 does not help.
  • (Edit) Uninstalling most VS2015 extensions does not help.

Clue 1

If I modify C2.cpp (e.g. adding a space character), then save and build, C2.cpp will be linked to B1.h.

Now, if I edit B1.h and press F5, C2.cpp will now be recompiled automatically (correct).
It is not a cure because I also have a lot of other than C2 that want to link with B1.h.
... and it is very suspicious
... after I changed some project setting (not sure which), C2.cpp is not linked anymore.

Clue 2

Remove / recreate a new file B1.h
This solution cures the problem but the effect will vanish if I restart computer.

Question

  • How to fix the project to make the automatic-recompilation process correct again?
    More specifically, I am searching for some kind of "ultimate clean / repair this project" button.
  • (secondary question) What is the cause of the issue?

It is hard to pinpoint the cause, so partial answers or unsure guess are welcomed.

Some progress

After a day, it finally recompiled C2.cpp correctly - even after a computer restart.

I am still not sure which of my action that fixed it.

However, here is my guess, just in case it may be useful for someone.

My project file structure is as followed:-

F/
F/Debug/     <--#X contains .exe,.exp,.ilk,.lib,.pdb; one file per type
F/Release/
F/F/         <-- contains all my .cpp,.h; 
F/F/Debug/   <--#Y contains many .obj of my own code, .pch, .log, .tlog
F/F/Release/

I had deleted everything in #X, but not #Y.
Deleting all .obj in #Y might be the action that solves the issue.

I will have to test it more to see how much this approach is reliable ...
Note that all of the above questions are still unanswered.

10
  • Are you using the precompiled headers and is B1.h included into the stdafx.h? Commented Jan 19, 2017 at 5:22
  • @Ari0nhh Yes, I use stdafx. I don't include, but I will check again and again. Nice guess. Commented Jan 19, 2017 at 5:24
  • 2
    If you are using precompiled headers then the precompiled header must be the first #include in every source file, and must be included in every source file. At least for MSVC. Commented Jan 19, 2017 at 5:55
  • 1
    @javaLover No links, just anecdotal experience of mostly VS2008 ... have had occasional "why the **** isn't this working" problems which seem to stem from (accidentally) having other includes and/or pre-processor commands above the #include "stdafx.h" line, which don't seem to be having any effect. When I've moved them below (and/or turned of pre-compiled headers completely), everything starts working as expected... [cont] Commented Jan 19, 2017 at 13:50
  • 1
    [cont]... My guess is that encountering the #include "stdafx.h" line (roughly speaking) replaces the "current state" of the compiler with the pre-compiled state, so any "state" from directives before that line is lost. But, as I said, this is only anecdotal. Commented Jan 19, 2017 at 13:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.