Wispy
Wispy

Reputation: 199

Is it better to keep functions in header files or source files

Is it better to keep functions in header files and include them into the main source file, or to keep functions in source files and link them in the main source file?

Some libraries keep functions in their separate source files which are declared in a special header file. When you include that header file in your main source files, you link all of those functions. However, it creates an object files for each of those source files (doesn't it?). I'm thinking wouldn't it be more efficient to keep function in header files that are all included in a special header file?

Including functions

#include <foo.h>
#include <boo.h>

int main() {

}

Linking functions

int foo();
int boo();

int main() {

}

Upvotes: 2

Views: 2937

Answers (3)

John Bode
John Bode

Reputation: 123468

Practical reasons to use separate source files:

  • Real projects can grow to dozens, even hundreds of files - if you’re doing everything with headers, you could wind up with a translation unit that’s too large for the compiler to handle;

  • You can’t do incremental builds if everything’s in header files - the smallest change requires a rebuild of the entire project. I’ve worked on projects that literally took hours to build, so that’s not practical;

  • While C doesn’t have a concept of “public” or “private” like C++ or other object-oriented languages, you can “hide” function and file scope variable names so that they’re not visible outside the current source file; however, if you include everything as headers, then you lose that ability and everything is visible to everything else;

  • Headers can include other headers which can include other headers, etc. This leads to two problems - duplicate definitions and circular dependencies (A requires B which requires C which requires A). The former can be dealt with by using include guards, but the second problem tends to be much thornier.

Basically, using separate source files makes code easier to maintain, easier to test, and faster to build.

Upvotes: 3

Adrian Costin
Adrian Costin

Reputation: 438

the declaration of functions inside header files and definition inside separate cpp files help keep your code uncluttered. yes, it will create separate object files for each .h and .cpp file pair but it should't be too big of a deal. in regards to the question, the same thing would happen if you included all the header files inside a main .h file, because it would just compile them one by one and generate object files

Upvotes: 0

0___________
0___________

Reputation: 67516

Header files should not contain any code or data definitions.

In header files:

  1. Function prototypes
  2. Types declarations (typedefs, structs, unions etc)
  3. extern objects declarations
  4. Macro definitions

and as an exception

  1. static inline functions definitions.

All other code or object definitions should be in the .c source files.

Upvotes: 3

Related Questions