不同目标中相同文件的不同编译标志 [英] Different compile flags for same file in different targets
问题描述
我想在两个不同的目标中包含一个.cpp文件(在运行CMake后变成两个VS项目)。我想为这些项目设置不同的COMPILE_FLAGS。
I would like to include a .cpp-file in two different targets (becoming two VS projects after running CMake). I would like to set different COMPILE_FLAGS for these projects.
但是,当我做
SET_TARGET_PROPERTIES(myfile.cpp PROPERTIES COMPILE_FLAGS "flags1")
ADD_EXECUTABLE(project1 myfile.cpp)
SET_TARGET_PROPERTIES(myfile.cpp PROPERTIES COMPILE_FLAGS "flags2")
ADD_EXECUTABLE(project2 myfile.cpp)
flags2适用于这两个项目,因此看起来属性被覆盖第3行,不在第2行。这是真的还是我错过了什么?有办法解决这个问题吗?
the "flags2" applies for both projects, so it seems like the properties are overwritten in line 3 and not considered in line 2. Is this true or am I missing something? Is there a way to solve this?
谢谢!
推荐答案
应用 set_target_properties
命令到项目,而不是源文件:
Apply the set_target_properties
command to the projects and not to the source files:
add_executable(project1 myfile.cpp)
set_target_properties(project1 PROPERTIES COMPILE_FLAGS "flags1")
add_executable(project2 myfile.cpp)
set_target_properties(project2 PROPERTIES COMPILE_FLAGS "flags2")
目标上设置的标志将应用于目标中的所有源。
The flags set on the target will apply to all sources within the target.
这篇关于不同目标中相同文件的不同编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!