I have a simple project going that uses CMake and gtest. I have a basic CMakeLists.txt file working, but I want to get a better understanding of how to use multiple CMakeLists.txt’s and connect them. The code so far for the project is like this:
https://github.com/dmonopoly/writeart/tree/10b62048e6eb6a6ddd0658123d85ce4f5f601178
For quicker reference, the only CMakeLists.txt file (in the project root) that I take advantage of has this inside:
cmake_minimum_required(VERSION 2.8)
# Options
option(TEST "Build all tests." OFF) # makes boolean 'TEST' available
# Make PROJECT_SOURCE_DIR, PROJECT_BINARY_DIR, and PROJECT_NAME available
set(PROJECT_NAME MyProject)
project(${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "-g") # -Wall")
#set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include) if you want your own include/ directory
# then you can do include_directories(${COMMON_INCLUDES}) in other cmakelists.txt files
################################
# Normal Libraries & Executables
################################
add_library(standard_lib Standard.cpp Standard.h)
add_library(converter_lib Converter.cpp Converter.h)
add_executable(main Main.cpp)
target_link_libraries(main standard_lib converter_lib)
################################
# Testing
################################
if (TEST)
# This adds another subdirectory, which has project(gtest)
add_subdirectory(lib/gtest-1.6.0)
enable_testing()
# Include the gtest library
# gtest_SOURCE_DIR is available due to project(gtest) above
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
##############
# Unit Tests
##############
# Naming
set(UNIT_TESTS runUnitTests)
add_executable(${UNIT_TESTS} ConverterTest.cpp)
# standard linking to gtest stuff
target_link_libraries(${UNIT_TESTS} gtest gtest_main)
# extra linking for the project
target_link_libraries(${UNIT_TESTS} standard_lib converter_lib)
# This is so you can do 'make test' to see all your tests run, instead of manually running the executable runUnitTests to see those specific tests.
add_test(NAME myUnitTests COMMAND runUnitTests)
endif()
My goal is to move Standard.cpp and Standard.h into lib/. The moment I do this, though, I find the ordering of what I do in my CMakeLists.txt complicated. I need the library for my gtest setup, but the library would have to get made in lib/CMakeLists.txt, right? Wouldn’t it easily become really complicated to find where all your libraries are and executables are as you would have to look through all your CMakeLists.txt’s?
If I am missing something conceptually, or if there is a good example I could use to solve this easily, that would be great.
Help appreciated, and thanks in advance.
If you don’t want to use multiple
CMakeLists.txtfiles, don’t.If you do want multiple
CMakeLists.txt, you’d move it out:and in
/lib/CMakeLists.txt:Here’s an example.