My C++ project is built with CMake and includes tests utilizing GoogleTest. In order to keep me from compatibility issues between different projects and the installed/compiled GoogleTest version, I once decided to have GoogleTest always local in my project directories.
I achieved this by having GoogleTest downloaded and built as part of my own test project, that includes/links gtest. This looks like this in the respective CMakeLists.txt
:
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/googletest-download"
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/googletest-download"
)
In order to have this working there is an external project defined in CMakeLists.txt.in
:
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
So far so good!
This worked quite a while for me, but I run into some trouble, since I started to use C++17 features in my most recent project. This is a problem because my test project refuses to compile, when it includes code, that makes use of C++17.
The reason for this is, that running GoogleTest's CMake build modifies the CMAKE_CXX_FLAGS
variable on a global scale. In fact it overrides my definition of the compiler mode:
CXX_FLAGS = -Wall -pedantic -std=c++17 -std=gnu++11
... where it should look like this:
CXX_FLAGS = -Wall -pedantic -std=c++17
Is there a way to keep GoogleTest's build script from modifying that global variable? Or maybe have it restored?