#include 在 .h 或 .c / .cpp 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3002110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:44:52  来源:igfitidea点击:

#include in .h or .c / .cpp?

c++c

提问by Louise

When coding in either C or C++, where should I have the #include's?

在 C 或 C++ 中编码时,我应该在哪里使用#include's?

callback.h:

回调.h:

#ifndef _CALLBACK_H_
#define _CALLBACK_H_

#include <sndfile.h>
#include "main.h"

void on_button_apply_clicked(GtkButton* button, struct user_data_s* data);
void on_button_cancel_clicked(GtkButton* button, struct user_data_s* data);

#endif

callback.c:

回调.c:

#include <stdlib.h>
#include <math.h>

#include "config.h"

#include "callback.h"
#include "play.h"

void on_button_apply_clicked(GtkButton* button, struct user_data_s* data) {
  gint page;
  page = gtk_notebook_get_current_page(GTK_NOTEBOOK(data->notebook));

  ...

Should all includes be in either the .h or .c / .cpp, or both like I have done here?

所有包含都应该在 .h 或 .c / .cpp 中,还是都像我在这里所做的那样?

回答by Brendan Long

Put as much as you can in the .cand as little as possible in the .h. The includes in the .care only included when that one file is compiled, but the includes for the .hhave to be included by every file that uses it.

把尽可能多的放在里面,尽量.c少放在里面.h。中的包含.c仅在编译该文件时包含,但.h每个使用它的文件都必须包含 的包含。

回答by John Bode

The only time you should include a header within another .h file is if you need to access a type definition in that header; for example:

您应该在另一个 .h 文件中包含头文件的唯一时间是您需要访问该头文件中的类型定义;例如:

#ifndef MY_HEADER_H
#define MY_HEADER_H

#include <stdio.h>

void doStuffWith(FILE *f); // need the definition of FILE from stdio.h

#endif

If header A depends on header B such as the example above, then header A should include header B directly. Do NOTtry to order your includes in the .c file to satisfy dependencies (that is, including header B before header A); that is a big ol' pile of heartburn waiting to happen. I mean it. I've been in that movie several times, and it always ended with Tokyo in flames.

如果 header A 依赖于 header B,例如上面的例子,那么 header A 应该直接包含 header B。千万不要尝试来订购包括.c文件来满足相关性(即,包括头A之前头B); 这是一大堆胃灼热等待发生。我是认真的。我已经在这部电影里看过好几次了,它总是以东京着火收场。

Yes, this can result in files being included multiple times, but if they have proper include guards set up to protect against multiple declaration/definition errors, then a few extra seconds of build time isn't worth worrying about. Trying to manage dependencies manually is a pain in the ass.

是的,这可能会导致文件被多次包含,但是如果它们设置了适当的包含保护来防止多次声明/定义错误,那么额外的几秒钟构建时间就不值得担心了。尝试手动管理依赖项很麻烦。

Of course, you shouldn't be including files where you don't needto.

当然,您不应该在不需要的地方包含文件。

回答by Parappa

Put as many includes in your cpp as possible and only the ones that are needed by the hpp file in the hpp. I believe this will help to speed up compilation, as hpp files will be cross-referenced less.

将尽可能多的包含在您的 cpp 中,并且仅将 hpp 文件需要的那些包含在 hpp.conf 文件中。我相信这将有助于加快编译速度,因为 hpp 文件将减少交叉引用。

Also consider using forward declarationsin your hpp file to further reduce the include dependency chain.

还可以考虑在您的 hpp 文件中使用前向声明以进一步减少包含依赖链。

回答by Johnsyweb

If I #include <callback.h>, I don't want to have to #includelots of other header files to get my code to compile. In callback.hyou should include everything needed to compile against it. But nothing more.

如果我#include <callback.h>,我不想有#include很多其他头文件来编译我的代码。在callback.h你应该包括编译反对它所需的一切。但仅此而已。

Consider whether using forward declarations in your header file (such as class GtkButton;) will suffice, allowing you to reduce the number of #includedirectives in the header (and, in turn, my compilation time and complexity).

考虑在您的头文件(例如class GtkButton;)中使用前向声明是否就足够了,允许您减少头文件中的#include指令数量(进而减少我的编译时间和复杂性)。