[CMake] adding same subdirectory multiple times.

Michael Wild themiwi at gmail.com
Thu Mar 11 03:56:42 EST 2010


On 11. Mar, 2010, at 9:41 , Mika.Rajala at patria.fi wrote:

> 
> 
> cmake-bounces at cmake.org wrote on 11.03.2010 10:37:47:
> 
>> 
>> On 11. Mar, 2010, at 9:27 , Mika.Rajala at patria.fi wrote:
>> 
>>> 
>>> 
>>> cmake-bounces at cmake.org wrote on 11.03.2010 10:04:42:
>>> 
>>>> On 11.03.10 09:56:35, Mika.Rajala at patria.fi wrote:
>>>>> 
>>>>> Hi
>>>>> 
>>>>> The problem:
>>>>> 
>>>>>> From a shared CMakeLists.txt i get the following error when defining
> a
>>>>> library target.
>>>>> Policy CMP0002 is not set: Logival target names must be globally
>>> unique.
>>>>> 
>>>>> 
>>>>> The situation:
>>>>> 
>>>>> Let's assume i have the following cmake files in the following
> folders
>>> with
>>>>> the svn:externals properties.
>>>>> 
>>>>> C:\project\CMakeLists.txt
>>>>> add_subdirectory(ProjectA)
>>>>> add_subdirectory(ProjectB)
>>>>> 
>>>>> C:\project\ProjectA   (svn:externals LibraryA <svn_path_to_libA>)
>>>>> add_subdirectory(LibraryA)
>>>>> 
>>>>> c:\project\ProjectB    (svn:externals LibraryA <svn_path_to_libA>)
>>>>> add_subdirectory(LibraryA)
>>>>> 
>>>>> c:\project\LibraryA
>>>>> add_library(LibraryA)
>>>>> 
>>>>> Thoughts:
>>>>> 
>>>>> I don't know if this causes any problem, I tried reading some
>>>>> documentations but I couldn't figure it out.
>>>> 
>>>> Yes it does. The library target is defined multiple times, thats not
>>>> possible. Each version of "LibraryA/CMakeLists.txt" has to use its own
>>>> name for LibraryA if you want that directory layout to work. Target
>>>> names must be globally unique as the error message says.
>>>> 
>>>> From a quick glance I see two options:
>>>> - drop the externals and let the projects depend on a "LibraryA"
> target
>>>> being defined (you need to use the add_subdirectory call for it to
> the
>>>> fron then
>>>> - find a way to test in LibraryA/CMakeLists.txt wether its under
>>>> ProjectA, ProjectB or project and adjust the target name then.
>>>> 
>>>> Andreas
>>>> 
>>>> --
>>>> Questionable day.
>>>> 
>>>> Ask somebody something.
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>> 
>>>> Visit other Kitware open-source projects at http://www.kitware.
>>>> com/opensource/opensource.html
>>>> 
>>>> Please keep messages on-topic and check the CMake FAQ at: http://www.
>>>> cmake.org/Wiki/CMake_FAQ
>>>> 
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.cmake.org/mailman/listinfo/cmake
>>> 
>>> Thanks.
>>> 
>>> I'd still like to know if there is a way to handle this like including
> c++
>>> header files
>>> 
>>> Something like
>>> 
>>> if(LibraryA)
>>>     message(Already included, doing nothing)
>>> else()
>>>     <Normal CMakeLists content>
>>> endif()
>>> 
>>> -mika
>> 
>> if(TARGET LibraryA)
>>  message("Already included, doing nothing")
>>  return()
>> endif()
>> ...
>> 
>> 
>> You could also use directory properties to set "include-guards".
>> 
>> HTH
>> 
>> Michael
>> 
>> 
>> _______________________________________________
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at http://www.kitware.
>> com/opensource/opensource.html
>> 
>> Please keep messages on-topic and check the CMake FAQ at: http://www.
>> cmake.org/Wiki/CMake_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
> 
> I tried the variable thing, i worked, kinda..
> 
> But how do i reset the variable if the CMakeLists.txt has changed.
> 
> Now it's a little too persistent, since changes are not handled.

Hu? What do you mean by "variable"? The

if(TARGET LibraryA)

code checks whether there exists a target called LibraryA (either through ADD_LIBRARY, ADD_EXECUTABLE or ADD_CUSTOM_TARGET). No variable involved, just the target name. I was thinking like this:

if(NOT TARGET LibraryA)
  add_subdirectory(LibraryA)
endif()

Michael


More information about the CMake mailing list