[CMake] target name collision

Angeliki Chrysochou angeliki.chrysochou at gmail.com
Thu Mar 27 11:20:14 EDT 2014


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/8a5e6246/attachment-0001.html>


More information about the CMake mailing list