Dragos Rizescu
Dragos Rizescu

Reputation: 3488

Including Libraries in Multiple Files C Lang

I am working on a C project and I like to keep myself organised. Thus, I have got multiple header files, files with functions for that header files and a main file.

|- Header Files
|   |- hash_table.h
|   |- linked_list.h
|   |- dictionary.h
|- Source Files
|   |- main.c
|   |- hash_functions.c
|   |- list_functions.c
|   |- dictionary_functions.c

Will it be any problem if I include libraries such as #include <stdio.h> in each of that function file? Will it affect by any means the efficiency of my program?

Upvotes: 1

Views: 1234

Answers (5)

Muthe
Muthe

Reputation: 23

Mutliple file Inclusion will not effect you untill you have a INCLUSION GAURD.So by thi sYou can also do one easy thing, That is by having Single header file (suppose includes.h) which will have all include files So that you can eliminate the Order of Inclusion problem with the cost of compilation time.

Upvotes: 1

Dhananjay
Dhananjay

Reputation: 360

No, there would not be any problem if you include same header file in multiple files.

When any header file is written it is written to avoid multiple inclusion.

To avoid multiple inclusion, macro pre-processors are often used. One such mostly used way is given below.

#ifndef SOME_SYMBOL
#define SOME_SYMBOL

// actual code goes here.

#endif

For example see the code of stdio.h file for Linux at : https://github.com/torvalds/linux/blob/master/arch/powerpc/boot/stdio.h

Upvotes: 1

user3497792
user3497792

Reputation:

No problems. Standard header files are written that way. See: http://en.wikipedia.org/wiki/Include_guard

Upvotes: 1

hdante
hdante

Reputation: 8030

No, the include file is a description of the library. The library itself is a separate binary file that is linked in the final stages of program assembly

Upvotes: 1

Sergey L.
Sergey L.

Reputation: 22562

All modern headers use header guards. This means that the header checks if it has been included before and then skips it. Also the compiler is smart enough to figure out which functions defined in a header you actually use and only include those in object code.

Upvotes: 1

Related Questions