在#包括或.H .C /的.cpp? [英] #include in .h or .c / .cpp?

查看:103
本文介绍了在#包括或.H .C /的.cpp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用C或C ++编码,我应该在哪里有的?

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

callback.h:

callback.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:

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

  ...

都应该包括在任.h或.C /的.cpp,或两者就像我在这里做什么?

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

推荐答案

把尽可能多的,你可以在 .C ,并在<$ C尽可能少$ C> .H 。在包括 .C 当一个文件被编译,而是包括了 .H 有只包含通过使用它的每一个文件被包括在内。

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

这篇关于在#包括或.H .C /的.cpp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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