0

So I'm getting undefined reference to testing::UnitTest::Run() along with some others with gtest. I've compiled the libraries (libgtest.a and libgtest_main.a) and placed them in my lib folder for MinGW and got no where. Here is my CMakeList.txt:

cmake_minimum_required(VERSION 3.2)
project(proj_tests)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

include_directories("E:/Git/proj")
include_directories("D:/Development/Libraries/gtest-1.7.0/include")

find_package(gtest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(proj_tests ${SOURCE_FILES} containers/proj_test.h)
target_link_libraries(proj_tests ${GTEST_LIBRARIES} pthread)

I have done make on the gtest sample and was able to run that without any issues.

I'm using CLion on Windows 7 for the project. How can I make this Google Test framework thing work properly?

  • So, what is the symptom? – rholmes Jul 23 '15 at 15:07
  • I think the key is to understand what ``find_package(gtest REQUIRED)`` is finding. What is the value of ``${GTEST_LIBRARIES}``? For what it is worth, I am not aware of a standard way to find gtest as an external module because the gtest maintainers do not recommend using gtest this way--they suggest you build gtest with your sources rather than linking to an external library. – Phil Jul 23 '15 at 15:12
  • "undefined references" for my project. – Keyboard embossed forhead Jul 23 '15 at 15:14
  • @Phil: So dump the entire gtest src folder in the project? – Keyboard embossed forhead Jul 23 '15 at 15:15
  • Found similar question: http://stackoverflow.com/questions/12657596/gtest-undefined-references. Also, if you want to link with `libgtest_main.a`, you also need `GTEST_MAIN_LIBRARIES` variable (or just use `GTEST_BOTH_LIBRARIES`). – Tsyvarev Jul 23 '15 at 17:06
  • Thank you to all. These answers got me in the right direction for a solution. – Keyboard embossed forhead Jul 24 '15 at 20:42

3 Answers3

1

When you build gtest, you should have the following three files:

  • include_fused/gtest/gtest.h
  • include_fused/gtest/gtest-all.cc
  • include_fused/gtest/gtest_main.cc

If you are providing your own main you just need the first two. If you want to use the gtest main, you need the third one too. The recommendation is to add these to each unit test project that you are building.

Phil
  • 5,822
  • 2
  • 31
  • 60
1

So after a lot of hurt I switched to Linux and tried it there by downloading and compiling the libgtest-dev and using the configuration from Erik Smistad's blog. It worked within the CLion project without issues meaning something weird was happening to my Windows compiled Google Test library.

For windows/MinGW: Here is the solution I reached for Windows..

  1. I got pre-compiled libraries from Richard Pattis's UCI webpage on how to get the google test framework to work on eclipse.
  2. the gtest folder in include was copied to the mingw32\include folder,
  3. the gtest_main.a and libgtest.a files from the make folder files were copied to mingw32\lib folder.

The final working CMake configuration looks like this for me:

cmake_minimum_required(VERSION 3.2)
project(eadlib_tests)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

#path to project to test
include_directories("E:/Git/eadlib")

#Google test framework stuff
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(eadlib_tests ${SOURCE_FILES} containers/eadlib_test.h)
target_link_libraries(eadlib_tests ${GTEST_LIBRARIES} pthread)
0

like this (here's a snippet from one of my library projects)

find_package(GTest REQUIRED)
target_link_libraries(cpputil_test cpputil ${CMAKE_THREAD_LIBS_INIT} ${GTEST_BOTH_LIBRARIES} ${Boost_LIBRARIES} )
add_test(NAME cpputil_test COMMAND cpputil_test)

Of course in my case the project is called cpputil_test. You'll need to replace it with your own.

Note also the use of CMAKE_THREAD_LIBS_INIT which allows your code to be portable across all host systems. It hides the dependency on pthreads when building for linux (OSX, iOS and Windows for example, do not have this dependency).

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142