9

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?

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
  • Did you try including with `<>` instead? Those are used for system headers. – Emil Laine Feb 21 '17 at 18:02
  • @tuple_cat you can't just change that, as far as I know. <> Is for system headers, not for files in the same folder as the rest of the project. – Miguel Mesquita Alfaiate Feb 21 '17 at 18:06
  • Why couldn't you? I mean `#include `. If I change that to use `"` I get warnings from that file. I have never heard that you couldn't have system headers in the same folder as the rest of the project, and didn't find anything saying so in the g++ manual. – Emil Laine Feb 21 '17 at 18:22
  • 5
    I think you need to add `-isystem include` to your compile line, then use `#include "rapi/RApi.h"` (not with `include`) in your source file. The `-isystem` option only applies to headers that are found using that search path, but if you `#include "include/rapi/RApi.h"` then there is no search path used, it's including the header directly. – MadScientist Feb 21 '17 at 18:22
  • 3
    The use of `""` vs `<>` is not really meaningful. It's completely up to the compiler implementation what the difference is (the standard doesn't say anything about it). Most compilers implement it so that if you use `""` then the compiler looks first in the current directory then at any directories given with `-I` and the default locations, and if you use `<>` then it looks only in `-I` and the system directories and not in the current directory (unless you use `-I.` or something). – MadScientist Feb 21 '17 at 18:25
  • @MadScientist thank you so much! I had been able to do this in the past, but I was now getting crazy about this. Post this as an answer so I can accept. – Miguel Mesquita Alfaiate Feb 21 '17 at 18:27
  • 1
    Formally there is a significant difference between `#include <...>` and `#include "..."`. The former pulls in a **header** from one of a set of implementation-defined locations. A header does not have to be a text file. The latter looks for a **text file**, again in one of a set of implementation-defined locations, and if it doesn't find it, it then looks in the locations where it would look for a `#include <...>` directive. In practice, nobody does binary headers, and the two forms of the directive are pretty much interchangeable, except for the rules about the locations where they look. – Pete Becker Feb 21 '17 at 18:51
  • *"When I compile, I want to see warnings from my code, but not from external included files."* - I feel your pain, but be careful. The warnings may appear in the included files but the problem may be caused by your own code, or there may be some incompatibility between two different libraries you are using. – Christian Hackl Feb 21 '17 at 20:27

2 Answers2

4

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.

Community
  • 1
  • 1
MadScientist
  • 92,819
  • 9
  • 109
  • 136
2
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#include "include/rapi/RApi.h"
#pragma GCC diagnostic pop

int main() {
  std::cout << "Test\n";
  return 0;
}
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 1
    I am not saying that this does not work, but I would rather have a solution that does not require me to do this hack on N files, where N is the number of files where the offending header is included. – Miguel Mesquita Alfaiate Feb 21 '17 at 18:26
  • 1
    how about making your own header that puts the GCC directives in there and then includes rapi – pm100 Feb 21 '17 at 18:28
  • 3
    BTW - ultra kudos for doing -wall -werror, true ninjas do that. -pedantic would of course move you to mega ninja mode – pm100 Feb 21 '17 at 18:30