Remove #pragma once warnings

和自甴很熟 提交于 2019-12-18 03:44:00

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/23135538/remove-pragma-once-warnings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!