为CMakeLists.txt之一禁用-Werror [英] Disable -Werror for one of CMakeLists.txt

查看:899
本文介绍了为CMakeLists.txt之一禁用-Werror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下CMake文件:

I have the following CMake file:

project(MyLib)
cmake_minimum_required(VERSION 2.8)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "release")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")
set(ROOT_DIR ${CMAKE_SOURCE_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ROOT_DIR}/bin/${CMAKE_BUILD_TYPE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ROOT_DIR}/bin/${CMAKE_BUILD_TYPE})
set(MAIN_LIBRARY_NAME "mylib")

add_subdirectory(src)
add_subdirectory(test_app)
add_subdirectory(test_app1) <--- I want to disable -Werror flag for CMakeLists.txt in this folder.
add_subdirectory(test_app2)

如何为子目录之一禁用-Werror标志?在每个子目录中,我也有CMakeLists.txt。

How to disable -Werror flag for one of subdirectories? In the each of sub directories I have CMakeLists.txt too.

推荐答案

将我的评论变成答案

在调用 add_subdirectory()时,所有变量和目录属性都会复制到子目录的上下文中。在此调用之前,可以使用以下命令修改 CMAKE_CXX_FLAGS

All variables and directory properties are copied to the subdirectory's context at the moment you call add_subdirectory(). Either modify CMAKE_CXX_FLAGS before this call with something like

string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_subdirectory(test_app1) 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")

或者使用旧方法,可以使用 add_definitions()给出编译器标志,并使用 remove_definitions()

Or use the "old way" were compiler flags could be given with add_definitions() and removed with remove_definitions()

add_definitions(-std=c++11 -Wall -Werror)
...
remove_definitions(-Werror)
add_subdirectory(test_app1)
add_definitions(-Werror)

但是,您可以-与我的第一个评论相反-仅在目标级别添加更改 add_compile_options()的标志 COMPILE_OPTIONS 属性,而不是完整目录。创建目标后,会将属性按原样从目录复制到目标,并且以后更改目录属性将不会自动更新目标的属性。

But you can - in contradiction to my first comment - change flags added add_compile_options() only on target level COMPILE_OPTIONS property and not for complete directories. When the target is created the property is copied as-is from the directory to the target and changing the directory property later won't automatically update the target's property.

如果具有较新的CMake版本,该版本支持 SOURCE_DIR 目标属性,您也可以通过一些疯狂的生成器表达式进行操作:

If you have a newer CMake version that supports SOURCE_DIR target property you can alternatively go by some crazy generator expression:

add_compile_options(
    -std=c++11 
    -Wall 
    $<$<NOT:$<STREQUAL:$<TARGET_PROPERTY:SOURCE_DIR>,${CMAKE_SOURCE_DIR}/test_app1>>:-Werror>
)

>参考

  • Is Cmake set variable recursive?

这篇关于为CMakeLists.txt之一禁用-Werror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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