在CMake中添加全局编译标志 [英] Adding global compile flags in CMake
问题描述
我正在编辑别人制作的CMakeLists.txt文件。我试图摆脱编译项目时生成的一些警告。
I am editing a CMakeLists.txt file made by someone else. I'm trying to get rid of some of the warnings generated when compiling the project.
通常我只添加 set(CMAKE_CXX_FLAGS $ {CMAKE_CXX_FLAGS } $ {MY_FLAGS})
加上我需要添加的任何标志,效果很好,但是对于此项目,它只是无法正常工作。警告仍然出现。我尝试了几种其他方法,但是什么也没做。
Normally I just add set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}")
with whatever flags I need to add, and it works fine, but for this project, it's just not working. The warnings still appear. I tried a couple alternative methods, but nothing.
是什么引起了问题?
cmake_minimum_required(VERSION 3.1)
project(PBS)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(LIBIGL_USE_STATIC_LIBRARY "Use libigl as static library" OFF)
option(LIBIGL_WITH_ANTTWEAKBAR "Use AntTweakBar" OFF)
option(LIBIGL_WITH_CGAL "Use CGAL" OFF)
option(LIBIGL_WITH_COMISO "Use CoMiso" OFF)
option(LIBIGL_WITH_CORK "Use Cork" OFF)
option(LIBIGL_WITH_EMBREE "Use Embree" OFF)
option(LIBIGL_WITH_LIM "Use LIM" OFF)
option(LIBIGL_WITH_MATLAB "Use Matlab" OFF)
option(LIBIGL_WITH_MOSEK "Use MOSEK" OFF)
option(LIBIGL_WITH_OPENGL "Use OpenGL" ON)
option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON)
option(LIBIGL_WITH_PNG "Use PNG" OFF)
option(LIBIGL_WITH_PYTHON "Use Python" OFF)
option(LIBIGL_WITH_TETGEN "Use Tetgen" OFF)
option(LIBIGL_WITH_TRIANGLE "Use Triangle" OFF)
option(LIBIGL_WITH_VIEWER "Use OpenGL viewer" ON)
option(LIBIGL_WITH_XML "Use XML" OFF)
if (NOT LIBIGL_FOUND)
find_package(LIBIGL REQUIRED QUIET)
endif()
add_subdirectory(0_dummy)
add_subdirectory(1_cannonball)
add_subdirectory(2_spring)
add_subdirectory(3_spinning)
add_subdirectory(4_gyro)
# Custom commands
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
add_compile_options ( -Wno-reorder )
add_definitions ( -Wno-unknown-pragmas )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")
编辑:
我发现在其中之一中添加标志子目录适用于该子目录(例如,在 3_spinning / CMakeLists.txt
中)。没有办法全局设置标志吗?
I found out that adding the flags in one of the subdirectories works for that subdirectory (e.g. in 3_spinning/CMakeLists.txt
). Is there no way of setting the flags globally?
推荐答案
结论: add_compile_options
和 add_definitions
适用于当前目录以及该命令后包含的所有包含的目录 。但是,设置 CMAKE_CXX_FLAGS
似乎仅适用于当前目录。但是不知道为什么,因为正如评论员Tsyvarev所说,它应该与前两种方法具有相同的作用域。
Conclusion: add_compile_options
and add_definitions
work on the current directory and all included directories that are included after the command. Setting CMAKE_CXX_FLAGS
, however, seems to only work on the current directory. Not sure why however, because as commenter Tsyvarev says, it should have the same scope as the first two methods.
基本上,像这样移动行:
Basically, shifting the lines around like this:
[...]
# Custom commands
add_compile_options ( -Wno-reorder )
add_definitions ( -Wno-unknown-pragmas )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")
add_subdirectory(0_dummy)
add_subdirectory(1_cannonball)
add_subdirectory(2_spring)
add_subdirectory(3_spinning)
add_subdirectory(4_gyro)
我不再收到 -Wreorder
和 -Wunknown-pragmas
的警告,但仍然得到- Wsign-compare
警告。
I no longer get -Wreorder
and -Wunknown-pragmas
warnings, but I still get -Wsign-compare
warnings.
这篇关于在CMake中添加全局编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!