删除#pragma警告 [英] Remove #pragma once warnings
问题描述
我在 .cpp
s和 .hpp <中使用了
#pragma一次
/ code> s,因此我会为每个使用它的文件发出警告。我没有找到任何禁用此类警告的选项,只有 #ifndef MY_FILE_H #define MY_FILE_H /*...*/ #endif
的东西。
I am using #pragma once
in my .cpp
s and .hpp
s 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
.
所以您会建议我用 ifndef
替换每个 #pragma一次
s?
So would you recommend me to replace each #pragma once
with ifndef
s?
标题中:
#define MYFILE_H
// all the header
以及其他文件中:
#ifndef MYFILE_H
#include "myfile.hpp"
#endif
// the rest of the file
您认为呢,这样使用更好吗?还是有一个选项可以在我不知道的GCC中禁用 #pragma一次
警告?
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?
推荐答案
常见的方法是将防护仅放置在.h文件中:
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
或
#pragma once
// all your myfile.hpp here
其余文件(其他.cpp文件)对警卫什么也不做。这样您就不会收到警告。
The rest of files (other .cpp) should do nothing regarding the guards. You should not get warnings by doing this.
这篇关于删除#pragma警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!