Getting redefinitoin error when adding project as reference - c++

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.

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.

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

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

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)
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.

Linker error LNK2019?

I have the following files
file1_moduleA.hpp
file1_moduleA.cpp
sharedFile.hpp
file1_moduleB.cpp
//sharedFile.hpp
extern CustomClass *doSomething(CustomClass *var1, const char *var2);
extern CustomClass *doSomethingElse(const char *var1);
template <class MYCLASS_A>
void myFunction(CustomClass *var1, const char* var2){
assert(somthing);
if (condition){
new (MYCLASS_A);
}
}
//file1_moduleA.cpp
#include "sharedFile.hpp"
// Contains the definitions of doSomething and doSomethingElse among others
//file1_moduleA.hpp
// Other declarations
//file1_moduleB.cpp
#include"sharedFile.hpp"
//...SNIPPETS OF CODE
void someFunction(CustomClass* var1){
doSomething(var1, "FOO");
}
//...
The following are in one Visual Studio Project, Project A:
file1_moduleA.hpp, file1_moduleA.cpp and sharedFile.hpp
The following are in another VS Project, Project B:
file1_moduleB.cpp, file1_moduleB.hpp
Project A compiles and links perfectly, whereas Project B compiles but gives an unresolved symbol for CustomClass *doSomething(CustomClass *var1, const char *var2) at someFunction in file1_moduleB.cpp
I have tried defining the function with and without extern; tried using a separate file for the template in file1_moduleA.hpp; tried inducing dependency between ProjectB and ProjectA in VS, but nothing seems to work. I am unsure why the definition is not being found during linking. ProjectA.lib however, is being created.
Any help in this regard would be appreciated.
if you are using visual studio, and these are different project, then make project B dependent upon project A.
Right click on the solution.
Go to project dependencies.
Choose Project B.
Click Project A (making project B dependent upon Project A)
rebuild.
Did you include shared file in project B?