CMake:使用同一静态库的多个子项目
我正在使用cmake来编译我的工作项目之一,这就是交易
I'm using cmake to compile one of my work projets, here is the deal
-
client/
CMakeLists.txt
server/
CMakeLists.txt
libs/
libstuff/
CMakeLists.txt
CMakeLists.txt
所以我希望能够单独编译每个子项目,并从根文件夹构建客户端和服务器。
So i want to be able to compile each subproject individually, and build both the client and the server from the root folder.
让我们说客户端和服务器需要libstuff。
Let's say the client and the server need libstuff.
我尝试将 add_subdirectory与客户端和服务器CMakeLists.txt中lib的路径,当您编译服务器或客户端时,它都可以工作,但是如果您尝试从根目录运行它们:
I tried to use "add_subdirectory" with the path of the lib in both client and server CMakeLists.txt, it works when you compile the server or the client, but if you try to run both from the root directory :
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.
所以我有点新意,我不确定应该怎么做,我应该使用add_dependencies吗?
So i'm kind of new w/ cmake and i'm not sure what i should do, should i use add_dependencies?
感谢您的帮助,
一个简单的解决方案是使用 if
来保护客户端和服务器CMake列表文件中的 add_subdirectory
调用。 目标是有条件的,即:
A simple solution is to guard the add_subdirectory
calls in both the client and the server CMake list file with an if
using a TARGET conditional, i.e.:
if (NOT TARGET plugin)
add_subdirectory("${CMAKE_SOURCE_DIR}/common/libplugin")
endif()
这可以防止多次添加 libplugin
子目录。
This prevents the libplugin
sub-directory from being added more than once.