0
votes

I have a large C++ library, and want to do some testing with GTest. At the moment, the build is handled with CMake, in particular there is one CMakeLists.txt file in the root directory like the following

make_minimum_required(VERSION 3.13.0)
project(mylib)

find_package(PkgConfig REQUIRED)

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/protobuf/src/.libs/")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

SET(BASEPATH "${CMAKE_SOURCE_DIR}")
INCLUDE_DIRECTORIES("${BASEPATH}")

add_executable(mylib run.cpp)
add_subdirectory(src)
add_subdirectory(proto)


target_include_directories(mylib PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/math
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/protobuf/src
    ... some dirs ...
)

target_link_directories(mylib PRIVATE
    ... some libs ..
)

target_link_libraries(mylib 
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/math
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/protobuf/src
    .....
)

target_compile_options(mylib PUBLIC -D_REENTRANT -fPIC)

Then in the src directory and every sub-directory there is a CMakeLists.txt file, for example this is in src/

target_sources(mylib
  PUBLIC
    includes.hpp
)

add_subdirectory(algorithms)
add_subdirectory(collectors)
add_subdirectory(hierarchies)
add_subdirectory(mixings)
add_subdirectory(runtime)
add_subdirectory(utils)

My question here is the following: what is the least painful way to integrate GTest in the current project? I was thinking of having a test/ subdirectory, like I've seen here: Adding Googletest To Existing CMake Project

However this example requires that for each executable you manually list all the files it includes. Is there a quicker way to use the sources that are already added to 'mylib'?

1

1 Answers

1
votes

You can split the current mylib executable target into two targets

  • mylib, a library target that is very much like the current mylib target, but without the run.cpp file
  • mylib_exe an executable target that compiles run.cpp and links to mylib

Now your test files can link to mylib.