CMake:如何清除目标编译选项 [英] CMake: how to clear target compile options

查看:1157
本文介绍了CMake:如何清除目标编译选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我已全局使用 CMAKE_CXX_FLAGS 定义了一些常规编译选项。在我的主CMakeLists文件中,使用 add_compile_options()指定了一些通常用于所有目标的选项。

In my project I have defined some general compile options using CMAKE_CXX_FLAGS globally. Some other options, that in common case should be applied to all targets, are specified using add_compile_options() in my main CMakeLists file.

例如,我希望将标志 -Wconversion 应用于所有目标。
但是我有一个外部库,启用此选项会产生许多警告。因此,我只想对此特定的库禁用选项:

For example I want the flag -Wconversion be applied to all targets. But I have one external library that produces to many warnings with this option enabled. So I want to disable option only for this particular lib:

get_target_property(EXTLIB_COMPILE_FLAGS ext_lib COMPILE_OPTIONS )
list(REMOVE_ITEM EXTLIB_COMPILE_FLAGS -Wconversion)
set_target_properties(ext_lib PROPERTIES COMPILE_OPTIONS ${EXTLIB_COMPILE_FLAGS } )

但仅现在 -Wconversion 是使用 add_compile_options()设置的。
并且目标没有任何自己的附加标志。因此,从列表中删除唯一条目后,我将得到一个空列表。调用set_target_properties()失败,并出现错误:

But right now only -Wconversion was setted using add_compile_options(). And target does not have any own additional flags. So, after removing the only entry from the list I will get an empty list. Call to set_target_properties() fails with error:


set_target_properties调用,参数数量错误。

set_target_properties called with incorrect number of arguments.

是否可以完全清除某些目标属性?
我正在使用CMake 3.11

Is any way to clear some of target properties completly? I'm using CMake 3.11

推荐答案

将我的评论变成答案

只需添加引号:

set_target_properties(ext_lib PROPERTIES COMPILE_OPTIONS "${EXTLIB_COMPILE_FLAGS}")

现在-如果 EXTLIB_COMPILE_FLAGS 变量是空的-您最终拥有一个空字符串,而没有一个缺少参数。

Now - if EXTLIB_COMPILE_FLAGS variable is empty - you end-up having an empty string and just not an "missing argument".

这篇关于CMake:如何清除目标编译选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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