gpt4 book ai didi

static - CMake:使用同一静态库的多个子项目

转载 作者:行者123 更新时间:2023-12-03 11:18:43 25 4
gpt4 key购买 nike

我正在使用cmake编译我的工作项目之一,这是交易

-
client/
CMakeLists.txt
server/
CMakeLists.txt
libs/
libstuff/
CMakeLists.txt
CMakeLists.txt

因此,我希望能够单独编译每个子项目,并从根文件夹构建客户端和服务器。

假设客户端和服务器需要libstuff。

我尝试在客户端和服务器CMakeLists.txt中都使用带有lib路径的“add_subdirectory”,当您编译服务器或客户端时,它可以工作,但是如果您尝试从根目录运行它们:
CMake Error at common/libplugin/CMakeLists.txt:33 (ADD_LIBRARY):
add_library cannot create target "plugin" because another target with the
same name already exists. The existing target is a static library created
in source directory "/home/adrien/git/r-type/common/libplugin". See
documentation for policy CMP0002 for more details.

所以我有点新w / cmake,我不确定应该怎么做,我应该使用add_dependencies吗?

谢谢你的帮助,

最佳答案

一个简单的解决方案是使用TARGET条件(即:)使用add_subdirectory保护客户端和服务器CMake列表文件中的if调用:

if (NOT TARGET plugin)
add_subdirectory("${CMAKE_SOURCE_DIR}/common/libplugin")
endif()

这样可以防止多次添加 libplugin子目录。

关于static - CMake:使用同一静态库的多个子项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com