Shashank
Shashank

Reputation: 13

How to include files in C, globally?

My compilation order is :
core1.c
top.c


core1.c contents :

 #include "header1.h"
 #include "header2.h"

void function1() {   
---- }

void function2() {   
---- }

header1.c contents function declarations, enums, includes :

 #include comdef.h    
 void function1();   
 void function2();

top.c contents :

 #include "header1.h"   
 #include "header2.h"   
 void main() {     
function1();  
function2();  
}

I will add more headers and more core C files into my project. Each core.c file needs the same header files. How to get this all working, without the need to put #include header1/2.h in each core1.c, core2.c etc, and include these headers only in main.c ?

Upvotes: 1

Views: 477

Answers (2)

Mike
Mike

Reputation: 4288

Use one header for each source file:

core1.h:

#ifndef _CORE1
#define _CORE1

#include comdef.h    
void function1();   
void function2();

#endif

core1.c:

 #include "core1.h"

void function1() {   
---- }

void function2() {   
---- }

top.c:

 #include "core1.h"   
 void main() {     
    function1();  
    function2();  
 }

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41046

You can use a global header including all files

/* glob.h */

#ifndef GLOB_H
#define GLOB_H

#include "header1.h"   
#include "header2.h"   

#endif /* GLOB_H */

and in your main file

#include "glob.h"

Even if this is considered bad style, there are several projects using this approach, i.e. gtk

Upvotes: 1

Related Questions