Wage
Wage

Reputation: 33

How to create a header file from a C code file so that it can be used in another program?

Let's say I have a file with a couple of functions, written in C.

What should I do to turn that file into a header file, so that I can #include it into programs in future?

Upvotes: 0

Views: 149

Answers (1)

sudo rm -rf slash
sudo rm -rf slash

Reputation: 1254

#include just copy pastes the file in place. You don't have to do anything to make a file "includable."

That said, there are some best practices:

  • Only put declarations in the header files. Put implementation in a .c file which you then compile to a .o file. Then link your .o files. This will prevent repeat compilation.
  • Include all headers that the to-be-included file needs. This way the header will be able to be included in any order.
  • Consider adding include guards
  • You should consider prefixing your functions and variables e.g. MyLib_...

Upvotes: 2

Related Questions