[CMake] Install libraries defined in INTERFACE targets

rozelak at volny.cz rozelak at volny.cz
Wed May 16 07:03:08 EDT 2018


Hallo,
I have created a simple project simulating the issue. 
 
There is 'run.sh' script which first builds an executable using 'add_subdirectory()' CMake command. It works without issues, as expected (the libraries required by Module1 and Module2 are passed to the Module4 builder).
Then, it first builds a STATIC library (defined by Module3) which is then used by Module4. This fails, since I do not know how to export a list of libraries required by Module1 and Module2.
 
Could someone, please, look at the project (which is really simple) and give me a hint how Module3 should export (when install iss called) a CMake script, which would containt the libraries to link with?
 
Thank you very much.
Best regards,
 
Dan
 
 
______________________________________________________________
> Od: <rozelak at volny.cz>
> Komu: <cmake at cmake.org>
> Datum: 04.05.2018 13:25
> Předmět: Install libraries defined in INTERFACE targets
>

 Hello,
 I build a project which consists of several "modules" with various mutual dependencies. Each module defines its OBJECT target as:
  
  
     # Module 1:
     add_library(Module1 OBJECT ....)
     target_include_directories(Module1 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
  
     add_library(Module1_LIBRARIES INTERFACE)
     target_link_libraries(Module1_LIBRARIES INTERFACE $<BUILD_INTERFACE:....>)
  
  
     # Module 2:
     add_library(Module2 OBJECT ....)
     target_include_directories(Module2 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
  
     add_library(Module2_LIBRARIES INTERFACE)
     target_link_libraries(Module2_LIBRARIES INTERFACE $<BUILD_INTERFACE:....> $<BUILD_INTERFACE:Module1_LIBRARIES>)
  
     add_library(Module2_INCLUDES  INTERFACE)
     target_include_directories(Module2_INCLUDES  INTERFACE $<TARGET_PROPERTY:Module2,INCLUDE_DIRECTORIES>)
     target_sources(Module2_INCLUDES  INTERFACE ....)
  
     add_library(Module_CONFFILE  INTERFACE)
     target_sources(Module2_CONFFILE  INTERFACE Module.config)
  
  
 And when building a final product (or one of the final products), I use the individual targets:
  
     add_library(Product1 STATIC $<TARGET_OBJECTS:Module1> $<TARGET_OBJECTS:Module2> ...)
     target_link_libraries(Product1 LINK_INTERFACE_LIBRARIES $<BUILD_INTERFACE:Module1_LIBRARIES> $<BUILD_INTERFACE:Module2_LIBRARIES> ...)
  
  
  
 It works perfectly, the library is built using all the sources. Even when defining INSTALL, all the properties of the targets (include files, config files, etc.) can be accessed and are installed correctly
  
     install(TARGETS Product1  DESTINATION "./" EXPORT "libProduct1")
     install(FILES $<TARGET_PROPERTY:Module2_CONFFILE,INTERFACE_SOURCES>          DESTINATION "./config")
     install(FILES $<TARGET_PROPERTY:Module2_INCLUDES,INTERFACE_SOURCES>          DESTINATION "./include")
  
     
 The problem starts when I want to export a CMake file listing all the libraries which were defined by the individual modules (and are accessed by $<BUILD_INTERFACE:Module2_LIBRARIES>) since they know what they need (how they were configured).
  
 When building a static library (which is shown here), I need to export a list of additional libraries necessary to link the Product1 correctly. But using $<BUILD_INTERFACE:Module[]_LIBRARIES> in the INSTALL() command does not work, neither works something like:
  
     install(EXPORT "llibProduct1" DESTINATION "." EXPORT_LINK_INTERFACE_LIBRARIES)
  
  
 So the question is: how to export the libraries defined by the individual modules to an CMake file which would be installed together with the Product1 library and header files?
  
  
 Thank you very much,
 Dan
  
 P.S. I use CMake 3.6.2.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CMake.tar.bz2
Type: application/x-bzip
Size: 2286 bytes
Desc: CMake.tar.bz2
URL: <https://cmake.org/pipermail/cmake/attachments/20180516/b6801261/attachment-0001.bin>


More information about the CMake mailing list