I'm using flycheck for C++ development, and get the following warning:

#pragma once in main file

In all headers included in a main file. Searching the webs, I get the impression this has something to do with how gcc and clang (both give the same warning) compile for flymake.

I would like to get rid of this warning. Is there anything I can do?

  • It will be easier to figure out how to suppress this warning if we can reproduce it first. Can you give an example file or files that exhibit the problem, while still being as simple as you can make them?
    – Ben Liblit
    Commented Oct 31, 2015 at 20:36
  • I have a question similar to yours, I have an answer.
    – user18592
    Commented Feb 27, 2018 at 23:43

2 Answers 2


Clang has an option to disable this warning. Adding the following to my config fixed the issue for me.

(with-eval-after-load "flycheck"
    (setq flycheck-clang-warnings `(,@flycheck-clang-warnings

Unfortunately, I don't know if similar option exists for gcc.


Currently GCC has no way of suppressing this warning. And flycheck has no way to ignore errors without touching its internals.

Here is a hack that works for gcc:

; ignore gcc "#pragma once" warning
(with-eval-after-load "flycheck"
  (eval-when-compile (require 'flycheck))  ; for flycheck-error struct
  (defun my-filter-pragma-once-in-main (orig-fun errors)
    (dolist (err errors)
      (-when-let (msg (flycheck-error-message err))
        (setf (flycheck-error-message err)
              (if (string-match-p "#pragma once in main file" msg) nil msg))))
    (funcall orig-fun errors))
  (advice-add 'flycheck-sanitize-errors :around #'my-filter-pragma-once-in-main))

For completeness, @grepcake's answer for clang:

; ignore clang "#pragma once" warning
(with-eval-after-load "flycheck"
  (setq flycheck-clang-warnings `(,@flycheck-clang-warnings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.