Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header not refreshed - Visual C++ 2012

Using Visual C++ 2012, without precompiled headers:

When I change a header file that is included in multiple files, the change is not taken into account when building. If I rebuild all, the change is taken into account.

Reproduction case:

include.h

#ifndef INCLUDE_H_
#define INCLUDE_H_

class A {
public:
   A(int i) : i_(i) { }
   int i_;
};

class B {
public:
  B(int i = 1) : a_(i) { }
  A a_;
};

#endif INCLUDE_H_

dummy.cpp

#include "include.h"

main.cpp

#include <iostream>
#include "include.h"

int main(int, char**) {
  B b;
  std::cout << b.a_.i_ << std::endl;
  return 0;
}

This outputs 1.

Now I change int i = 1 to int i = 2 in include.h; I build and run, it outputs 1! I rebuild and run, it outputs 2.

  • The dummy.cpp file is necessary to reproduce the error. In real-life, this file is using include.h but not classes A and B (but this doesn't seem to change anything; declaring a class C with members A and B in dummy.cpp will still reproduce the problem). When removing the file dummy.cpp, or renaming it to zdummy.cpp (presumably it will be compiled after main.cpp), then the problem disappears.

  • I tried with include guards, with pragma once, with both of them, with none of them, the problem is reproduced in each case.

  • I cannot reproduce this problem with Code::Blocks/GCC; I didn't try with older versions of Visual Studio.

Am I missing something or is this really a bug in Visual Studio? If the latter, is there a known workaround? (Other than re-building at every step)

like image 479
bonob Avatar asked Apr 16 '13 19:04

bonob


1 Answers

The header file must be part of the project. If it isn't the project will still build i.e. the compiler can find it, but Visual Studio won't track the date of the file.

like image 90
Mark Ransom Avatar answered Oct 15 '22 00:10

Mark Ransom