[CMake] target name collision

Aurelien Richez aurelien.richez at inria.fr
Thu Mar 27 11:42:39 EDT 2014


Thanks Angeliki. I will try to do something like this. In fact I forgot to mention that my target which collide comes from add_custom target() but I can apply the same principle. 

To be more precise about the context, my projects have a target "unit_test" which is run tests each time I build (included in all) and a target "doc" to generate the documentation. 

To answer to Jakub : 

There are other subprojects which are modules needing my independent projects. like this: 

Project 
|--subrojectWithTargetCollision1 
|--subrojectWithTargetCollision2 
|--someOtherProject1 
`--someOtherProject2 

My independent projects are both under development and not stable yet.So, having a code repository containing both project is a convenient way to work with a fixed version (which will ensure that every project compile) and update when I want to. 

May be add_subdirectory is not the way to go but it was handy to compile every project at once and link my programs with libraries in the project, without installing them (for testing purpose). 

Thanks for your answers. 
Regards, 

Aurélien 

----- Mail original -----

> De: "Angeliki Chrysochou" <angeliki.chrysochou at gmail.com>
> À: "Jakub Zakrzewski" <jzakrzewski at e2e.ch>
> Cc: "Aurelien Richez" <aurelien.richez at inria.fr>, cmake at cmake.org
> Envoyé: Jeudi 27 Mars 2014 16:20:14
> Objet: Re: [CMake] target name collision

> Hi Aurelien,

> Even though it is unsafe and not recommended, you could set policy CMP0002 to
> old so that cmake silently accepts non-unique target names like this:

> cmake_policy(SET CMP0002 OLD)

> I can't guarantee that your build will then work however.

> Another option is to write your own wrapper to include the directory name
> using a macro or a function providing the name of the root project added by
> cmake using the variable CMAKE_PROJECT_NAME. Here is an example wrapper of
> add_library (I used "_" as a delimiter instead of "/"):

> macro( my_add_library LIB_NAME LIB_TYPE SRC_FILES INC_FILES )
> add_library( ${CMAKE_PROJECT_NAME}_${LIB_NAME} ${LIB_TYPE} ${SRC_FILES}
> ${INC_FILES} )
> endmacro()

> Invoking it for a project with name ProjectA to create a shared library LibA
> would look like this, for example:

> my_add_library(LibA SHARED src.cpp inc.h)

> and would result to library name ProjectA_LibA.

> A third solution is to set the target properties after adding the target.
> Here is an example for shared libraries in case you want to preserve the
> original prefix too:

> set_target_properties(LibA PROPERTIES PREFIX
> "${CMAKE_SHARED_LIBRARY_PREFIX}${CMAKE_PROJECT_NAME}_" )

> I hope it helps!

> Best regards,
> Angeliki

> On Thu, Mar 27, 2014 at 3:30 PM, Jakub Zakrzewski < jzakrzewski at e2e.ch >
> wrote:

> > Is there a reason, why you treat your independent projects with
> > add_directory() instead of making them external projects?
> 

> > --
> 

> > Gruesse,
> 

> > Jakub
> 

> > From: CMake [mailto: cmake-bounces at cmake.org ] On Behalf Of Aurelien Richez
> 
> > Sent: Donnerstag, 27. März 2014 14:58
> 
> > To: cmake at cmake.org
> 
> > Subject: [CMake] target name collision
> 

> > Hi,
> 

> > I have two project that are independent and can be compiled on their own. I
> > also have a bigger project which include these two project with
> > add_subdirectory. However, these projects contain targets with the same
> > names. As a consequence, cmake 2.8 complains with a message such as
> > "another
> > target with the same name already exists" when I try to generate the
> > makefile.
> 

> > Isn't there a way to handle this situation which does not involve renaming
> > the targets in the subproject ? Like an option to automatically add a
> > prefix
> > to target added from add_subdirectory (for instance, target "doc" in
> > "subproject1" would become "subproject1/doc" in the upper project).
> 

> > Thanks,
> 

> > Aurélien Richez
> 

> > --
> 

> > Powered by www.kitware.com
> 

> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> 

> > Kitware offers various services to support the CMake community. For more
> > information on each offering, please visit:
> 

> > CMake Support: http://cmake.org/cmake/help/support.html
> 
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> 
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> 

> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> 

> > Follow this link to subscribe/unsubscribe:
> 
> > http://www.cmake.org/mailman/listinfo/cmake
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20140327/8c9bfc97/attachment.html>


More information about the CMake mailing list