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.
My answer is based on the answer from firegurafiku. I modified it in the following ways:
CMAKE_ARGS
to theExternalProject_Add
call so it works with msvc.INTERFACE_INCLUDE_DIRECTORIES
does not yet exist because the external project has not yet been built.I prefer keeping gtest as an external project rather than adding its source directly to my project. One reason is because I do not like having the gtest source code included when I am searching my code. Any special build flags that are needed by my code that should also be used when building gtest can be added to the definition of
CMAKE_ARGS
in the call toExternalProject_Add
Here is my modified approach:
It is long past when the original question being asked, but for the benefit of others, it is possible to use
ExternalProject
to download the gtest source and then useadd_subdirectory()
to add it to your build. This has the following advantages:Used in the normal way, ExternalProject won't do the download and unpacking at configure time (i.e. when CMake is run), but you can get it to do so. I've written a blog post on how to do this which also includes a generalised implementation which works for any external project which uses CMake as its build system, not just gtest. You can find it here:
https://crascit.com/2015/07/25/cmake-gtest/
Update: The approach described above is now also part of the googletest documentation.
There is a bit less complex solution using
ExternalProject
module and imported libraries feature ofcmake
. It checks out code from repository, builds it and creates target from built static libraries (they'relibgtest.a
andlibgtest_main.a
on my system).You may want to replace
SVN_REVISION
or addLOG_CONFIGURE
andLOG_BUILD
options here. AfterGTest
andGTestMain
targets are created, they can be used like this:or, if you have your own
main()
function:This is an unusual case; most projects specify install rules.
CMake's
ExternalProject_Add
module 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/src
and create a project file (MyTest/build/Test.sln
on 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.