Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify imported dependencies of an OBJECT library?

Tags:

cmake

I have an OBJECT library objlib which is linked into the main target maintarget. The objlib has a dependent library, say, ZLIB. If we're using the legacy <package-name>_* variables then it's easy:

add_library(objlib OBJECT ...) target_include_directories(objlib ${ZLIB_INCLUDE_DIRS}) ... add_executable(maintarget $<TARGET_OBJECTS:objlib>) target_link_libraries(maintarget ${ZLIB_LIBRARIES}) 

But I want to use the dependency as an IMPORTED library because it's more concise (and the convenient way to create config modules, that is, using install(EXPORT ...), does just that).

The following code does not work because target_link_libraries cannot be used with an OBJECT library:

add_library(objlib OBJECT ...) target_link_libraries(objlib ZLIB::ZLIB) 

Linking ZLIB::ZLIB to maintarget does not work either, objlib does not get the include directories:

add_library(objlib OBJECT ...) ... add_executable(maintarget $<TARGET_OBJECTS:objlib>) target_link_libraries(maintarget ZLIB::ZLIB) 

Hacking with an intermediate INTERFACE library (objlib-wrapper) does not work either.

The only thing that works is to query the IMPORTED library's properties and regenerate the information normally available in the <package-name>_* variables. Which is a nasty workaround.

Is there a better way?

like image 541
tamas.kenez Avatar asked Jul 08 '15 10:07

tamas.kenez


People also ask

What does target_ LINK_ LIBRARIES do?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

What is Add_library in CMake?

add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [<source>...]) Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project.

What is a CMake Object library?

13 February, 2020. CMake Object Libraries can be used to keep build directories less cluttered and speed up the build process. The traditional workflow for Makefile was to create lots of object files (targets) with the relevant compile options, definitions and link flags.


1 Answers

As of CMake 3.12, you can now use target_link_libraries on object libraries to get usage requirements.

Using 3.12, this approach that you mentioned should work:

add_library(objlib OBJECT ...) target_link_libraries(objlib ZLIB::ZLIB) 
like image 68
ian5v Avatar answered Sep 23 '22 11:09

ian5v