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

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

问题描述

如何在 C 或 C++ 项目中维护#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.

推荐答案

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

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 的Large-Scale C++ Software Design 一书(例如)列出了许多将实现细节从标头移出并移入相应 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:

  • 对于属于数据成员的类型没有头文件(相反,使用柴郡猫"又名pimpl"技术在 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; 而不是头文件中的 #include "foo.h" 就足够了).

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).

那么你需要的是头文件:

What you need then is the header file for:

  • 超类,如果这是子类

  • 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 特定类型和/或任何 #defines 等)由项目中的任何/所有头文件使用,并将其作为每个应用程序头文件中的第一个头文件(并告诉编译器将此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天全站免登陆