Is is possible to disable this warning in clang? w

2019-04-19 05:04发布

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?

5条回答
乱世女痞
2楼-- · 2019-04-19 05:19

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

查看更多
戒情不戒烟
3楼-- · 2019-04-19 05:22

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)

查看更多
4楼-- · 2019-04-19 05:24

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

查看更多
叛逆
5楼-- · 2019-04-19 05:25

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

查看更多
该账号已被封号
6楼-- · 2019-04-19 05:39

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. :)

查看更多
登录 后发表回答