cmake - 如何为不同的文件设置不同的警告级别?
问题描述
虽然我可以根据编译器设置不同的警告级别,例如:
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
我无法逐个文件设置它们。
在同一个目录中,我有一组名称在${SRC_WARN}
CMake 变量中的文件,与其他文件相比,它们需要不同的警告级别。
有没有办法用 指定这样的条件target_compile_options
?
解决方案
COMPILE_OPTIONS
您可以使用 为单个文件(或文件组)设置编译选项 ( ) set_source_files_properties()
。您可以通过添加到现有的 CMake 代码来更改源文件COMPILE_OPTIONS
:${SRC_WARN}
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
# Change these files to have warning level 2.
set_source_files_properties(${SRC_WARN} PROPERTIES COMPILE_OPTIONS /W2)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
# Change these files to inhibit all warnings.
set_source_files_properties(${SRC_WARN} PROPERTIES COMPILE_OPTIONS -w)
endif()
推荐阅读
- mysql - 唯一值不会丢失重复mysql的来源
- micronaut - 绑定可为空的参数
- php - 在 Mac 上运行带有 Cron 作业的 Laravel 5 调度程序会给我找不到类错误
- android-studio - 从 IBM Watson Assistant 获取我的聊天机器人中的选项列表
- javascript - 在json中查找具有相同值的键的总和
- android - 我无法在 android 的材质日历中添加更多图标
- html - 如果多行,如何为列元素设置错误消息
- angular - 更新我们的应用程序时如何更新缓存文件?
- laravel - Laravel 复选框问题,我无法更新数据透视表
- javascript - 在 react-native 中打开 Routes 中未指定的 DeepLink