Anon234_4521 Anon234_4521 - 13 days ago 5
C++ Question

Gtest test project link to other executable

I have a project that I added a gtest project to, then turned the original project into a .lib project and called its main() function from a 3rd, new project. This way, I can build the test .exe with the project .exe. Except, it's not working.

These are the steps I've followed as mentioned here

Create or open a project, then create a Win32 Console Application test project for it.

Right click the test project, go to properties, and set Configuration to debug.

Go to Configuration Properties > C/C++ > General > Additional Include Directories and add the gtest includes folder.

Under Code Generation > Runtime Library, choose MDt or MTd; whatever is used by the project to be tested.

Under Linker > General > Additional Library Directories, add a reference to the folder with gtestd.lib.

Under Input > Additional Dependencies, add gtestd.lib.

Right click the test project, go to properties, and set Configuration to release, then perform steps 3 and 4.

Under Linker > General > Additional Library Directories, add a reference to the folder with gtest.lib.

Under Input > Additional Dependencies, add gtest.lib.

These are the steps I've followed for linking the test project, as mentioned here

Right click the test project, go to Build Dependencies > Project Dependencies, and add the project to be tested.

Go to Properties > C/C++ > General > Additional Include Directories and include headers from the project to test.

Under Linker > Input > Additional Dependencies, enter the .lib file of the project

Here is the problem

For some reason, the test project surprisingly works, but only in release mode. The wrapper project doesn't work in either mode, despite having the same settings as the test project. I've looked at 20 possible solutions and have yet to find one. I also tried __declspec(dllexport) in front of every method in the library project, but that doesn't do any justice. VS is fighting me tooth-and-nail.

Other stuff I tried

I don't want to change the project to build a .lib every time I need to test. So I looked into creating both a .exe and .lib from the project using a pre-link build event, but it seems to only take console commands and I couldn't figure out how to hook up the resulting .lib file from the console commands I got from another SO answer.

Here is the current .sln file with these problems. URLs are absolute: https://drive.google.com/file/d/0B6r81tGW7hODeXNpR3ROc3hYMWc/view?usp=sharing

How do I simply make a test project and wrapper project to launch a normal project (that works in both debug and release mode)?

Answer

Fixes:

  1. In SimpleCalc (debug), change Project | Properties | General | Configuration type to Static Library. This is why Release worked but Debug didn't.
  2. For SimpleCalcWrapper, select Project | Add reference... and add SimpleCalc. This is why SimpleCalcWrapper didn't work.
  3. For SimpleCalcTest (release), reset to default Project | Properties | Linker | Advanced | Import Library or SimpleCalcTest.exe will not compile.
  4. For SimpleCalcTest, change Project | Properties | C/C++ | Additional Include Directories to $(SolutionDir)\gtest\include;$(SolutionDir)\SimpleCalc;%(AdditionalIncludeDirectories). This will have them relative to the project.
  5. For SimpleCalcTest, change Project | Properties | Linker | Additional Library Directories to $(SolutionDir)\gtest;%(AdditionalLibraryDirectories) - you don't need SimpleCalc directory because the project is added to references.
  6. For SimpleCalcWrapper, change Project | Properties | C/C++ | Additional Include Directories to $(SolutionDir)\SimpleCalc;%(AdditionalIncludeDirectories)
  7. For SimpleCalcWrapper, reset to default Project | Properties | Linker | Additional Library Directories - you don't need SimpleCalc directory because the project is added to references.