Add target properties to an existing imported library in CMake

[亡魂溺海] 提交于 2020-03-16 06:45:32

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/55081746/add-target-properties-to-an-existing-imported-library-in-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!