linker error lnk2005
-
I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards
-
I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards
Roberto64_Ge wrote:
main.cpp includes header.cpp which includes header.h
This is probably the source of your problem. Visual Studio is probably compiling header.cpp and creating a separate object file, then compiles main.cpp, which includes header.cpp, and so creates two copies of the methods defined in header.cpp. In general it is a mistake to
#include
a cpp file in another cpp file. You main.cpp only needs to include header.h, which should only provide a declaration of your class. You would then compile main.cpp and header.cpp separately. You can do this on the ubuntu command line usingg++ -Wall -Wextra main.cpp header.cpp -o main
or alternatively
g++ -Wall -Wextra -c main.cpp
g++ -Wall -Wextra -c header.cpp
g++ main.o header.o -o mainIn the above example, the
-c
option to g++ tells the compiler to only produce an intermediate object file (e.g. a .o file), and not try to produce an executable. The third line creates the final executable, and includes all the system libraries and startup code needed for the executable.Keep Calm and Carry On
-
I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards
Roberto64_Ge wrote:
main.cpp includes header.cpp which includes header.h
This means that all the definitions in header.cpp will be duplicated in the object files. Do not include .cpp files in others, only include the actual headers, i.e. in this case main.cpp should only include header.h.
-
Roberto64_Ge wrote:
main.cpp includes header.cpp which includes header.h
This is probably the source of your problem. Visual Studio is probably compiling header.cpp and creating a separate object file, then compiles main.cpp, which includes header.cpp, and so creates two copies of the methods defined in header.cpp. In general it is a mistake to
#include
a cpp file in another cpp file. You main.cpp only needs to include header.h, which should only provide a declaration of your class. You would then compile main.cpp and header.cpp separately. You can do this on the ubuntu command line usingg++ -Wall -Wextra main.cpp header.cpp -o main
or alternatively
g++ -Wall -Wextra -c main.cpp
g++ -Wall -Wextra -c header.cpp
g++ main.o header.o -o mainIn the above example, the
-c
option to g++ tells the compiler to only produce an intermediate object file (e.g. a .o file), and not try to produce an executable. The third line creates the final executable, and includes all the system libraries and startup code needed for the executable.Keep Calm and Carry On
Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?
-
Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?
-
Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?
Ok I tried and I did it. I right clicked on header.cpp and it gave the option to compile. This created the header.obj. Then I right clicked on the main.cpp and again I had the option to compile it and this generated the exe. Before doing all this I included in the main.cpp only header.h, Now it works. Thanks for the clear explanation
-
Roberto64_Ge wrote:
main.cpp includes header.cpp which includes header.h
This means that all the definitions in header.cpp will be duplicated in the object files. Do not include .cpp files in others, only include the actual headers, i.e. in this case main.cpp should only include header.h.
ok thanks
-
Ok I tried and I did it. I right clicked on header.cpp and it gave the option to compile. This created the header.obj. Then I right clicked on the main.cpp and again I had the option to compile it and this generated the exe. Before doing all this I included in the main.cpp only header.h, Now it works. Thanks for the clear explanation
That's one way to do it, but not the most efficient: [Building and Cleaning Projects and Solutions - Visual Studio (Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/ide/building-and-cleaning-projects-and-solutions-in-visual-studio?view=vs-2022)
Keep Calm and Carry On
-
That's one way to do it, but not the most efficient: [Building and Cleaning Projects and Solutions - Visual Studio (Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/ide/building-and-cleaning-projects-and-solutions-in-visual-studio?view=vs-2022)
Keep Calm and Carry On
ok i will read it :thumbsup::thumbsup::thumbsup: :)