Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake : multiple subprojects using the same static library

Tags:

static

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.

Let's say the client and the server need libstuff.

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. 

So i'm kind of new w/ cmake and i'm not sure what i should do, should i use add_dependencies?

Thanks for your help,

like image 758
Intrepidd Avatar asked Dec 09 '11 00:12

Intrepidd


1 Answers

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()  

This prevents the libplugin sub-directory from being added more than once.

like image 103
sakra Avatar answered Sep 29 '22 05:09

sakra