1

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 ?

Shashank
  • 13
  • 2
  • 1
    Why would you want to do this? If you only include a header in `main.c` it will only be visible in that file. – Gerhardh Nov 29 '18 at 09:25
  • 1
    Placing function declarations into `c` files makes no sense, did you mean `header1.h` instead of `header1.c`? Your headers should only contain extern function declarations, i.e. those that you want to share with the rest of your code. – vgru Nov 29 '18 at 09:27
  • Maybe this could be of interrest: https://stackoverflow.com/questions/3387453/include-header-files-using-command-line-option – Support Ukraine Nov 29 '18 at 09:28
  • You could create a new file called let's say *headers.h*, which includes all the sources. That way when adding a new *headerXX.h* file you'll only need to change one place. – CristiFati Nov 29 '18 at 09:28
  • @Gerhardh - I want to simplify the file management and hence want to include all headers 1 time globally and not have to include them in each of 30+ c files. – Shashank Nov 29 '18 at 15:40
  • You should only include a header where you need it. Including all headers in all C files does not make much sense. – Gerhardh Nov 29 '18 at 15:53

2 Answers2

1

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

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • This doesnt work since the core.c file needs header1.h. I am looking to not include header1.h in core.c which it needs and still get it working. – Shashank Nov 29 '18 at 10:55
  • your sentence made no sense, if glob.h include header1.h and core.c include glob.h then core.c will have header1.h datas – silmaril Nov 29 '18 at 12:03
  • @Shashank, you can not do that, the compiler must know the prototype of the (`extern`) functions before using them. – David Ranieri Nov 29 '18 at 12:12
1

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();  
 }
Mike
  • 4,041
  • 6
  • 20
  • 37