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?
Do not have a header file including other header files. Let the
.cfile do that – makes compilation quicker.Use forward declarations. Makes recompilation quicker as it does not need to open up other files and if any simple change the
makecommand will spend ages compiling lots of stuff.Group functions together in both a header file and the corresponding
.cfile 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.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.