将目标属性添加到CMake中的现有导入库中 [英] Add target properties to an existing imported library in CMake

查看:203
本文介绍了将目标属性添加到CMake中的现有导入库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

外部项目由CMake构建,并安装在通过 CMAKE_PREFIX_PATH 可见的目录中。由于是CMake项目,因此会安装正确的 .cmake 文件。在那些自动生成的文件中,创建了 EXPORTED 目标的文件,并设置了所有必需的属性。



我会做什么想要做的是-在不修改原始 CMakeLists.txt 的情况下-添加一个编译定义,为了正确包含该库中的标头,我需要此定义。



到目前为止,我已经尝试了两种方法:



重新添加库并正常指定定义

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

这是行不通的,因为目标的所有已设置属性(如include目录)都将被忽略。



只需添加定义

  target_compile_definitions(_external_lib_name_ INTERFACE FOO_BAR)

这一次CMake抱怨:

  CMake错误在foo.cmake:1(target_compile_definitions)处:
无法为该项目未构建的目标 _external_lib_name_指定编译定义。

当前我正在考虑代理目标:

  add_library(_proxy_target_ INTERFACE)
target_link_libraries(_proxy_target_ INTERFACE _external_lib_name_)
target_compile_definitions(_proxy_target_ INTERFACE FOO_BAR)$ b $ c

虽然这可能行得通,但是有人知道是否有更好的方法来修改导入的目标吗?



更新



使用Tsyvarev的答案,我可以使其正常工作,但是还有另一个问题:为了使目标正确运行修改后,我需要 include 一个文件,该文件首先 find_package 然后是 set_property 。如果我不使用 include ,而是标准的 CMakeLists.txt add_subdirectory 目标保留旧属性。

解决方案

命令 target_compile_definitions 使用 INTERFACE 关键字附加到属性 INTERFACE_COMPILE_DEFINITIONS ,但不适用于 IMPORTED 目标。您需要使用直接与目标属性配合使用的命令:

  set_property(TARGET _external_lib_name_ APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS FOO_BAR)


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.

解决方案

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)

这篇关于将目标属性添加到CMake中的现有导入库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆