问题
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})
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?
回答1:
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})
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}")
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
.
来源:https://stackoverflow.com/questions/32180851/how-can-i-add-more-sources-files-to-a-target-after-the-target-has-been-created-i