[CMake] import/export targets

Alexander Neundorf a.neundorf-work at gmx.net
Tue Feb 17 12:45:51 EST 2009


On Tuesday 17 February 2009, Micha Renner wrote:
> In a top-level CMakeLists I have the following lines
>
> PROJECT(BuildDLL14)
>
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> SET(CMAKE_MODULE_PATH "/usr/local/share/CMake")
> SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
>
> ADD_SUBDIRECTORY(DLL-1)
> ADD_SUBDIRECTORY(TestDLL)
> -------------------------------------------
> The CMakeLists.txt of DLL-1 looks like this:
>
> PROJECT(DLL-1)
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> SET(_targetname SLib1)
>
> INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/DLL-2)
>
> SET(_src cmFile.c)
> SET(_imIncludeFiles cmFile.h)
>
> ADD_LIBRARY(${_targetname} STATIC ${_src} ${_imIncludeFiles})
> INSTALL(TARGETS ${_targetname} DESTINATION lib EXPORT lib1-targets)
> INSTALL(EXPORT lib1-targets DESTINATION lib)
>
> The CMakeLists.txt of TestDLL looks like this:
> -------------------------------------------
> PROJECT(TestDLL14)
>
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
>
> INCLUDE(${CMAKE_INSTALL_PREFIX}/lib/lib1-targets.cmake)
>
> INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/DLL-1)
>
> SET(_src MRTestC.c)
> ADD_EXECUTABLE(TestDll14 ${_src})
> TARGET_LINK_LIBRARIES(TestDll14 _Lib1)
>
> ... and the result is:
> -------------------------------------------
> CMake Error at CMake/lib/lib1-targets.cmake:16 (ADD_LIBRARY):
> add_library cannot create imported target "SLib1" because another target
>   with the same name already exists.
> Call Stack (most recent call first):
>   TestDLL/CMakeLists.txt:10 (INCLUDE)
> -- Configuring incomplete, errors occurred!
>
> If one out-comment the ADD_SUBDIRECTORY(DLL-1) then it works.

Yes. The library is created in DLL-1, and then an additional target with the 
same name is created when you try to include the exported targets.
There are at least two thiongs you can do:
1) put the exported targets in a namespace using the "NAMESPACE" option of 
export, then they will all have the given namespace as prefix, and the names 
won't clash
2) test in a different way, i.e. make the test a separate build, and not part 
of the main project. I.e. "make test" would then run cmake on the test 
directory and build it separately. You can find examples for that e.g. in 
CMake/Tests/.

Alex


More information about the CMake mailing list