CMake:使用同一静态库的多个子项目 [英] CMake : multiple subprojects using the same static library

查看:294
本文介绍了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.

这篇关于CMake:使用同一静态库的多个子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆