我有一个使用 CMake 和 gtest 的简单项目。我有一个基本的 CMakeLists.txt 文件正在工作,但我想更好地了解如何使用多个 CMakeLists.txt 并连接它们。到目前为止,该项目的代码是这样的:
https://github.com/dmonopoly/writeart/tree/10b62048e6eb6a6ddd0658123d85ce4f5f601178
为了更快地参考,我利用的唯一 CMakeLists.txt 文件(在项目根目录中)有这个:
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()
我的目标是将 Standard.cpp 和 Standard.h 移入 lib/。不过,在我这样做的那一刻,我发现我在 CMakeLists.txt 中所做的事情的顺序很复杂。我的 gtest 设置需要该库,但该库必须在 lib/CMakeLists.txt 中创建,对吗?查找所有库和可执行文件的位置不会变得非常复杂,因为您必须查看所有 CMakeLists.txt 文件吗?
如果我在概念上遗漏了一些东西,或者如果有一个很好的例子可以用来轻松解决这个问题,那就太好了。
帮助表示赞赏,并提前感谢。