什么是一个很好的参考文档利用和QUOT模式; .H"在C文件? [英] What is a good reference documenting patterns of use of ".h" files in C?

查看:108
本文介绍了什么是一个很好的参考文档利用和QUOT模式; .H"在C文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C接口和实现给出了数据结构的一些有趣的使用方式,但我相信还有其他那里。

"C Interfaces and Implementations" shows some interesting usage patterns for data structures, but I am sure there are others out there.

http://www.amazon.com/Interfaces-Implementations-Techniques-Addison-Wesley-Professional/dp/0201498413

推荐答案

看戈达德太空飞行中心(NASA)C编码标准(在这的网址)。它有一些很好的和有趣的指导方针。

Look at the Goddard Space Flight Center (NASA) C coding standard (at this URL). It has some good and interesting guidelines.

一个具体的指导原则,我已经为自己的code采用,是头应该是独立的。也就是说,你应该可以写:

One specific guideline, which I've adopted for my own code, is that headers should be self-contained. That is, you should be able to write:

#include "header.h"

和code应正确编译,与任何其他必要的头文件包括,无论先发生。简单的方法,以确保这是包括在执行源的头 - 如第一报头。如果编译,首标是自包含的。如果没有编译,修东西,以便它。当然,这也需要你确保头文件幂等 - 的工作一样的,不管它们是如何经常​​包括在内。有一个成语的标准为,得:

and the code should compile correctly, with any other necessary headers included, regardless of what has gone before. The simple way to ensure this is to include the header in the implementation source -- as the first header. If that compiles, the header is self-contained. If it doesn't compile, fix things so that it does. Of course, this also requires you to ensure that headers are idempotent - work the same regardless of how often they are included. There's a standard idiom for that, too:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...operational body of header.h...
#endif /* HEADER_H_INCLUDED */

有必要的,当然,有#定义在该文件的顶部,而不是在底部。否则,如果这个包含的头还包括header.h,那么你最终得到一个无限循环 - 没有健康的。即使你决定使用的策略:

It is imperative, of course, to have the #define at the top of the file, not at the bottom. Otherwise, if a header included by this also includes header.h, then you end up with an infinite loop - not healthy. Even if you decide to go with a strategy of:

#ifndef HEADER_H_INCLUDED
#include "header.h"
#endif /* HEADER_H_INCLUDED */

在code,它包含头 - 这种做法是不会建议 - 这是很重要的,包括在标题中的守卫本身太

in the code that include the header - a practice which is not recommended - it is important to include the guards in the header itself too.

在戈达德空间飞行中心的网址上面不再起作用。你可以找到答案的问题更多信息,我应该使用头文件的#include ,其中还包含的交叉引用这个问题

The GSFC URL above no longer works. You can find more information in the answers for the question Should I use #include in headers, which also contains a cross-reference to this question.

引用的美国航空航天局ç编码标准可以被访问,并通过互联网下载的存档:

The referenced NASA C coding standard can be accessed and downloaded via the Internet archive:

<一个href=\"http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard\" rel=\"nofollow\">http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard

这篇关于什么是一个很好的参考文档利用和QUOT模式; .H&QUOT;在C文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆