8

I am using #pragma once in my .cpps and .hpps and because of that I get a warning for each file that uses it. I have not found any option to disable this kind of warning, only the thing of #ifndef MY_FILE_H #define MY_FILE_H /*...*/ #endif.

So would you recommend me to replace each #pragma once with ifndefs?

in header:

#define MYFILE_H
// all the header

and in the other files:

#ifndef MYFILE_H
#include "myfile.hpp"
#endif
// the rest of the file

What do you think, is it better to use it like this? Or there is an option to disable the #pragma once warnings in GCC, that I do not know?

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48
  • Which version of GCC are you using? Maybe it's an old one that doesn't support `#pragma once`? – Some programmer dude Apr 17 '14 at 14:15
  • 1
    There were old versions of GCC that complained about pragmas that it didn't know about. The C99 and C11 standards require (ISO/IEC 9899:2011 §6.10.6 Pragma directive): _Any such `pragma` that is not recognized by the implementation is ignored._ Since pragmas cause problems for you, especially if the compiler doesn't recognize it and you include the header more than once, but the header guards work everywhere, use the header guards. Compilers are normally clever enough to spot the header guards. – Jonathan Leffler Apr 17 '14 at 14:22
  • Just an info: if you need to remove warnings of unknown `#pragma`s, use the `-Wno-unknown-pragmas` flag – thedarkside ofthemoon Apr 30 '14 at 10:01
  • What's the exact warning message? `#pragma once in main file`? Related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89808 | https://stackoverflow.com/questions/56563679/gcc-precompiled-header-pragma-once-in-main-file – Ciro Santilli OurBigBook.com Sep 02 '20 at 09:46

2 Answers2

13

The common approach is to place the guard in the .h file only:

#ifndef MYFILE_H
#define MYFILE_H
// all your myfile.hpp here
#endif

or

#pragma once
// all your myfile.hpp here

The rest of files (other .cpp) should do nothing regarding the guards. You should not get warnings by doing this.

Tim Beaudet
  • 791
  • 7
  • 16
ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
  • 1
    I'd just like to add that it should be #ifndef MYFILE_H #define MYFILE_H // all your myfile.hpp here #endif – Tim Beaudet Apr 17 '14 at 14:19
  • He may be using an outdated version of GCC (pre 3.3) which could still generate the warnings for `#pragma once` usage, though this is unlikely. – ssell Apr 17 '14 at 14:21
  • 1
    @ssell: No, the problem was because I was using the `#pragma once` in `.cpp` too – thedarkside ofthemoon Apr 17 '14 at 14:26
  • @BlackBird true, I forgot. – ChronoTrigger Apr 17 '14 at 14:29
  • Or both. In networked systems, `#pragma once` is not always reliable, so you need the include guards anyway. (Even if all your files are currently on a local drive, that might not be the case in the future.) And if one of your targets is VC++, `#pragma once` will make a significant difference in compile times. (For other compilers, it probably doesn't change anything.) – James Kanze Apr 17 '14 at 14:39
-5

Indeed the #ifndef guard can always be used, but just to remove the warning while compiling the source which uses #pragma once I would recommend to use the -woption while compiling.

e.g. gcc -w -o <output file> <input file(s)>

Ravi Tiwari
  • 946
  • 8
  • 17