CMAKE - 设置库的编译标志 [英] CMAKE - setting compile flags for libraries
问题描述
我有一个CMakeLists.txt脚本来编译我的库。
问题是我不能为库设置编译标志。
我试过了
SET(CMAKE_CXX_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_CXX_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_CXX_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
SET(CMAKE_C_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_C_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_C_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
SET(CMAKE_EXE_LINKER_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_EXE_LINKER_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_EXE_LINKER_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
SET(CMAKE_MODULE_LINKER_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_MODULE_LINKER_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_MODULE_LINKER_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
SET(CMAKE_SHARED_LINKER_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
SET(CMAKE_STATIC_LINKER_FLAGS/ W3 / nologo / EHsc)
SET(CMAKE_STATIC_LINKER_FLAGS_DEBUG/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG)
SET(CMAKE_STATIC_LINKER_FLAGS_RELEASE/ MT / O2 / Ob2 / DNDEBUG)
这些都没有效果...
我可以更改库的标志的唯一方法是这样。
SET_TARGET_PROPERTIES(MyLib PROPERTIES COMPILE_FLAGS / W3 / nologo / EHsc)
,这将更改Debug和Release
如何在世界上分别为使用CMake的库设置Release和Debug标志?
这些是您尝试在第一种方法中覆盖的全局缓存变量。
在本地更改这些编译器/链接器选项需要配置特定的目标属性或生成器表达式。因此,采取第二种方法和如何使用CMake在特定构建配置中为特定目标设置特定的编译器标志?我将获得:
target_compile_options(
MyLib PRIVATE
/ W3 / nologo / EHsc
$< $< CONFIG:Debug>:/ MTd / Od / Ob0 / Zi / RTC1 / DDEBUG / D_DEBUG>
$< $< CONFIG:Release>:/ MT / O2 / Ob2 / DNDEBUG>
)
或CMake版本< = 2.8.12(另请参阅 policy CMP0043 ):
set_target_properties (
MyLib PROPERTIES
COMPILE_FLAGS
/ W4 / nologo / EHsc
COMPILE_FLAGS_DEBUG
/ MTd / Od / Ob0 / Zi / RTC1
COMPILE_FLAGS_RELEASE
/ MT / O2 / Ob2
COMPILE_DEFINITIONS_DEBUG
DEBUG; _DEBUG
COMPILE_DEFINITIONS_RELEASE
NDEBUG
)
我个人喜欢OLD的做法更好,因为它替换了Visual Studio的项目属性中的默认选项。通过 target_compile_options()
传递的任何内容将以配置属性/ C / C ++ /命令行/附加选项
结尾。
一些背景信息为什么您的第一种方法不起作用:
generator在 CMakeLists.txt
文件结尾处为 CMAKE_< LANG> _FLAGS
设置任何设置,并将其应用于所有库和可执行目标在同一个 CMakeLists.txt
文件中作为默认编译器选项生成构建环境。
如果为您的 CMakeLists.txt
目标在子目录 CMakeLists.txt
中设置链接器变量,它将无法帮助(范围错误)。如果我把你上面的代码到主要的 CMakeLists.txt
它的确工作,我得到以下警告(因为你使用编译器选项的链接器):
1> LINK:warning LNK4044:unrecognized option'/ W3'; ignored
1> LINK:warning LNK4044:unrecognized option'/ EHsc'; ignored
1> LINK:warning LNK4044:unrecognized option'/ MTd'; ignored
1> LINK:warning LNK4044:无法识别的选项'/ Od'; ignored
1> LINK:warning LNK4044:unrecognized option'/ Ob0'; ignored
1> LINK:warning LNK4044:unrecognized option'/ Zi'; ignored
1> LINK:warning LNK4044:无法识别的选项'/ RTC1';忽略
1> LINK:warning LNK4044:无法识别的选项'/ DDEBUG'; ignored
1> LINK:warning LNK4044:unrecognized option'/ D_DEBUG';忽略
现在我们在编译器检测期间隐藏由CMake设置的同名缓存变量。详情请参阅:
I have a CMakeLists.txt script to compile my libraries. The problem is I cannot set the compile flags for the libraries.
I've tried
SET(CMAKE_CXX_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
SET(CMAKE_C_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_C_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_C_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
SET(CMAKE_EXE_LINKER_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
SET(CMAKE_MODULE_LINKER_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_MODULE_LINKER_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_MODULE_LINKER_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
SET(CMAKE_SHARED_LINKER_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
SET(CMAKE_STATIC_LINKER_FLAGS "/W3 /nologo /EHsc")
SET(CMAKE_STATIC_LINKER_FLAGS_DEBUG "/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG")
SET(CMAKE_STATIC_LINKER_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
None of those have effect...
The only way I can change flags for a library is like this.
SET_TARGET_PROPERTIES(MyLib PROPERTIES COMPILE_FLAGS "/W3 /nologo /EHsc")
and this will change the flags for both Debug and Release
How in the world can I set the Release and Debug flags separately for a library using CMake?
Those are globally cached variables you are trying overwrite in your first approach.
Changing those compiler/linker options locally you need config-specific target properties or generator expressions. So taking your second approach and How can I set specific compiler flags for a specific target in a specific build configuration using CMake? I would get:
target_compile_options(
MyLib PRIVATE
"/W3 /nologo /EHsc"
"$<$<CONFIG:Debug>:/MTd /Od /Ob0 /Zi /RTC1 /DDEBUG /D_DEBUG>"
"$<$<CONFIG:Release>:/MT /O2 /Ob2 /DNDEBUG>"
)
or for CMake versions <= 2.8.12 (see also policy CMP0043):
set_target_properties(
MyLib PROPERTIES
COMPILE_FLAGS
"/W4 /nologo /EHsc"
COMPILE_FLAGS_DEBUG
"/MTd /Od /Ob0 /Zi /RTC1"
COMPILE_FLAGS_RELEASE
"/MT /O2 /Ob2"
COMPILE_DEFINITIONS_DEBUG
"DEBUG;_DEBUG"
COMPILE_DEFINITIONS_RELEASE
"NDEBUG"
)
I personally like the "OLD" way of doing it better, because it replaces the default options in Visual Studio's project properties. Whatever is passed via target_compile_options()
will end up in Configuration Properties / C/C++ / Command Line / Additional Options
.
Some background information why your first approach didn't work:
CMake's generator takes whatever is set for CMAKE_<LANG>_FLAGS
at the end of any CMakeLists.txt
file and applies it to all library and executable targets in the same CMakeLists.txt
file as the default compiler options when generating the build environment.
If you set the linker variables for you main CMakeLists.txt
targets in subdirectories CMakeLists.txt
it won't help (wrong scope). If I take your above code into the main CMakeLists.txt
it does work and I get the following warnings (because you used compiler options for the linker):
1>LINK : warning LNK4044: unrecognized option '/W3'; ignored
1>LINK : warning LNK4044: unrecognized option '/EHsc'; ignored
1>LINK : warning LNK4044: unrecognized option '/MTd'; ignored
1>LINK : warning LNK4044: unrecognized option '/Od'; ignored
1>LINK : warning LNK4044: unrecognized option '/Ob0'; ignored
1>LINK : warning LNK4044: unrecognized option '/Zi'; ignored
1>LINK : warning LNK4044: unrecognized option '/RTC1'; ignored
1>LINK : warning LNK4044: unrecognized option '/DDEBUG'; ignored
1>LINK : warning LNK4044: unrecognized option '/D_DEBUG'; ignored
I'm now hiding the cached variables of the same name set by CMake during the compiler detection. For more details see:
- What's the CMake syntax to set and use variables?
- CMake: In which Order are Files parsed (Cache, Toolchain, …)?
这篇关于CMAKE - 设置库的编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!