Gtest: Undefined References

后端 未结 3 1241
半阙折子戏
半阙折子戏 2020-12-31 00:14

I am trying to use GoogleTest to test a simple function, but as I run make in my build folder, the compiler throws Undefined Reference error messag

相关标签:
3条回答
  • 2020-12-31 00:19

    Put the libgtest.a after your object files

    If you are doing things manually instead of with CMake, make sure to do:

    g++ main.cpp googletest/build/lib/libgtest.a
    

    instead of:

    g++ googletest/build/lib/libgtest.a main.cpp
    

    Here's a full working example I've tested with: https://askubuntu.com/questions/97626/how-to-install-googletest/1295185#1295185

    This problem is not exclusive to GoogleTest: I can also reproduce it with a minimal library example like this one and Eli explainss the ordering rules which I don't have the patience to learn right now.

    0 讨论(0)
  • 2020-12-31 00:21

    Your setup looks to be almost correct. However, you're needing to have 2 separate main functions; one for the real executable Proj2 and another with the gtest includes and functions for the test executable unit-test.

    You could do this by having 2 different main.cpp files, say main.cpp and test_main.cpp. The one you've shown would be test_main.cpp, and would be included in the add_executable(unit-test ... command.

    Your new main.cpp would have no references to gtest, either includes or functions.

    0 讨论(0)
  • 2020-12-31 00:41

    From linker errors it is obvious that you did not link gtest library to your test program.

    See Primer:

    To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. ...

    Just see this doc for details about your compiler and system.

    0 讨论(0)
提交回复
热议问题