#incluir en .h o .c/.cpp?

#incluir en .h o .c/.cpp?


Al codificar en C o C++, ¿dónde debo tener el #include 's?


devolución de llamada.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

devolución de llamada.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));
...

¿Todos los incluidos deben estar en .h o .c / .cpp, o en ambos como he hecho aquí?


Respuestas:


Pon todo lo que puedas en el .c y lo menos posible en el .h . Los incluye en el .c solo se incluyen cuando se compila ese archivo, pero se incluyen para el .h tiene que ser incluido por cada archivo que lo use.


Algunas respuestas de código


#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
#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));
...
#ifndef MY_HEADER_H #define MY_HEADER_H  #include <stdio.h>
void doStuffWith(FILE *f);
// need the definition of FILE from stdio.h #endif