I just downloaded googletest, generated its makefile with CMake and built it. Now, I need to use it in my testing project.
With CMake, I have been advised not pointing to gtest libraries directly (using include _directories or link_directories) but use find_package() instead.
The problem is, there is no install target for the gtest makefile generated. I cannot understand how find_package(GTest REQUIRED) could work without some kind of installation. Also, putting the gtest folder as a subfolder in my project is not possible.
Thanks for any help.
This is an unusual case; most projects specify install rules.
CMake’s
ExternalProject_Addmodule is maybe the best tool for this job. This allows you to download, configure and build gtest from within your project, and then link to the gtest libraries.I’ve tested the following CMakeLists.txt on Windows with Visual Studio 10 and 11, and on Ubuntu using GCC 4.8 and Clang 3.2 – it might need adjusted for other platforms/compilers:
If you create this as CMakeLists.txt in an empty directory (say
MyTest), then:This should create a basic main.cpp in
MyTest/srcand create a project file (MyTest/build/Test.slnon Windows)When you build the project, it should download the gtest sources to
MyTest/build/ThirdParty/src/googletest, and build them inMyTest/build/ThirdParty/src/googletest-build. You should then be able to run the MainTest target successfully.