I had need to use Google’s C++ unit testing frame work, gtest
, in one of my projects. I spent a lot of time Googling on what folders to include, where to put it, how to configure cmake
, etc. Here’s the guide so you don’t end up pulling out your hair like I did.
When you download the latest release of gtest
, put the entire repo (unzipped of course) into your project folder. I put it in a folder called includes
.
Now the cmake
file, here is what CLion started me out with:
1 2 3 4 5 6 |
cmake_minimum_required(VERSION 3.0) project(Dice) set(CMAKE_CXX_STANDARD 11) add_executable(Dice dice.h main.cpp) |
And this is all I had to add to get everything to compile and the tests to run:
8 9 10 11 |
add_subdirectory(include) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x") add_executable(DiceTest test.cpp) target_link_libraries(DiceTest gtest gtest_main) |
The important bits are adding the subdirectory where you put the gtest
repo and linking the gtest
and gtest_main
libraries to your project. If you are using G++, the flag is needed too.
That’s it!