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.

Community
  • 1
  • 1
javaLover
  • 6,347
  • 2
  • 22
  • 67
  • Are you using the precompiled headers and is `B1.h` included into the `stdafx.h`? – Ari0nhh Jan 19 '17 at 05:22
  • @Ari0nhh Yes, I use stdafx. I don't include, but I will check again and again. Nice guess. – javaLover Jan 19 '17 at 05: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. – user975989 Jan 19 '17 at 05:55
  • @Ari0nhh After checking the printed "show includes", there is no such thing. My `stdafx.h` only includes external library. – javaLover Jan 19 '17 at 05:55
  • @user975989 I have made it do so (automatically in project setting). "I don't include" I mean I didn't include `B1.h` in `stdafx.h`. Sorry for not being clear. – javaLover Jan 19 '17 at 05:56
  • @javaLover I think what user975989 is asking you to check is that you don't have `#include "B1.h"` on one line and `#include ` on a following line. Certainly in earlier versions of MSVC the inclusion of a pre-compiled stdafx seems to "reset" the (pre)compilation process/state, so earlier headers etc. aren't seen. I don't _think_ you can work around such issues with project settings. – TripeHound Jan 19 '17 at 13:27
  • @TripeHound Thank a lot for clarification! I am sure that there is not because I just use find all (`ctrl+shift+F`), it reports no string `stdafx` in any of my file except in `stdafx.cpp`. However, if you have a link or some more information about that "reset" please tell me more! It is new knowledge for me. – javaLover Jan 19 '17 at 13:33
  • 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] – TripeHound Jan 19 '17 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. – TripeHound Jan 19 '17 at 13:53
  • I have the same problem. Edit a header file. Some source file which includes that header does not recompile. Result is a link error where I expect a compiler error to stop the build. – Alan Baljeu Jun 21 '17 at 21:28

0 Answers0