CMake - 删除单个翻译单元的编译标志 [英] CMake - remove a compile flag for a single translation unit

查看:338
本文介绍了CMake - 删除单个翻译单元的编译标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除单个翻译单元的set compile flag。有办法做到这一点吗? (例如使用 set_property ?)

I would like to remove a set compile flag for a single translation unit. Is there a way to do this? (e.g. using set_property?)

注意:compile-flag没有 -name 否定(无论什么原因)。

Note: the compile-flag has no -fno-name negation (for whatever reason).

我尝试过:

get_property(FLAGS TARGET target PROPERTY COMPILE_FLAGS)
string(REPLACE "-fname" "" FLAGS ${FLAGS})
set_property(TARGET target PROPERTY COMPILE_FLAGS ${FLAGS})

没有运气。我要删除的属性是 CMAKE_CXX_FLAGS 的一部分,因此这不起作用。

without any luck. The property I want to remove is part of CMAKE_CXX_FLAGS and thus this does not work.

推荐答案

A(不可扩展和非可移植)解决方案正在创建带有修改标志的自定义命令。我工作的最小示例如下:

A (non-scalable & non-portable) solution is creating a custom command with modified flags. A minimal example that I got working is the following:

cmake_minimum_required(VERSION 2.8)

project(test)
# Some flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
# Remove unwanted flag
string(REPLACE "-Wall" "" CUSTOM_FLAGS ${CMAKE_CXX_FLAGS})
# Split the flags into a cmake list (; separated)
separate_arguments(CUSTOM_FLAGS UNIX_COMMAND ${CUSTOM_FLAGS})
# Custom command to build your one file.
add_custom_command(
    OUTPUT out.o
    COMMAND ${CMAKE_CXX_COMPILER} 
    ARGS ${CUSTOM_FLAGS} -c ${CMAKE_SOURCE_DIR}/out.cpp 
                         -o ${CMAKE_BINARY_DIR}/out.o
    MAIN_DEPENDENCY out.cpp)

add_executable(test test.cpp out.o)

虽然远不完美,但它的工作是什么。 separate_arguments 是必要的,因为否则 CUSTOM_FLAGS 中的所有空格都被转义了(不知道如何快速解决) 。

While far from perfect, it does work which is what counts. The separate_arguments was necessary because otherwise all spaces in CUSTOM_FLAGS were escaped (don't know how to solve that one quickly).

这篇关于CMake - 删除单个翻译单元的编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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