清理你的#include语句? [英] Clean up your #include statements?

查看:319
本文介绍了清理你的#include语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C或C ++项目中维护#include语句?似乎几乎不可避免的是,最终在一个文件中的include语句集不是足够的(但由于项目的当前状态而发生)或包含不再需要的东西。

How do you maintain the #include statements in your C or C++ project? It seems almost inevitable that eventually the set of include statements in a file is either insufficient (but happens to work because of the current state of the project) or includes stuff that is no longer needed.

您是否创建了任何工具来发现或纠正问题?任何建议?

Have you created any tools to spot or rectify problems? Any suggestions?

我一直在考虑编写一些东西,编译每个非头文件单独多次,每次删除一个#include语句。继续这样做,直到达到最小的包含集。

I've been thinking about writing something that compiles each non-header file individually many times, each time removing an #include statement. Continue doing this until a minimal set of includes is achieved.

要验证头文件是否包含所需的所有内容,我将创建一个源文件,一个头文件并尝试编译它。如果编译失败,头文件本身缺少一个包含。

To verify that header files are including everything they need, I would create a source file that all it does is include a header file and try to compile it. If the compile fails, then the header file itself is missing an include.

在我创建之前,我想我应该问这里。这似乎有点普遍的问题。

Before I create something though, I thought I should ask here. This seems like a somewhat universal problem.

推荐答案


要验证头文件是否包括他们需要的一切,我会创建一个源文件,它所做的是包含一个头文件,并尝试编译它。如果编译失败,那么头文件本身缺少一个include。

To verify that header files are including everything they need, I would creating a source file that all it does is include a header file and try to compile it. If the compile fails, then the header file itself is missing an include.

通过制定以下规则, foo .c或 foo .cpp必须包含的第一个头文件应该是相应命名的 foo .h。
这样做可以确保 foo .h包含了编译所需的任何内容。

You get the same effect by making the following rule: that the first header file which foo.c or foo.cpp must include should be the correspondingly-named foo.h. Doing this ensures that foo.h includes whatever it needs to compile.

此外,Lakos的书< Scale C ++软件设计(例如)列出了用于将实现细节从头部移动到相应的CPP文件中的许多技术。如果你把它带到极限,使用像Cheshire Cat(隐藏所有实现细节)和Factory(隐藏子类的存在)的技术,那么许多标题将能够独立,而不包括其他标题,而​​是做只做

Furthermore, Lakos' book Large-Scale C++ Software Design (for example) lists many, many techniques for moving implementation details out of a header and into the corresponding CPP file. If you take that to its extreme, using techniques like Cheshire Cat (which hides all implementation details) and Factory (which hides the existence of subclasses) then many headers would be able to stand alone without including other headers, and instead make do with just forward declaration to opaque types instead ... except perhaps for template classes.

最后,每个头文件可能需要包括:

In the end, each header file might need to include:


  • 没有作为数据成员的类型的头文件(而是使用cheshire catakapimpl技术在CPP文件中定义/隐藏数据成员)

  • No header files for types which are data members (instead, data members are defined/hidden in the CPP file using the "cheshire cat" a.k.a. "pimpl" technique)

没有头文件用于作为参数的类型或从方法返回类型(相反,这些是类似 int ;或者,如果它们是用户定义的类型,那么它们是引用,在这种情况下,一个向前声明的,不透明的类型声明只是 class Foo;

No header files for types which are parameters to or return types from methods (instead, these are predefined types like int; or, if they're user-defined types, then they're references in which case a forward-declared, opaque type declaration like merely class Foo; instead of #include "foo.h" in the header file is sufficient).

你需要的头文件是:


  • 超类,如果这是一个子类

  • The superclass, if this is a subclass

可能是用作方法参数和/或返回类型的任何模板类型:显然你应该能够转发声明模板类,但是一些编译器实现可能有一个问题(虽然你也可以封装任何模板例如 List< X> 作为用户定义类型的实现细节。 ListX )。

Possibly any templated types which are used as method parameters and/or return types: apparently you're supposed to be able to forward-declare template classes too, but some compiler implementations may have a problem with that (though you could also encapsulate any templates e.g. List<X> as implementation details of a user-defined type e.g. ListX).

standard.h,其包括由任何所使用的所有系统文件(例如STL头部,O / S特定类型和/或任何 #define /所有头文件在项目中,并将其作为每个应用程序头文件中的第一个头(并告诉编译器将此standard.h视为预编译头文件)。

In practice, I might make a "standard.h" which includes all the system files (e.g. STL headers, O/S-specific types and/or any #defines, etc) that are used by any/all header files in the project, and include that as the first header in every application header file (and tell the compiler to treat this "standard.h" as the 'precompiled header file').


//contents of foo.h
#ifndef INC_FOO_H //or #pragma once
#define INC_FOO_H

#include "standard.h"
class Foo
{
public: //methods
  ... Foo-specific methods here ...
private: //data
  struct Impl;
  Impl* m_impl;
};
#endif//INC_FOO_H


//contents of foo.cpp
#include "foo.h"
#include "bar.h"
Foo::Foo()
{
  m_impl = new Impl();
}
struct Foo::Impl
{
  Bar m_bar;
  ... etc ...
};
... etc ...

这篇关于清理你的#include语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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