在cmake中创建目标后,如何将更多源文件添加到目标 [英] How can I add more sources files to a target after the target has been created in cmake
问题描述
我使用以下示例来说明我的问题:假设现在我已经在CMake中使用以下命令创建了目标:
I use the following example to show my question: suppose now I have created a target using the following commands in CMake:
add_custom_target(MyProject SOURCES a.txt b.txt)
创建此目标后,将存储更多文件放置到目标上,然后使用以下命令:
After this target is created, more files are put to the target, and in order to do that I use the following command:
set(moreFiles c.txt d.txt e.txt)
set_target_properties(MyProject PROPERTIES SOURCES ${moreFiles})
但是VC向目标添加的文件仅为c.txt,其他文件(例如a.txt,b.txt,d.txt和e.txt)在目标中不可用。我有什么可以改善的吗?
However, in VC the added files to the target is only c.txt, other files such as a.txt,b.txt,d.txt and e.txt are not available in the target. Anything I can do for improvement?
推荐答案
对于具有属性的非平凡操作,最好放弃诸如 set_target_properties 之类的速记功能 code>并使用
set_property
的全部功能:
For non-trivial manipulations with properties, it's best to forego the shorthand functions like set_target_properties
and use the full power of set_property
:
set_property(TARGET MyProject APPEND PROPERTY SOURCES ${moreFiles})
如果出于某种原因想要继续使用 set_target_properties
,您必须先查询当前值,并引用整个结果:
If, for some reason, you'd want to keep using set_target_properties
for that, you'd have to query the current value first, and quote the entire result:
get_property(currentFiles TARGET MyProject PROPERTY SOURCES)
set_target_properties(MyProject PROPERTIES SOURCES "${currentFiles};${moreFiles}")
引号很重要,因为简写的 set _ * _ properties
命令期望其参数为 ...属性prop1 value1 prop2 value2
,即属性名称和值的交替列表。原始命令将属性 SOURCES
设置为 c.txt
,然后将属性 d设置为。 txt
转换为值 e.txt
。
The quoting is important, because the shorthand set_*_properties
commands expect their arguments to be ... PROPERTIES prop1 value1 prop2 value2
, that is, an alternating list of property names and values. Your original command set the property SOURCES
to c.txt
, and then the property d.txt
to the value e.txt
.
这篇关于在cmake中创建目标后,如何将更多源文件添加到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!