在 CMake 中有没有办法将库用于具有不同静态依赖项的多个目标? 为了更好地解释它,考虑这个最小的例子:我想要两个可执行文件:第一个应该打印“YES”,第二个应该打印“NO”。为此,我使用了库“printsth”,它打印“某物”。它打印的字符串来自“用户”(printyes 或 printno)提供的头文件。这看起来像这样:
├── apps
│ ├── printno
│ │ ├── CMakeLists.txt
│ │ │ add_executable(printno main.cpp)
│ │ │ target_link_libraries(printno PRIVATE printsth)
│ │ │
│ │ ├── main.cpp
│ │ │ #include "printsth/printsth.h"
│ │ │
│ │ │ int main() {
│ │ │ printsth();
│ │ │ return 0;
│ │ │ }
│ │ │
│ │ └── print_usr.h
│ │ #define USR_STRING "NO"
│ │
│ └── printyes
│ │ ├── CMakeLists.txt
│ │ │ add_executable(printyes main.cpp)
│ │ │ target_link_libraries(printyes PRIVATE printsth)
│ │ │
│ │ ├── main.cpp
│ │ │ #include "printsth/printsth.h"
│ │ │
│ │ │ int main() {
│ │ │ printsth();
│ │ │ return 0;
│ │ │ }
│ │ │
│ │ └── print_usr.h
│ │ #define USR_STRING "YES"
│ │
├── extern
│ └── printsh
│ ├── include
│ │ └── printsh
│ │ └── printsh.h
│ │ void printsth();
│ │
│ ├── src
│ │ ├── CMakeLists.txt
│ │ │ add_library(printsth printsth.cpp)
│ │ │ target_include_directories(printsth PUBLIC ../include)
│ │ │
│ │ └── printsh.cpp
│ │ #include "printsth/printsth.h"
│ │ #include "print_usr.h"
│ │ #include <iostream>
│ │
│ │ void printsth() {
│ │ std::cout << USR_STRING << std::endl;
│ │ }
│ │
│ └── CMakeLists.txt
│ cmake_minimum_required(VERSION 3.11...3.16)
│
│ project(printsh
│ VERSION 0.1
│ DESCRIPTION "Print something"
│ LANGUAGES CXX)
│
│ add_subdirectory(src)
│
└── CMakeLists.txt
cmake_minimum_required(VERSION 3.11...3.16)
project(printexamples
VERSION 0.1
DESCRIPTION "Print examples"
LANGUAGES CXX)
add_subdirectory(apps/printyes)
add_subdirectory(apps/printno)
add_subdirectory(extern/printsth)
在构建时我显然得到了错误
fatal error: print_usr.h: No such file or directory
那么我可以告诉 CMake 在为 printno 构建 printsh 库时使用 apps/printno 作为 include 目录并在为 printyes 构建时使用 apps/printyes 作为 include 目录有什么改变吗?
我知道这个例子没有多大意义,并且很容易摆脱 header 依赖性(例如,将自定义字符串作为参数传递给 printsth())并且一切正常。所以这只是一个演示“现实世界”问题的例子,我无法轻易摆脱依赖关系。
最佳答案
在 CMake 中,库目标 表示编译一次但可以链接到多个目标(可执行文件或其他库)的库。
因为您不能将两个宏定义都编译到一个库中,所以您需要为每个定义集创建一个不同的库目标。也就是说,在您的情况下,您需要创建两个库。
对于重复的命令序列,CMake 提供了宏和函数,并使用不同的参数多次调用它们。
您还可以创建一个“参数化”CMakeLists.txt
,并多次“调用”它(通过add_subdirectory
):
extern/printsh/src/CMakeLists.txt:
# Requires 'printsth_target' and 'printish_usr_dir' variables to be set.
# The first variable denotes name of the target created,
# the second variable denotes include directory with 'print_usr.h' header.
if (NOT printsth_target)
message(FATAL_ERROR "printsth_target variable is required but is not set.")
endif()
if (NOT printish_usr_dir)
message(FATAL_ERROR "printish_usr_dir variable is required but is not set.")
endif()
# Create a library target with user-provided name
add_library(${printsth_target} printsth.cpp)
target_include_directories(${printsth_target} PUBLIC ../include)
# Add user-provided include directory
target_include_directories(${printsth_target} PRIVATE ${printish_usr_dir})
使用这样的脚本,printsth
库可以在应用程序的 CMakeLists.txt
中实例化。
apps/printno/CMakeLists.txt:
add_executable(printno main.cpp)
# Instantiate 'printsth' library with needed parameters.
set(printsth_target "printsh_no")
set(printish_usr_dir ${CMAKE_CURRENT_SOURCE_DIR})
# Need to specify the second argument - build directory, where the library will be built.
add_subdirectory(../../extern/printsh printsh_no_dir)
# No we can link with a library. Use a name, specified for 'printsth_target' variable.
target_link_libraries(printno PRIVATE printsh_no)
顶级 CMakeLists.txt
应该修改为不实例化库。
CMakeLists.txt:
# <...>
add_subdirectory(apps/printyes)
add_subdirectory(apps/printno)
# Uncommenting the following line causes FATAL_ERROR triggered.
#add_subdirectory(extern/printsth)
关于c++ - 在 CMAKE 中使用具有静态依赖项的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61887926/