CMake 文件分别构建主要和测试
CMake files to build main and tests separately
我的目录结构如下:
main.cpp
CMakeLists.txt
src/
some_function.h
some_function.cpp
some_class.h
some_class.cpp
CMakeLists.txt
test/
catch.hpp
tests.cpp
CMakeLists.txt
CmakeLists.txt 在项目根目录中:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(main)
add_subdirectory (src)
add_executable(main main.cpp)
target_link_libraries (main some_class)
CmakeLists.txt 在 src/:
add_library (some_class some_class.h some_class.cpp some_function.h some_function.cpp)
以上工作用于构建 运行 main
目标。
现在我想构建 运行 测试。文件 tests.cpp 包括 some_function.h 和 some_class.h。但是,我不确定如何在此处添加 src/ 目录。
这是我目前在测试中的内容/(导致 some_function.h 中函数的链接错误):
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(tests)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
add_executable(tests tests.cpp)
target_link_libraries(tests Catch)
只是 link some_class
库目标到 tests
目标,就像你对 main
可执行目标所做的那样。
CmakeLists.txt 在项目根目录中:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(main)
add_subdirectory (src)
# Add the test sub-directory also.
add_subdirectory(test)
add_executable(main main.cpp)
target_link_libraries (main some_class)
CmakeLists.txt 在 test
目录中:
project(tests)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
add_executable(tests tests.cpp)
# Link 'some_class' here also!
target_link_libraries(tests PRIVATE some_class Catch)
由 add_library()
或 add_executable()
创建的普通目标具有项目范围而不是目录,这就是为什么它们的名称在项目中必须是唯一的。所以你可以“target_link” some_class
到你的目标 tests
即使目标 tests
不在 src/
...
的子目录中
The <name>
corresponds to the logical target name and must be globally unique within a project.
参考:https://cmake.org/cmake/help/latest/command/add_library.html#normal-libraries
这不是导入目标的情况。
The [imported] target name has scope in the directory in which it is created and below, but the GLOBAL option extends visibility. It may be referenced like any target built within the project.
参考:https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries
我的目录结构如下:
main.cpp
CMakeLists.txt
src/
some_function.h
some_function.cpp
some_class.h
some_class.cpp
CMakeLists.txt
test/
catch.hpp
tests.cpp
CMakeLists.txt
CmakeLists.txt 在项目根目录中:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(main)
add_subdirectory (src)
add_executable(main main.cpp)
target_link_libraries (main some_class)
CmakeLists.txt 在 src/:
add_library (some_class some_class.h some_class.cpp some_function.h some_function.cpp)
以上工作用于构建 运行 main
目标。
现在我想构建 运行 测试。文件 tests.cpp 包括 some_function.h 和 some_class.h。但是,我不确定如何在此处添加 src/ 目录。
这是我目前在测试中的内容/(导致 some_function.h 中函数的链接错误):
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(tests)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
add_executable(tests tests.cpp)
target_link_libraries(tests Catch)
只是 link some_class
库目标到 tests
目标,就像你对 main
可执行目标所做的那样。
CmakeLists.txt 在项目根目录中:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(main)
add_subdirectory (src)
# Add the test sub-directory also.
add_subdirectory(test)
add_executable(main main.cpp)
target_link_libraries (main some_class)
CmakeLists.txt 在 test
目录中:
project(tests)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
add_executable(tests tests.cpp)
# Link 'some_class' here also!
target_link_libraries(tests PRIVATE some_class Catch)
由 add_library()
或 add_executable()
创建的普通目标具有项目范围而不是目录,这就是为什么它们的名称在项目中必须是唯一的。所以你可以“target_link” some_class
到你的目标 tests
即使目标 tests
不在 src/
...
The
<name>
corresponds to the logical target name and must be globally unique within a project.
参考:https://cmake.org/cmake/help/latest/command/add_library.html#normal-libraries
这不是导入目标的情况。
The [imported] target name has scope in the directory in which it is created and below, but the GLOBAL option extends visibility. It may be referenced like any target built within the project.
参考:https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries