Header not refreshed - Visual C++ 2012 - c++

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)

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.

I've been through such thing in the past.
Do you use precompiled headers? If you do please remove any project specific headers from the PCH. That is a lame mistake. Only place external, nonchanging headersin PCH, like C/C++ standard headers, Windows headers, Boost etc.

Related

Using static library in current project

I'm facing the error when I'm linking an external library and code with the current project. You will understand the problem by this example.
//foo.h
namespace LMS
{
class foo
{
foo(); //implementation in cpp
foo(int dummy) //implemented here
{
//something
}
};
} // namespace LMS
//foo.cpp
#include"foo.h"
namespace LMS
{
foo::foo()
{
//something
}
} // namespace LMS
//externalProgram.h
#include "foo.h"
LMS::foo *ptr;
ptr = new LMS::foo(); //Linking error: LNK 2019
ptr = new LMS::foo(2); //No error
Now the problem is that the external program doesn't know about the foo.cpp and the implementation of class foo methods in it. I'm currently using VS2019 and using two projects in the same solution. I've tried several ways to correct this but it didn't work out. The current way I'm seeing is to implement all the functions in header files.
EDIT: I've already linked the files!!
You should be able to mark your externalProgram project as dependent on the foo project. This will link foo.o in with external program.
Using the UI you will select this in
Project > Project Dependencies : Depends on ...
If that is correct, then the problem is more subtle, sometimes just a simple typo. At this point you want to use the command line tools to break apart the library and confirm the object files, and break apart the object files and confirm the symbols within. A ten year old SO posting discusses using lib.exe to example the library file. The dumpbin tool has a /symbols option that might also be handy for looking at the actual code generated.

VSCode IntelliSense proposes a .h file but marks it not found

I can't compile a simple stupid demo "project" using visual Studio Code.
As I'm trying to enrich my knowledge portfolio, I trying to learn the basics manually, as to speak I try to compile code manually, without that the Visual Studio Enterprise IDE does all automatically. So I decided to use Visual Studio Code on Mac + clang++ for a simple demo project.
My problem is: I have a Workspace. In this workspace, I have some directories. One Of those directories called Functions and Headers contains again directories: debug, release, include, src. In the src directory, I have the functions.cpp file containing two dummy methods:
int add(int x, int y)
{
return x + y;
}
int subtract(int x, int y)
{
return x - y;
}
and in the main.cpp:
#include "functions.h"
int main()
{
int x = add(1, 3);
int y = subtract(5,1);
return 0;
}
In my include folder I have two method declarations:
int add(int x, int y);
int subtract(int x, int y);
The interesting point is: When I'm in main.cpp and I type #include " then IntelliSense finds the functions.h and proposes that file to me.
Also, the method calls are highlighted correctly. But: after including the file, Visual Studio Code marks the file read and says: functions.h file not found.
Is that a visual Studio Code bug? Because I also had a look on the settings and the settings seems ok (see picture).
Clang writes:
Ginos-MBP:src ginovalentinopensuni$ clang++ functions.cpp main.cpp main.cpp:2:10: fatal error: 'functions.h' file not found
#include "functions.h"
^~~~~~~~~~~~~ 1 error generated.
Do I miss something or am I doing something wrong? I also looked at the other similar questions here, but there was nothing helpful.
Pictures:
Indeed. The VSCode include path that allowed #include "functions.h" to be autocompleted has no bearing on what is a valid path to the compiler. There are two ways to address this:
Fix the path in the source file
Since you are including the header functions.h from the ./src directory, try instead writing:
#include "../include/functions.h"
int main()
{
int x = add(1, 3);
int y = subtract(5,1);
return 0;
}
This should work. Unless a specific include path is given to the compiler, clang++, like most other C++ compilers, will interpret the path relative to the location of the source file. In this case, it needs to go up a directory and then into the include directory in order to locate the functions.h header file.
Add an include path to that of clang++
From reading the clang++ man page, it can be ascertained that the -I flag may be used to specify an additional include path for the compiler to search to find header files. So, assuming that the command was run from the ./ directory, one jump out of the src directory and the root of the project:
clang++ src/main.cpp -I./include
would also address the issue at hand.

Getting redefinitoin error when adding project as reference

I work on VS2015.
I have C++ project A that has a few files and MyClass.h. It's built with /clr flag.
I have another C++ console project B and is also built with /clr flag. The B has B.cpp with the following content:
//B.cpp
#include "MyClass.h"
void main()
{
MyClass* obj = new MyClass();
}
I succesfully can build project B. But, when I add project A as reference (right click->Add Reference) to project B, I suddenly get redefinition error of a class that's definied in a file in project A.
How adding a reference can cause such error?
UPDATE1
All header files have "header guard".
UPDATE2
I thought ProjectA is console and ProjectB is static lib. But, it was late at night and I didn't notice that I created both ProjectA and ProjectB as console applications.
I know that I need to export MyClass if ProjectB is a dll so I could use it in ProjectA. The example maybe stupid but since I got this by mistake I still want to understand why I get the redifinition error in this case. I've uploaded the test to here.
ProjectA has reference to ProjectB.
//ProjectA.cpp
#include "MyHeader.h"
int main()
{
return 0;
}
//ProjectB.cpp
#include "MyHeader.h"
//MyHeader.h
#pragma once
public enum class MyClass : int
{
};
If I remove the reference to ProjectB or I comment out #include "MyHeader.h" I do not get the redifinition error anymore.

Eclipse CDT Including Unreferenced Files in Automatically Generated Build

I'm new to cpp in eclipse and trying to mess with simple builds. I have made a basic project which automatically generates build info (no user-defined makefile).
Simple (Working) Case
I made a "Hello World" project called Test (not an empty project). It has one file - Test.cpp with a main() in => builds and runs fine.
Test.cpp
int main() {
// output some stuff
}
Slightly More Complex (Working) Case
I make a new file called Main.cpp. Move the main() function into Main.cpp and make a decleration of a function in main too - void test();
The test() function lives in Test.cpp, which is where I provide the function definition.
Main.cpp
#include <iostream>
void test();
int main() {
// Use test()
}
Test.cpp
#include <iostream>
void test() {
// output some stuff
}
Build it, there are now two .o files in the Debug directory - Test.o and Main.o. This runs fine.
The Problem
Now I try to introduce a third file - Limits.cpp.
Limits.cpp
#include <iostream>
void printLimits() {
// Print out limits for different integer sizes
}
Again I provide a deceleration of this function in Main.cpp.
Main.cpp
#include <iostream>
void test();
void printLimits();
int main() {
// Use printLimits()
}
This time there is no object file created for Limits in the Debug folder => the build fails.
Main.cpp:*line* undefined reference to `getLimits()'
It just looks like the autogenerated build config for the project is duff. I've tried looking around the project properties but I have had no luck. I've tried including the path/file in Include Paths/Include files, other objects, and I've checked just about every other property I can see. This has been frustrating me for two days.
The strange thing about this is case 2. It looks like because Test.cpp was the first file made it always includes this in the build? (even if it is not imported). Where as because Limits.cpp is new and not imported it doesn't generate an object file so it can't link the function.
I could me making an error by needing to include the files but my understanding is if all the object files make it to the linker then all will be well (ie I just need the function declarations when making the object files which is what I have here).
With a "Blank project" it seems to compile all the cpp files even if they are never used so my case above works. (Although, it doesn't work for files in subfolders of src). Looks like its a "feature" of the "Hello world" project type but it's going to drive me crazy knowing there must be a way to get it to do this.
If anyone knows if it is possible to include this file without writing my own makefile (I'm not a makefile guru (yet!)), or let me know if this is not possible with autogen CDT that would be great.
Using MinGW GCC toolchain.
Thanks

Bug in Xcode 7 about precompiled header and forward declaration in C++

I found a bug in latest Xcode 7.0 that annoys us very much in our company because it makes most of our C++ code not debuggable. After lots of experiment, I was able to reproduce it with a minimum amount of code.
In some cases, it is impossible to see members inside a C++ class on LLDB. It seems that three conditions must be present for that bug to appear:
the class is forward declared
the class has a virtual method
the class is declared inside a precompiled header
I am asking whether somebody else already knowns about that bug and what is the recommended procedure to report that bug (to LLVM or Apple ?).
Steps to reproduce:
Create two source files with their content:
header.h
#ifndef HEADER_INCLUDED
#define HEADER_INCLUDED
class A; // forward declaration, has an effect on bug
class A
{
public :
virtual ~A() {}
protected:
int doYouSeeMe;
};
#endif
PCHAndFDbug.cpp
#include "header.h"
int main()
{
A* a = new A();
return 0;
}
Create a small Xcode 7 project with these two files. header.h must be set as a precompiled header (Prefix header setting in Xcode). As a reference, I am using Premake to generate that project and here is the premake5.lua source:
solution "PCHAndFDbug"
configurations {"Debug"}
xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = "10.7" }
project "WithPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
pchheader "header.h"
project "WithoutPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
Place a breakpoint on the return 0 statement. Check if you can see member doYouSeeMe in a variable.
Same issue for me. Fixed by turning off "Enable Clang Module Debugging" in the Build Settings
For Apple at least, you should submit a bug report through the developer centre https://developer.apple.com/bug-reporting/
I've encountered this bug too and it is annoying.