Conditionally disable warnings with qmake/gcc? - c++

I am involved with a software project written in Qt and built with qmake and gcc on Linux. We have to link to a third-party library that is of fairly low quality and spews tons of warnings. I would like to use -W -Wall on our source code, but pass -w to the nasty third-party library to keep the console free of noise and clutter so we can focus on our code quality.
In qmake, is there a way to conditionally add CFLAGS/CXXFLAGS to certain files and libraries?

Jonathan, I think the problem is where your source files are including header files from 3rd party libraries, and you want to switch off the warnings for the latter.
Kevin, i think you can use pragmas to control warnings : gcc diagnostic pragmas
You could add these before and after any #includes for 3rd party libs.

What if you include your library using -isystem.
In the project file e.g.:
QMAKE_CXXFLAGS += -isystem /usr/local/boost_1_44_0

Normally, you'd build the third-party library in a separate directory from your own code, so you would have a different makefile for it, so you could put a different set of flags for that compilation.
If you've mixed the third-party library code with your own code, you have set yourself up for a maintenance nightmare.

Kevin,
qmake CONFIG+=debug QMAKE_CXXFLAGS_WARN_ON=-w QMAKE_CFLAGS_WARN_ON=-w
should do
(use CONFIG+=release if you wish...)

As Martin wrote adding the include directory via
QMAKE_CXXFLAGS += -isystem ...
suppresses warnings just in the respective headers. No need to disable warnings for any source files of your project (or even project-wide) or mess with #pragmas or wrappers files.
Note that if you're using QtCreator you'll still (i.e. additionally) want add the directory to INCLUDEPATH so the indexer picks up the headers.

Related

How can I disable compiler warnings regarding certain library?

I'm using CLion as my IDE. I downloaded MinGW from here (comes with boost), extracted, installed and connected it to CLion successfully. Then I set my compiler flags in CMakeLists and when I compiled my program, I encounted hundreds of warnings coming from boost libraries (in this case - boost/lexical_cast.hpp).
I really want to use most, if not all, of these compiler flags, but I also don't want boost (which is for sure better written than any of my own programs) to generate that much noise.
Is there any way of disabling all warnings from particular header / library (maybe even namespace)?
You can add the include paths as SYSTEM instead of standard ones:
target_include_directories(target SYSTEM ${Boost_INCLUDE_DIR})
This only works for GCC and clang, as Visual Studio doesn't have a specific include flag for system paths.

How to tell Libtool to use C++ and not C?

I am working on an Autools front-end for a C++ library. It looks like Libtool is adding C source files to the project and its causing a fair amount of trouble on some platforms. We think its causing unexplained crashes like Message “During startup program terminated with signal SIGKILL” from GDB.
The C source files cause trouble for several reasons. First, we only query CXXFLAGS and set AM_CXXFLAGS; and we don't do anything with CFLAGS or AM_CFLAGS. Second, C files need additional options in a C++ project, like -frtti and -fexceptions under GCC and options like -qrtti under IBM XL C/C++ compiler. Its not clear to me if libtool is adding the necessary options. Third, the C source files added by Libtool need additional Posix options on platforms that use Newlib, like Cygwin and MSYS. Our source files don't need the options.
I'd like to force Libtool to use C++ instead of C but I have not been able to locate an option or method to do so. I think the easiest path would be for Libtool to use lt-<some file>.cpp and CXXFLAGS rather than lt-<some file>.c and CFLAGS but I can't figure out how to do it.
How do we tell Libtool to use C++ and not C?
A related problem is How to disable C compiler in C++ Autotools project, but it only ask to use the C++ compiler for feature testing.
You could add the C compiler options you mention to the CFLAGS env var before compilation. Do you see any reasons why this would not work?

Does adding many -isystem include directives slow down the compilation process significantly?

For our software project, we have around 15 third party library dependencies. We suppress the warnings in these third party libraries using the -isystem include directive.
Our software project consists of more than 10 executables and libraries, each having dependencies on a subset of the third party libraries. We compile with clang on mac.
An easy way to disable all third party warnings for all projects in our build system is to include all third party libraries in all projects with -isystem.
The alternative is to separately define for each of our projects the -isystem directive. This is more work to set up and will blow up the configurations of all project files, but only the third party libraries that are actually used are included.
I'd prefer to go with the first option because it's not much work, but I am not sure if this will significantly slow down the compilation process?
This flag will slowdown only standard headers search because prior each standard header is found it will also search it in your directories. So, that it depends on how many such directories you add and how many standard headers you use. For each standard header the impact will be a summ of:
time ls YOUR_DIR >> /dev/null 2>&1
for each your added directory.

Qt 4.6 + MinGW: suppress warnings for generated code

We are using Axis2 (WSDL2C) to generate *.c/*.h files from WSDLs in order to be able to call webservices with Qt 4.6. But the generated code creates a massive amount of warnings (3 services -> >1k warnings), mostly about uninitialized or unused variables. How can we suppress these warnings properly?
I know I can wrap headers in #pragma to suppress warnings from 3rd party libs, but how to deal with generated code where the warnings come from the implementation?
Add this line to your Qt .pro or .pri file:
QMAKE_CFLAGS += -Wno-unused
It turns off unused warning.
The code generator is Apache, i.e. Open Source. Have you had a peek at the source? Perhaps you can patch that?

How to eliminate external lib/third party warnings in GCC [duplicate]

This question already has answers here:
How to suppress GCC warnings from library headers?
(10 answers)
Closed 7 years ago.
In the software project I'm working on, we use certain 3rd party libraries which, sadly, produce annoying gcc warnings.
We are striving to clean all code of warnings, and want to enable the treat-warnings-as-errors (-Werror) flag in GCC.
Is there a way to make these 3rd party generated warnings, which we cannot fix, to disappear?
Use -isystem
Example:
gcc -I./src/ -isystem /usr/include/boost/ -c file.c -o obj/file.o
With -isystem NO warning about boost :D
If you're using CMake, you can achieve this by adding SYSTEM to include_directories:
include_directories(SYSTEM "${LIB_DIR}/Include")
^^^^^^
I presume you are talking about the warnings coming from the 3rd party library headers.
The GCC specific solution would be to create another wrapper header file which has essentially the two lines:
#pragma GCC system_header
#include "real_3rd_party_header.h"
And use the wrapper instead of the original 3rd party header.
Check another SO response detailing the pragma. It essentially tells GCC that this (with recursively included files) is a system header, and no warning messages should be generated.
Otherwise, I'm not aware how one can disable warnings coming from the 3rd party code. Except by the brute force of course: in the build system configure the files to be built with warnings off.
http://www.artima.com/cppsource/codestandards.html
Example 1: A third-party header file.
A library header file that you cannot
change could contain a construct that
causes (probably benign) warnings.
Then wrap the file with your own
version that #includes the original
header and selectively turns off the
noisy warnings for that scope only,
and then #include your wrapper
throughout the rest of your project.