您的preferred C / C ++的大项目头部政策? [英] Your preferred C/C++ header policy for big projects?

查看:120
本文介绍了您的preferred C / C ++的大项目头部政策?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个大的C / C ++项目时,你有没有对一些具体规则的的#include 的源文件或头文件中?

When working on a big C/C++ project, do you have some specific rules regarding the #include within source or header files?

例如,我们可以想像遵循这两个过度规则:

For instance, we can imagine to follow one of these two excessive rules:


  1. 的#include 的在被禁止的的.h 的文件;它是由每个 .C 的文件包含所有需要的头文件

  2. 每个的.h 的文件应该包括所有的依赖关系,即它应该能够单独编译没有任何错误。

  1. #include are forbidden in .h files; it is up to each .c file to include all the headers it needs
  2. Each .h file should include all its dependancies, i.e. it should be able to compile alone without any error.

我想有取舍之间的任何项目,但什么是你的吗?你有没有更具体的规则?或认为任何解决方案的任何一个环节?

I suppose there is trade-off in between for any project, but what is yours? Do you have more specific rules? Or any link that argues for any of the solutions?

推荐答案

这样的.h只包括C文件意味着,如果我只包括头(定义我想要的东西在C文件中使用)可能会失败。因为我必须包括其他20头前期它可能会失败。而更糟的是,我必须包括他们在正确的顺序即可。一个真正的大量的.h文件,该系统最终将在长期运行管理的地狱。你只是想有一个.C文件中的一个.h文件中,你花2个小时找出你需要和以何种顺序必须包括他们的其他.h文件。

Doing .h includes only in C files means that if I just include a header (defining something I want to use in the C file) might fail. It might fail because I have to include 20 other headers upfront. And even worse, I have to include them in the right order. With a real lot of .h files, this system ends up to be an administrative hell in the long run. You just want to include one .h file in one .c file and you spend 2 hours to find out which other .h files you need and in which order you must include them.

如果.h文件需要另一个.H文件被成功纳入不包含任何东西,但这个.h文件中一个C文件,而不会导致编译错误,我将包括在本.H其他.h文件中文件本身。这样,我知道肯定每个C文件可以包含每一个.h文件中,它绝不会导致错误。 .c文件从来没有担心哪些其他.h文件导入或以何种顺序,包括他们。这个工程即使有庞大的项目(1000 .h文件及以上)。

If a .h file needs another .h file to be successfully included into a C file that does not include anything else but this .h file and without causing compile errors, I will include this other .h file in the .h file itself. That way I know for sure that every C file can include every .h file and it will never cause errors. The .c file never has to worry about which other .h files to import or in which order to include them. This works even with huge projects (1000 .h files and upwards).

在另一方面,我从来不包括.h文件到另一个,如果这是没有必要的。例如。如果我有hashtable.h和hashtable.c和hashtable.c需要hashing.h,但hashtable.h并不需要它,我不包括它。我只包括在.c文件,其他.c文件包括hashtable.h不需要hashing.h或者,如果他们需要它无论出于何种原因,他们应包括它。

On the other hand, I never include a .h file into another one, if this is not necessary. E.g. if I have hashtable.h and hashtable.c, and hashtable.c needs hashing.h, but hashtable.h doesn't need it, I don't include it there. I only include it in the .c file, as other .c files including hashtable.h don't need hashing.h either and if they need it for whatever reason, they shall include it.

这篇关于您的preferred C / C ++的大项目头部政策?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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