C++: Disable old style cast warnings in CMake - c++

I am interfacing with an old code base which extensively uses old style casts (thousands). I am trying to disable these warnings in Cmake, but it just isn't working (I do not want to see 1000s of warnings, nor do I care to fix the root cause). To do this, I assume that I should be setting -Wno-old-style-cast. I tried doing this in 3 separate places in the CMakeLists.txt file, but to no avail. Here is the complete file:
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})
I am compiling on Ubuntu 18.04 with GCC 7.4.0. I see thousands of errors like this:
/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)
EDIT:
The output of make VERBOSE=1 yields:
/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

Some libraries of pcl set -Wold-style-cast warnings using Diagnostic Pragmas. For this reason this warnings should appear whatever you set in you cmake configuration. Disabling it using pragmas before of including pcl headers solved the problem in my case.
#include <pcl/surface/concave_hull.h>
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif

Related

Building/Linking against Keg (/usr/local/opt) when normal brew package exists in /usr/local

[MacOS Mojave 10.14.5, Xcode 10.2, ROS Kinetic, Qt 5.14.1]
I'm trying to build ROS Kinetic on my Macbook Pro. Certain modules within Kinetic utilise Qt, and I'm having a compilation problem because I have multiple versions of Qt on my system.
I have Qt v5.14.1, a Keg formula which is addressed(linked) through /usr/local/opt/qt, and Qt v4.8.7_6 which is addressed(linked) through /usr/local. I don't want to unlink/uninstall either.
The particular package I'm having difficulties with is qt_gui_cpp which uses CMakeLists.txt files. (Though the package is actually a catkin package, I haven't had any issues using CMake functionality in the make files.)
I'm using Apple's Clang++ compiler, and I think that, based on the types of errors being reported, the compiler is finding the wrong include folder even though the paths in the compiler's error messages is correct for the version I'm using, under the hood. I've tried using set(CMAKE_IGNORE_PATH /usr/local/include) but this hasn't helped.
The actual compiler command that is issued is below. Note that -I/usr/local/include precedes the Qt v5.14.1 include, thus the compiler only looks into the Qt v5.14.1 folder when it can't find it under /usr/local/include:
cd /Users/user/ros/ros_catkin_ws/build_isolated/qt_gui_cpp/src/qt_gui_cpp && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-DQT_CORE_LIB
-DQT_GUI_LIB
-DQT_NO_DEBUG
-DQT_WIDGETS_LIB
-DROS_BUILD_SHARED_LIBS=1
-DROS_PACKAGE_NAME=\"qt_gui_cpp\"
-Dqt_gui_cpp_EXPORTS
-I/Users/user/ros/ros_catkin_ws/src/qt_gui_core/qt_gui_cpp/include
-I/Users/user/ros/ros_catkin_ws/install_isolated/include
-I/usr/local/include
-I/Users/user/ros/ros_catkin_ws/src/qt_gui_core/qt_gui_cpp/src/qt_gui_cpp/qt_gui_cpp
-isystem /usr/local/Cellar/qt/5.14.1/lib/QtCore.framework/Headers
-isystem /usr/local/Cellar/python#2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/include/python2.7
-isystem /Users/user/ros/ros_catkin_ws/src/qt_gui_core/qt_gui_cpp/include/qt_gui_cpp
-isystem /Users/user/ros/ros_catkin_ws/src/qt_gui_core/qt_gui_cpp/src/qt_gui_cpp
-iframework /usr/local/Cellar/qt/5.14.1/lib
-isystem /usr/local/Cellar/qt/5.14.1/lib/QtWidgets.framework/Headers
-isystem /usr/local/Cellar/qt/5.14.1/lib/QtGui.framework/Headers
-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers
-isystem /usr/local/Cellar/qt/5.14.1/./mkspecs/macx-clang
-I/usr/local/opt/qt/include
-L/usr/local/opt/qt/lib
-fPIC
-O3
-DNDEBUG
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
-fPIC
-fPIC
-std=gnu++11
-o CMakeFiles/qt_gui_cpp.dir/composite_plugin_provider.cpp.o
-c /Users/user/ros/ros_catkin_ws/src/qt_gui_core/qt_gui_cpp/src/qt_gui_cpp/composite_plugin_provider.cpp
How can I resolve this problem without breaking other things?
(More info if needed.)

How can I find out which module is adding a given compile flag in Cmake?

I'm facing the following problem: I have a CMake project with a few targets and a multitude of dependencies added via find_package().
I am using set(CMAKE_CXX_STANDARD 11) to compile with C++11, nevertheless somehow, the -std=gnu-14 flag makes it into the command line, which I can see when compiling with CMAKE_VERBOSE_MAKEFILE=1.
/usr/local/opt/ccache/libexec/c++ -DCORE_DEBUG -DDISABLE_LIBUSB_1_0 -DDISABLE_PCAP
-DDISABLE_PNG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB
-DvtkRenderingContext2D_AUTOINIT="1(vtkRenderingContextOpenGL2)"
-DvtkRenderingCore_AUTOINIT="3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2)"
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include
-Isrc -Ibuild -I/usr/local/include/spinnaker -Ibuild/gtest/src/gtest/googletest/include
-Ibuild/gtest/src/gtest/googlemock/include -isystem /usr/local/include/eigen3 -isystem
/usr/local/Cellar/vtk/8.2.0_1/include/vtk-8.2 -isystem /usr/local/include -isystem
/usr/local/include/pcl-1.9 -isystem /usr/local/Cellar/eigen/3.3.7/include/eigen3
-isystem /Users/ -isystem /usr/local/Cellar/opencv/4.1.0_2/include/opencv4 -isystem
libs/tensorflow/include -isystem /usr/local/Cellar/zeromq/4.3.2/include -iframework
/usr/local/opt/qt/lib -isystem /usr/local/opt/qt/lib/QtWidgets.framework/Headers
-isystem /usr/local/opt/qt/lib/QtGui.framework/Headers -isystem
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers
-isystem /usr/local/opt/qt/lib/QtCore.framework/Headers -isystem
/usr/local/opt/qt/./mkspecs/macx-clang -DPCL_ONLY_CORE_POINT_TYPES=ON
-DNO_EXPLICIT_INSTANTIATIONS -g -g -O0 -isysroot
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -Wno-deprecated-declarations
-Wno-unused-parameter -Wno-sign-compare -std=gnu-c++11 -Wno-unused-private-field -fPIC
-std=gnu++14 -o CMakeFiles/…file1.cpp.o -c src/…file1.cpp
# ^see here
I would like to know where it comes form, as I am not setting it myself. I suspect OpenCV 4.1 and PCL 1.9, but the *_DEFINITIONS properties of the two packages do not include it.
How can I find out who is adding that option and how can I force my own options to be added at the end (regardless of whether that is advisable)?

Replace -fPIC with -fpic

When using Qt CMake automatically adds the -fPIC flag to compile options. I want to use -fpic, so I went through all Cmake variables and replaced -fPIC with -fpic.
cmake_minimum_required(VERSION 3.5)
project(sss)
find_package(Qt5 REQUIRED COMPONENTS Core Sql)
get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
if (NOT "${${_variableName}}" STREQUAL "")
string(REPLACE "-fPIC" "-fpic" ${_variableName} ${${_variableName}})
string(REPLACE "-fPIE" "-fpie" ${_variableName} ${${_variableName}})
endif()
#message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
set(CMAKE_CXX_FLAGS "-fpie")
set(CMAKE_EXE_LINKER_FLAGS "-fpie -pie")
add_executable(sss main.cpp)
target_link_libraries(sss Qt5::Core Qt5::Sql)
main.cpp contains
#include <QSqlDatabase>
int main(){
QSqlDatabase::addDatabase("QPSQL");
}
Unfortunately CMake still adds the -fPIC flag, althoguh the listed variables does not contain it:
Building CXX object CMakeFiles/sss.dir/main.cpp.o
/usr/bin/c++ -DQT_CORE_LIB -DQT_NO_DEBUG -DQT_SQL_LIB -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -isystem /usr/include/x86_64-linux-gnu/qt5/QtSql -fPIC -o CMakeFiles/sss.dir/main.cpp.o -c src/main.cpp
How can I replace -fPIC with -fpic?
Turning my comment into an answer
Your code overwrites CMake global variables like CMAKE_CXX_COMPILE_OPTIONS_PIC or CMAKE_CXX_COMPILE_OPTIONS_PIE.
But Qt brings its own -fPIC option through target properties. The Qt5::Core target does have INTERFACE_COMPILE_OPTIONS set to -fPIC (see e.g. here).
Try overwriting the target properties by adding
set_property(TARGET Qt5::Core PROPERTY INTERFACE_COMPILE_OPTIONS "-fpic")
after your find_package(Qt5 ...) call.

homebrew list of available compiler

Hello I need to compile a Qt project using openmp (built using cmake).
Up to now I used gcc-5 provided by homebrew but I would like to use clang (my taste...)
I installed clang-omp via homebrew install clang-omp but now when I try to compile my project (generated via cmake -DCMAKE_CXX_COMPILER=/usr/local/bin/clang-omp++ ..), I get this error:
cd /Users/tommaso/neutrino/debug/src && /usr/local/bin/clang-omp++ -DHAVE_HDF5 -DHAVE_JPEG -DHAVE_LIBCFITSIO -DHAVE_LIBCLFFT -DHAVE_LIBDF -DHAVE_LIBFFTW -DHAVE_LIBFFTW_THREADS -DHAVE_LIBGSL -DHAVE_LIBGSLCBLAS -DHAVE_LIBHDF5HL -DHAVE_LIBMFHDF -DHAVE_LIBNETPBM -DHAVE_LIBTIFF -DHAVE_NPHYSIMAGE -DHAVE_OPENCL -DHAVE_PYTHONQT -DQT_CORE_LIB -DQT_GUI_LIB -DQT_MULTIMEDIAWIDGETS_LIB -DQT_MULTIMEDIA_LIB -DQT_NETWORK_LIB -DQT_PRINTSUPPORT_LIB -DQT_SQL_LIB -DQT_SVG_LIB -DQT_UIPLUGIN_LIB -DQT_UITOOLS_LIB -DQT_WIDGETS_LIB -DUSE_QT5 -D__VER=\"osx-cmake-v2.0-75.bdff6e3\" -I/Users/tommaso/neutrino/debug/src -I/Users/tommaso/neutrino/src -I/usr/include/hdf -I/usr/local/include/netpbm -I/Library/Frameworks/qwt.framework/Headers -I/usr/local/Cellar/hdf5/1.8.16_1/include -I/usr/include/python2.7 -I/Users/tommaso/neutrino/src/../../pythonqt-code/src -I/Users/tommaso/neutrino/src/../../pythonqt-code/src/gui -I/Users/tommaso/pythonqt-code/src -I/Users/tommaso/pythonqt-code/extensions/PythonQt_QtAll -I/Users/tommaso/neutrino/src/python -iframework /usr/local/opt/qt5/lib -isystem /usr/local/opt/qt5/lib/QtCore.framework/Headers -isystem /usr/local/opt/qt5/./mkspecs/macx-clang -isystem /usr/local/opt/qt5/lib/QtGui.framework/Headers -isystem /System/Library/Frameworks/OpenGL.framework/Headers -isystem /usr/local/opt/qt5/lib/QtSql.framework/Headers -isystem /usr/local/opt/qt5/lib/QtWidgets.framework/Headers -isystem /usr/local/opt/qt5/lib/QtSvg.framework/Headers -isystem /usr/local/opt/qt5/lib/QtPrintSupport.framework/Headers -isystem /usr/local/opt/qt5/include -isystem /usr/local/opt/qt5/include/QtUiTools -isystem /usr/local/opt/qt5/lib/QtMultimedia.framework/Headers -isystem /usr/local/opt/qt5/lib/QtNetwork.framework/Headers -isystem /usr/local/opt/qt5/lib/QtMultimediaWidgets.framework/Headers -I/Users/tommaso/neutrino/debug -I/Users/tommaso/neutrino/src/graphics -I/Users/tommaso/neutrino/src/doc -I/Users/tommaso/neutrino/src/pans -I/Users/tommaso/neutrino/src/pans/VISAR -I/Users/tommaso/neutrino/src/pans/colorbar -I/Users/tommaso/neutrino/src/pans/winlist -I/Users/tommaso/neutrino/src/../nPhysImage -I/Users/tommaso/neutrino/nPhysImage -isystem /usr/local/opt/qt5/lib/QtUiPlugin.framework/Headers -O3 -fopenmp -fopenmp -O0 -ggdb -D__phys_debug=10 -std=c++11 -Wall -fPIC -mmacosx-version-min=10.6 -o CMakeFiles/Neutrino.dir/neutrino.cc.o -c /Users/tommaso/neutrino/src/neutrino.cc
In file included from /Users/tommaso/neutrino/src/neutrino.cc:26:
In file included from /usr/local/opt/qt5/lib/QtSvg.framework/Headers/QtSvg:3:
In file included from /usr/local/opt/qt5/include/QtSvg/QtSvgDepends:3:
In file included from /usr/local/opt/qt5/include/QtCore/QtCore:4:
In file included from /usr/local/opt/qt5/lib/QtCore.framework/Headers/qabstractanimation.h:37:
In file included from /usr/local/opt/qt5/include/QtCore/qobject.h:41:
/usr/local/opt/qt5/include/QtCore/qstring.h:739:55: error: no type named 'u16string' in namespace 'std'
static inline QString fromStdU16String(const std::u16string &s);
It turns out (correct me if I'm wrong) that I need probably to compile qt with the same clang-omp compiler.
I thought no big deal but homebrew cries when I try this:
HOMEBREW_CC=clang-omp HOMEBREW_CXX=clang-omp++ brew install -s qt5
Error: Invalid value for HOMEBREW_CC: clang-omp
So is there a way to have Qt rebuilt using homebrew?
Is there a command to get the available compilers for homebrew?
Recompiling Qt with a different compiler probably is not going to solve the problem.
Have a look at this other question instead ...
It may also be a bug in Qt's header that can't detect your compiler properly and then wrongly assumes it has support for std::u16string
Or maybe you just forgot to add #include <string>

CMake target_include_directories(SYSTEM ...): SYSTEM option does not seem to work on recent versions of CMake targeting Xcode

We are building an application depending on Boost, and compilation generates a lot of warning "Class member cannot be redeclared" in a Boost header (tag_of.hpp).
To avoid spamming the build log, we decided to include Boost headers as system headers:
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
Our understanding is that this command should put Boost include directory behind a -isystemcompiler flags. Yet it does no such thing, instead putting it alongside 'normal' include directories in the header search path.
The environment is CMake 3.0.0, generating a project file for Xcode 5.1.1.
This seems to be an issue with some others (see last two comments in this answer).
Is there a working way to include headers as system ?
EDIT: Just tested in the same environment, updating CMake to version 3.3.0, and the behaviour is the same.
EDIT: Here is a minimal example that is showing the issue on my environment
cmake_minimum_required(VERSION 3.0)
find_package(Boost 1.49 COMPONENTS)
project(system_dependencies)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
The Xcode project file generated from this script places the path to Boost in the build setting: Header search path, instead of appending it behind a -isystem compiler flag. Which is confirmed by the compilation command issued by the IDE:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
-fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.9 -g -Wno-sign-conversion -I/Users/.../system_dependencies/build/Debug/include
-I/Users/.../SDK/boost/include
......
This issue looks to only occur for the XCode generator, as CMake believes it doesn't support the isystem flag. I would think the best solution is to report this issue on CMake bug tracker.