I am building an environnement to execute c++ code and I do need to use #pragma once in my main cpp source code (and all cpp files actually). When I compile, I get the "#pragma once in main file" warning. The only way to turn this warning off is to disable all warnings with the -w option. I want to only disable this specific warning and keep all the other ones. gcc needs something like clang's -Wno-pragma-once-outside-header option. I do appreciate that this warning is actually useful but there are corner cases where this warning must be disabled.
Testcase: #pragma once int main() {} > g++ t.C -S t.C:1:9: warning: #pragma once in main file #pragma once ^~~~ you could work around this by wrapping the pragma in a preprocessor conditional
You haven't explained why do you need to use #pragma once in the main cpp source file, are you sometimes including it as a header and other times compiling it as the TU itself? Not to mention that it is best not to use #pragma once at all anywhere.
(In reply to Richard Biener from comment #1) > Testcase: > > #pragma once > int main() {} > > > g++ t.C -S > t.C:1:9: warning: #pragma once in main file > #pragma once > ^~~~ > > > you could work around this by wrapping the pragma in a preprocessor > conditional Hi, thanks for adding a minimal test case. I tried: #if 1 #pragma once #endif but I'm still getting the warning.
(In reply to Jakub Jelinek from comment #2) > You haven't explained why do you need to use #pragma once in the main cpp > source file, are you sometimes including it as a header and other times > compiling it as the TU itself? Not to mention that it is best not to use > #pragma once at all anywhere. Yes, in the system I'm working on, there is no "main file", all C++ (xcpp files actually) are small C++ programs that I link with a generated main file. xcpp programs can use other programs (xcpp files) or libs (xhpp files). This is all working pretty well right now, except for that #pragma once warning. It's still under pretty heavy development. You can try it @ https://github.com/binarez/xcpp Thanks
(In reply to sduguay from comment #3) > (In reply to Richard Biener from comment #1) > > Testcase: > > > > #pragma once > > int main() {} > > > > > g++ t.C -S > > t.C:1:9: warning: #pragma once in main file > > #pragma once > > ^~~~ > > > > > > you could work around this by wrapping the pragma in a preprocessor > > conditional > > Hi, thanks for adding a minimal test case. I tried: > > #if 1 > #pragma once > #endif > > but I'm still getting the warning. I of course meant #if !defined(THIS_IS_THE_MAIN_FILE) #pragma once #endif and compile the main file with -DTHIS_IS_THE_MAIN_FILE
In any case, I agree with confirming this as a bug: all warnings should be controllable by a -Wxxx option. Adding such an option is quite easy, and a good first contribution to GCC. For an example of adding a new option see https://gcc.gnu.org/r192968
(In reply to Jonathan Wakely from comment #6) > In any case, I agree with confirming this as a bug: all warnings should be > controllable by a -Wxxx option. This is bug 44209 > > Adding such an option is quite easy, and a good first contribution to GCC. > For an example of adding a new option see https://gcc.gnu.org/r192968
(In reply to Jonathan Wakely from comment #6) > In any case, I agree with confirming this as a bug: all warnings should be > controllable by a -Wxxx option. > > Adding such an option is quite easy, and a good first contribution to GCC. > For an example of adding a new option see https://gcc.gnu.org/r192968 I was going to propose looking into it. I'll try to find some time.
(In reply to sduguay from comment #8) > (In reply to Jonathan Wakely from comment #6) > > In any case, I agree with confirming this as a bug: all warnings should be > > controllable by a -Wxxx option. > > > > Adding such an option is quite easy, and a good first contribution to GCC. > > For an example of adding a new option see https://gcc.gnu.org/r192968 > > I was going to propose looking into it. I'll try to find some time. Have you found some time yet?
Just adding a second use-case. Emacs with cmake-ide (using flycheck) shows warnings that update as you type. They are generated by running the file through gcc, which could be a .hpp file. This usually works well enough for interactive use. But it leaves you with an irrelevant "#pragma once in main file" warning in the editor. (Not sure if this whole setup is "legit", or if there are better alternatives. It involves guessing which compile flags of some .cpp file belong to the .hpp file being edited.)
An additional use case: to test that header files can compile in isolation, one could compile the header file with -fsyntax-only ( g++ -xc++ -fsyntax-only myheader.hpp ). However, this emits the "#pragma once in main file" warning, which is undesirable, especially because it makes -Werror impossible. This means that a tool for checking header file isolation would have to emit a new file to check header isolation.
Still buggy in gcc-11 (Debian 11.1.0-3) 11.1.0 I'll have a look at the code and see if I can fix it.
Another use case. We are using precompiled headers in our application, and that includes headers which have "#pragma once". When generating the precompiled header we get this warning: warning: #pragma once in main file In that context the warning is actually invalid, so it would be really nice to be able to turn this warning off, without turning off all warnings.
(In reply to Diego SC from comment #13) > Another use case. > > We are using precompiled headers in our application, and that includes > headers which have "#pragma once". When generating the precompiled header we > get this warning: > > warning: #pragma once in main file > > In that context the warning is actually invalid, so it would be really nice > to be able to turn this warning off, without turning off all warnings. That is PR 47857 .
*** Bug 113873 has been marked as a duplicate of this bug. ***
The master branch has been updated by Ken Matsui <kmatsui@gcc.gnu.org>: https://gcc.gnu.org/g:821d56100e1110ab6a166f50819522254eb30923 commit r15-4191-g821d56100e1110ab6a166f50819522254eb30923 Author: Ken Matsui <kmatsui@gcc.gnu.org> Date: Fri Mar 1 22:10:55 2024 -0800 gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] This patch adds a warning switch for "#pragma once in main file". The warning option name is Wpragma-once-outside-header, which is the same as Clang provides. PR preprocessor/89808 gcc/c-family/ChangeLog: * c.opt (Wpragma_once_outside_header): Define new option. * c.opt.urls: Regenerate. gcc/ChangeLog: * doc/invoke.texi (Warning Options): Document -Wno-pragma-once-outside-header. libcpp/ChangeLog: * include/cpplib.h (cpp_warning_reason): Define CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER. * directives.cc (do_pragma_once): Use CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER. gcc/testsuite/ChangeLog: * g++.dg/warn/Wno-pragma-once-outside-header.C: New test. * g++.dg/warn/Wpragma-once-outside-header.C: New test. Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org> Reviewed-by: Marek Polacek <polacek@redhat.com>
Thank you for your report, sduguay. This issue has been resolved in https://gcc.gnu.org/g:821d56100e1110ab6a166f50819522254eb30923.