【问题标题】:C++: Disable old style cast warnings in CMakeC++:在 CMake 中禁用旧式强制转换警告
【发布时间】:2020-01-20 13:53:33
【问题描述】:

我正在与一个旧代码库交互,该代码库广泛使用旧样式转换(数千个)。我试图在 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

【问题讨论】:

  • CMake 全局变量称为CMAKE_CXX_FLAGS
  • 我相信你想禁用警告,而不是强制转换。
  • @molbdnilo 你认为我想要什么标志?
  • 使用VERBOSE=1 选项运行make,并在出现警告时检查您的-Wno-old-style-cast 选项是否实际使用
  • 这些转换是在您的项目代码中还是在您正在使用的“外部”库中?如果是后者,您可以在执行target_include_directoriestarget_link_libraries 时添加SYSTEM,这将自动抑制该目标定义的包含目录中的标头中的所有警告

标签: c++ gcc cmake


【解决方案1】:

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

#include <pcl/surface/concave_hull.h>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    相关资源
    最近更新 更多