Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add target properties to an existing imported library in CMake

Tags:

cmake

External project is built by CMake and installed in a directory visible via CMAKE_PREFIX_PATH. As it's CMake project, it installs proper .cmake files. In those, automatically generated, files an EXPORTED target is created and all required properties are set.

What I would like to do, is - without modification of original CMakeLists.txt - add a compile definition, that I need in order to properly include header from this library.

So far I've tried two approaches:

Re-add library and specify definitions normally

add_library(_external_lib_name_ INTERFACE IMPORTED)
target_compile_definitions(_external_lib_name_ INTERFACE FOO_BAR)

This doesn't work, as all the already set properties of the target (like include directories) are ignored.

Just add definitions

target_compile_definitions(_external_lib_name_ INTERFACE FOO_BAR)

This time CMake complains:

CMake Error at foo.cmake:1 (target_compile_definitions):
  Cannot specify compile definitions for target "_external_lib_name_" which is not built by this project.

Currently I am thinking about a proxy target:

add_library(_proxy_target_ INTERFACE)
target_link_libraries(_proxy_target_ INTERFACE _external_lib_name_)
target_compile_definitions(_proxy_target_ INTERFACE FOO_BAR)

While this one might work, does anyone know if there is a better way of modifying imported targets?

Update:

Using Tsyvarev's answer I was able to make it work, but there is another issue: in order for the target to be properly modified, I need to include a file that first find_package and later set_property. If I don't use include, but standard CMakeLists.txt and a add_subdirectory the target holds old properties.

like image 645
Red XIII Avatar asked Nov 22 '25 12:11

Red XIII


1 Answers

Command target_compile_definitions with INTERFACE keyword appends to property INTERFACE_COMPILE_DEFINITIONS but doesn't work with IMPORTED targets. You need to use a command which directly works with the target properties:

set_property(TARGET _external_lib_name_ APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS FOO_BAR)
like image 105
Tsyvarev Avatar answered Nov 25 '25 11:11

Tsyvarev