I have a Visual Studio solution containing 2 project: main project and test (via googletest). In main project I have myclass.cpp and myclass.h files. When I’m trying to compile test project, there are bunch of LNK2019 errors when I include “myclass.h” in my test.cpp file, but everything works fine if I include “myclass.cpp” instead. Is that normal? As far as I know, including of .cpp files is not recommended and generally can be avoided. Any suggestions?
Share
It’s normal. If you have 2 projects, 2 binaries will be generated.
Don’t include the cpp file.
Instead, link the binaries together.
main project – generates
.libfile and either.dllor.exe.test project – includes header from
main. You need to add the.libgenerated by main in the additional dependencies of the test project. Somewhere in the Project Settings – Linker Options – Additional Dependencies.You can generate both .exe and .lib file from a single project. To do this you set:
Linker -> General -> Output FileLinker -> Advanced -> Import LibraryYou may also need to mark exported functions with
__declspec( dllexport )in the .exe project (see docs), otherwise compiler won’t generate a .lib file.