Keith Miller
Keith Miller

Reputation: 1347

What is the most effective way to use header files

So currently in my programming I now have a fairly large range of functions I have created and stored in separate C files that I use quite frequently from project to project.

My question is what is the simplest, most effective way to implement them into other projects? Currently I just make a header file for each new project that has the function prototypes for all the custom functions I want to use.

I then have every C file in the project include this "master" header. In this header I also include header files that each C file utilizes, so every C file has one header; let's just call it master.h.

I feel like I am doing this completely wrong. Should I be making header files for each C file and including them into a master header file? or should I just create header files per C file and include them as needed? If I do that how will everything still be linked together?

What is the best way to go about using header files in projects?

Upvotes: 1

Views: 1231

Answers (3)

rashok
rashok

Reputation: 13464

Create a header file for all the .c files. Group the similar functions in a .c file. Dont forget to add header guard in each header files.

For example consider a header file one.h, it should contain the below header guards.

#ifndef ONE_H
#define ONE_H

//give your function prototypes here.

#endif //ONE_H

Header guard will be useful to avoid double includes.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60017

  1. Do not have a header file including other header files. Let the .c file do that - makes compilation quicker.

  2. Use forward declarations. Makes recompilation quicker as it does not need to open up other files and if any simple change the make command will spend ages compiling lots of stuff.

  3. Group functions together in both a header file and the corresponding .c file if they logically fit together. For static libraries the linker picks out the appropriate bits. For dynamic libraries they are loaded at run time (hence can be used by other binaries) if not currently in memory.

  4. Do not have a master.h. Just make libraries contain functions that are related (e.g. math function, input/output functions etc). The various projects can pick 'n' chose what they require.

Upvotes: 5

user1202136
user1202136

Reputation: 11567

I recommend against having a master.h file, as your whole project ends up being too coupled. Every time you change master.h all your source files need to be recompiled, which significantly slows down the build process. Instead, try to create .h files with related functions. The C library is a good inspiration for this: stdlib.h, math.h etc.

In order to use these headers, you should include them in each .c, just like you would with the standard C headers:

#include <math.h>
#include <stdio.h>

As for linking, this is a totally different subject. If most of your functions are inlined, you do not have to worry about linking. Otherwise, you should define them in .c files which are named similarly to your headers (e.g., utils.c for utils.h etc.)

Upvotes: 0

Related Questions