[CMake] object-libraries and compile-dependencies to other target

Patrick Boettcher patrick.boettcher at posteo.de
Thu Jul 21 05:30:48 EDT 2016


On Wed, 20 Jul 2016 13:49:52 -0400
Robert Maynard <robert.maynard at kitware.com> wrote:

> Hi Patrick,
> 
> Can you provide a simple example of what you are trying to do, and
> where it is failing?

-----
add_library(lib1 STATIC <files>)
target_include_directories(lib1 PUBLIC lib1-dir)
target_compile_features(lib1 PUBLIC cxx_nonstatic_member_init)
target_compile_definitions(lib1 PUBLIC FLAG)

* lib2 and lib3 like lib1 *

# runtime needs include-dirs of lib2 and lib3
add_library(runtime OBJECT file1.cpp)
target_link_libraries(runtime INTERFACE lib2 lib3) # fails

# so I manually add them: 

target_include_directories(runtime PRIVATE
   $<TARGET_PROPERTY:lib2,INTERFACE_INCLUDE_DIRECTORIES>)
# and the same for target_compile_features and
# the INTERFACE_COMPILE_FEATURES property - for all libraries.

add_executable(exe exe1.cpp exe2.cpp $<TARGET_OBJECTS:runtime>)
target_link_libraries(exe lib1 lib2 lib3)

-----

You might ask, why I need to make an object-library here. This is
because of a problem I encounter on some platforms where the linking
fails when runtime is a static library. In runtime there is a function
which is required by lib2, on the problematic platforms the only way to
be sure that the linker is not throwing away this function before it has
been referenced, is by passing it as .o-file to the link-line. 

regards,
--
Patrick.


More information about the CMake mailing list