28

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 messages at me. I've referenced the gtest header file, so I'm not sure what is wrong. Any ideas? I'm new to the entire subject of both unix and unit testing , so I could very well be missing something simple. Thanks in advance!

Error Messages:

CMakeFiles/Proj2.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
main.cpp:(.text+0x23): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text+0x2b): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status

main.cpp

#include "gtest/gtest.h"

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Test.cpp

#include "gtest/gtest.h"
#include "Testable.h"

TEST(GetTwoTest, Two) {
    EXPECT_EQ(2, GetTwo());
}

Testable.cpp

#include "Testable.h"

int GetTwo() {
    return 3;
}

Here is my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.6)

SET(CMAKE_CXX_FLAGS "-std=gnu++11") #Turn on C++11 Support

set(FILES_TO_TEST Testable.cpp)
set(UNIT_TESTS Test.cpp)
set(MAIN_FILE main.cpp)

add_subdirectory(gtest) #Build all the gtest stuff
include_directories(gtest/include)
include_directories(.)
add_library(codeToTest ${FILES_TO_TEST})

add_executable(Proj2 ${MAIN_FILE})
target_link_libraries(Proj2 codeToTest)

add_executable(unit-test ${UNIT_TESTS})
target_link_libraries(unit-test gtest gtest_main rt pthread codeToTest)
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
Vance
  • 471
  • 4
  • 8
  • 16
  • Note that Google recommend that you DO NOT build a library, but instead include the GTest code into your project. See https://code.google.com/p/googletest/wiki/FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog – Mawg says reinstate Monica Aug 14 '15 at 14:21

3 Answers3

16

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.

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Thanks, that was my problem. I was using gtest_main in the CMakeLists file, so I just had to remove the gtest functions in my main.cpp. – Vance Sep 30 '12 at 18:11
4

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • 1
    Thanks for the help. I read over the link you provided, but it seemed to skip right over the details on correctly linking the gtest Library (since I'm not using an IDE). I used CMake to generate the build files, so that's where the linking should take place, no? I provided my CMakeLists.txt file for further clarification – Vance Sep 30 '12 at 01:59
  • 1
    Follow this link: http://stackoverflow.com/questions/8507723/how-to-start-working-with-gtest-and-cmake – PiotrNycz Sep 30 '12 at 03:17
0

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985