Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide warnings in compilation from external libraries

I have a main.cpp file which only has this code:

#include <iostream>
#include "include/rapi/RApi.h"

using namespace std;

int main() {
    std::cout << "Test\n";
    return 0;
}

When I compile, I want to see warnings from my code, but not from external included files. I have been able to achieve this in the past, but I might be missing something here in the compilation flags, as I keep on seing errors from the included header file, when I don't want to see them.

This is my compile command:

g++ -isystem include -pedantic -Wall -Wextra main.cpp -o main.o

I want to see warnings and errors from main.cpp, but not from files in the include folder.

I have tried -isysteminclude -isysteminclude/rapi, passing the -isystem to the end of the flags, but to no avail.

Am I missing something here?

like image 588
Miguel Mesquita Alfaiate Avatar asked Feb 21 '17 18:02

Miguel Mesquita Alfaiate


People also ask

How do I turn off compiler warning?

Turn off the warning for a project in Visual StudioSelect the Configuration Properties > C/C++ > Advanced property page. Edit the Disable Specific Warnings property to add 4996 . Choose OK to apply your changes.

How do you suppress warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.

How do I supress a warning in GCC?

-Wno-coverage-mismatch can be used to disable the warning or -Wno-error=coverage-mismatch can be used to disable the error. Disabling the error for this warning can result in poorly optimized code and is useful only in the case of very minor changes such as bug fixes to an existing code-base.

Can compiler warnings be ignored?

Unlike compilation errors, warnings don't interrupt the compilation process. They are not errors from the viewpoint of a programming language, but they may be software bugs. However, many compilers can be customized so that their warnings don't stop the compilation process. Warnings must not be ignored.


1 Answers

You need to add -isystem include to your compile line, then ALSO in your code use:

#include "rapi/RApi.h"

(not include/rapi/RApi.h). The -isystem option only applies the "system header" attribute to files that are looked up using that search path. If you put the full path in the #include then GCC looks up the path directly and doesn't use the -isystem path, so the "system header" attribute is not applied.

Regarding using <> vs "", the exact difference in behavior is essentially implementation-defined. There is no need to guess, just look at various SO questions and answers, such as this one.

like image 80
MadScientist Avatar answered Oct 31 '22 17:10

MadScientist