C ++:在CMake中禁用旧样式转换警告

时间:2019-09-19 13:48:13

标签: c++ gcc cmake

我正在与一个旧的代码库进行交互,该代码库大量使用旧的样式转换(数千个)。我正在尝试在Cmake中禁用这些警告,但是它不起作用(我不想看到1000条警告,也不想解决根本原因)。为此,我假设我应该设置-Wno-old-style-cast。我尝试在CMakeLists.txt文件的3个不同位置执行此操作,但无济于事。这是完整的文件:

set(example "train")
project(${example} CXX)

# Python
message(STATUS "We are going to force it to use Python 3. If you change this, you will need to remove the build folder and reload the cmake project.")
find_package(PythonLibs 3 EXACT)
find_package(PythonInterp 3 EXACT)

# PCL library
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-old-style-cast")
set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} -Wno-old-style-cast")

add_executable(${example} src/${example}.cpp)
target_compile_options(${example} PUBLIC -Wno-old-style-cast)
target_link_libraries(${example} ${Boost_LIBRARIES} ${PCL_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(${example} PRIVATE include/ ${PYTHON_INCLUDE_DIRS})

我正在使用GCC 7.4.0在Ubuntu 18.04上进行编译。我看到成千上万个这样的错误:

/usr/include/python3.7m/object.h:118:49: warning: use of old-style cast [-Wold-style-cast]
 #define Py_TYPE(ob)             (((PyObject*)(ob))->ob_type)

编辑:

make VERBOSE=1的输出产生:

/usr/bin/c++  -DDISABLE_LIBUSB_1_0 -DDISABLE_PCAP -DDISABLE_PNG -DPCL_NO_PRECOMPILE=0 -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -Dqh_QHpointer -DvtkRenderingContext2D_AUTOINIT="1(vtkRenderingContextOpenGL2)" -DvtkRenderingCore_AUTOINIT="3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2)" -isystem /usr/include/vtk-7.1 -isystem /usr/include/freetype2 -isystem /usr/include/x86_64-linux-gnu -isystem /usr/local/include/pcl-1.9 -isystem /usr/include/eigen3 -I/home/bob/Desktop/choc/include -I/usr/include/python3.7m -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++  -Wno-old-style-cast    -Wno-old-style-cast -march=native -msse4.2 -mfpmath=sse -fPIC -std=gnu++14 -o CMakeFiles/train.dir/src/train.cpp.o -c /home/bob/Desktop/choc/src/train.cpp

1 个答案:

答案 0 :(得分:0)

一些 pcl 库使用诊断编译指示设置 -Wold-style-cast 警告。因此,无论您在 cmake 配置中设置什么,此警告都应该出现。在我的情况下,在包含 pcl 标头之前使用编译指示禁用它解决了这个问题。

#include <pcl/surface/concave_hull.h>

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif