从cmake构建中检索目标属性 [英] Retrieve target properties from cmake build

查看:304
本文介绍了从cmake构建中检索目标属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个现有项目,它是由cmake配置的构建目录.只要知道目标名称,如何使用此构建检索目标的某些属性?我尝试创建一个像这样的单独脚本

Suppose I have an existing project and it's cmake-configured build directory. How can I retreive some target's properties using this build, provided that I know the target name? I tried creating a separate script like this

get_target_property(VAR target property)

但失败并出现错误

Command get_target_property() is not scriptable

还有其他方法吗?

推荐答案

显然,只有在使用cmake配置构建目录时,才能调用get_target_property().我不知道在已配置的构建目录上获取目标属性的任何方法.但是,如果可以修改现有的CMakeFiles.txt,则有一种解决方法.

Apparently get_target_property() can be called only when configuring a build directory with cmake. I am not aware of any method of getting targets properties on already configured build directory. But if modifying existing CMakeFiles.txt is an option, there is a workaround.

您可以尝试查找目标定义,在那里找到目标的属性并将其转储到文本文件中.然后,可以在构建目录配置完成后调用的任何其他脚本中使用该文件.

You can try locating target definition, get the target's properties there and dump them into a text file. Then, this file can be then used in any other scripts called after build directory configuration is done.

此示例说明了此解决方法:

This example illustrates this workaround:

add_executable(app ${app_sources}) 
set_target_properties(app PROPERTIES COMPILE_DEFINITIONS SOME_DEF=1)

get_target_property(compile_defs app COMPILE_DEFINITIONS)
file(WRITE app_compile_defs.txt ${compile_defs})

确保在完成CMakeFiles.txt中给定目标的每个属性更改后,使用get_target_property.否则,您可能会错过以下示例中的内容.

Be sure to use get_target_property after every property changes for given target in CMakeFiles.txt are done. Otherwise you can miss something as in example below.

add_executable(app ${app_sources}) 
set_target_properties(app PROPERTIES COMPILE_DEFINITIONS SOME_DEF=1)

get_target_property(compile_defs app COMPILE_DEFINITIONS)
file(WRITE app_compile_defs.txt ${compile_defs})

set_target_properties(app PROPERTIES COMPILE_DEFINITIONS ANOTHER_DEF=0)

在上面的示例中,ANOTHER_DEF=0定义不会在app_compile_defs.txt中列出.

In the example above, the ANOTHER_DEF=0 definition will not be listed in app_compile_defs.txt.

这篇关于从cmake构建中检索目标属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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