本文介绍了#include头文件格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它对项目没有什么影响,但是,假设你为你的C ++代码使用#defined标题守护,你使用什么格式?例如假设标题为 foo.hpp

I know it makes little difference to a project but, assuming you use #defined header guards for your C++ code, what format do you use? e.g. assuming a header called foo.hpp:

#ifndef __FOO_HPP__
...

#ifndef INCLUDED_FOO_HPP
...

#ifndef SOME_OTHER_FORMAT

我是以大写的#defines的形式出售的,但是不能用这些保护的格式。

I'm sold on the idea of upper-case #defines but cannot settle on a format for these guards.

推荐答案

我总是在include guard中包含命名空间或相对路径,因为只有头名称被证明是危险的。

I always included the namespace or relative path in the include guard, because only the header name alone has proven to be dangerous.

/myproject/module1/misc.h
/myproject/module2/misc.h

因此,如果您使用一致的命名架构包括警卫,你最终可能会在两个文件中定义 _MISC_HPP __ (很有趣,可以找到这样的错误)。

So if you use a consistent naming schema for your include guards you might end up with having _MISC_HPP__ defined in both files (very funny to find such errors).

所以我结算了

MYPROJECT_MODULE1_MISC_H_
MYPROJECT_MODULE2_MISC_H_

这些名称相当长,但与双重定义的痛苦相比是值得的。

These names are rather long, but compared with the pain of double definitions it is worth it.

如果你不需要编译器/平台独立性,你可能会寻找一些#pragma一次的东西。

Another option, if you don't need compiler/platform independence you might look for some #pragma once stuff.

这篇关于#include头文件格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:30