kylawl
kylawl

Reputation: 320

Is is possible to disable this warning in clang? warning: #pragma once in main file

warning: #pragma once in main file

We're running our headers through clang to get a partial AST.

Is it possible to disable that warning?

Upvotes: 23

Views: 21574

Answers (5)

W1M0R
W1M0R

Reputation: 3626

Use the -Wno-pragma-once-outside-header command line argument. Consult the Clang documentation here.

Upvotes: 12

Ravi Tiwari
Ravi Tiwari

Reputation: 962

Use the -w (lowercase w not uppercase W) option while compiling the source to suppress such warnings.

Upvotes: -1

fnc12
fnc12

Reputation: 2237

I had this thing when I accidentally included a header file in compile sources (this header has #pragma once line). To fix this remove header from compile sources (and probably you need to replace it with .cpp file)

Upvotes: 10

Quuxplusone
Quuxplusone

Reputation: 27342

There's no -W option for "#pragma once in main file", so you can't turn it off via the usual means. (However, the Clang developers are very aware that warnings without -W options suck, and there's a general rule that new warnings always get -W options. Cleaning up the old code, unfortunately, is left as an exercise for frustrated users.)

If you don't mind shell hackery, you could always do something like this:

# This gives the warning...
clang -c myheader.h

# ...This doesn't.
echo '#include "myheader.h"' | clang -c -x c++-header -o myheader.h.gch -

The trailing -, as usual, means "read from stdin". The -x c++ tells Clang what language you're using (since it can't tell from the file extension when there is no file), and changing c++ to c++-header means that we want to produce a .gch file instead of an .o file.

The two .gch files thus produced are NOT bit-for-bit identical. I don't know enough about gch files to tell you what might be observably different about their behavior. However, since all you care about is Clang's AST, I bet you'll be fine with it. :)

Upvotes: 5

Sergey Shambir
Sergey Shambir

Reputation: 1602

There are no option to control it, so just ban this warning in your code.

Upvotes: -3

Related Questions