Search code examples
c++visual-studiovisual-c++linker

C++ Google Tests and Visual Studio Linker Error


I am trying to write tests using the google test framework for a C++ project of mine in Visual Studio.

Here is my current project structure:

solution
|
+-- project
|   |
|   +-- src
|       |
|       +-- .testee.cpp
|
+-- test_project
|    |
|    +-- testee_tester.cpp
|
+-- x64
    |
    +-- Debug (standard debug output folder for a generic c++ solution, I haven't changed this)
        |
        +-- testee.obj (files output by compiler and used by linker)

Here how I set up the test_project:

First the test_project has the project as a reference so the build order is: first project then test_project.

Second here are the test project properties:

VC++ Directories -> Include Directories: $(SolutionDir)project\src\;$(IncludePath)
VC++ Directories -> Library Directories: $(OutDir);$(LibraryPath)

C/C++ -> General -> Additional Include Directories: $(SolutionDir)project\src\;
%(AdditionalLibraryDirectories);

Linker -> General -> Additional Library Directories: $(OutDir); %(AdditionalLibraryDirectories);
Linker -> Input -> Additional Dependencies: testee; %(AdditionalDependencies)

I have changed the names of the files and directories to make them easier to read and understand without more context so:

solution = sim-world
project = green-sim
test_project = green-sim-test

testee.cpp = spherical-tensor.cpp
testee_tester = test.cpp (temporary name until I get this figured out)

And yet when building:

Build started at 3:29 PM...
1>------ Build started: Project: green-sim-test, Configuration: Debug x64 ------
1>LINK : fatal error LNK1104: cannot open file 'spherical-tensor.obj'
1>Done building project "green-sim-test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
========== Build completed at 3:29 PM and took 00.381 seconds ==========

So the project builds correctly, but the test_project is unable to build because it can't find the .obj file of the testee.cpp file.

I have followed the official Microsoft tutorials for setting these up but I can't quite figure it out.

Here are the tutorials I'm using:

Write unit tests for C/C++ in Visual Studio

Use the Microsoft Unit Testing Framework for C++ in Visual Studio

Use Google Test for C++ in Visual Studio

Am I not setting up the project properties correctly?

Why is the linker not finding the .obj file that does exist (I've checked the folder).

Thank you! I hope the question is nice and clear!


Solution

  • I figured out my mistake: The solution actually has two types of outputs, therefore my understanding of the solution file architecture was wrong:

    Solution Output

    solution-directory\x64\Debug\
    

    this is equivalent to $(OutDir) which contains the final executables such as project-name.exe

    and: Project Output

    solution-directory\project-directory\x64\Debug\
    

    which contains all the compiled source code waiting to be linked together such as: main.obj

    Therefore the linker was searching the wrong folder. Tada, there is always a simple explanation.

    Conclusion

    In the test_project properties:

    Linker -> General -> Additional Library Directories
    

    Make sure you add the Project Output ie: $(SolutionDir)project\src\;

    Hope this helps!