Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header naming convention

Tags:

c

header-files

From

How can I define a C function in one file, then call it from another?

Say I define a function in the file func1.c, and I want to call it from the file call.c, how would I accomplish this?

and the answer

You would put a declaration for the function in the file func1.h, and add #include "func1.h" in call.c. Then you would compile or link func1.c and call.c together (details depend on which C system).

My question is does the name of the header file have to be func1.h, as in <name-of-c-file>.h, or is this just a best practice? Please provide link for reference if possible.

like image 804
Zombo Avatar asked Oct 23 '12 01:10

Zombo


2 Answers

In c, you don't have to call your files anything at all. As such, you can call your header mylongheadername.h, while you call your source file a.c and have a function in it called justArandomFunctionName.

However, you should be aware that your source file needs to include your header file. Generally there is a strong link between headers and source files, so that's the reason this is just about always done in this way. However, the following is completely valid:

a.c : func1 implementation
      func2 implementation

b.c : func3 implementation
      func4 implementation

c.h : func1 declaration
      func3 declaration

d.h : func2 declaration
      func4 declaration

However, there are some problems to this approach that might occur when using files set up like this (which mean you have to put extra work into structuring these files right) and it's just poor practice. But, the way one uses header files is just convention and barely any of it is enforced by the language.


Then there is the question how this can work if the header file does not know where the function is defined. The idea of this is that it doesn't need to know.

Basically, all your header does is tell your compiler that somewhere you defined a function that fits a certain profile (what name, what parameters, what return type). When your compiler reads this, basically all it does is mix all this info into a fancy name, which it will then insert into the file that is calling, which means it still doesn't do anything. The next step you need to take is to use the linker to turn the compiled versions of each of your files into a single executable. This does a number of things, but one of the most important ones is that it resolves all those fancy names the compiler cooked up. However, the way that your linker does this, is that it just reads all the compiled versions of your files and matches the definition of functions to their actual location in other code. Because it just handles all you have at the same time, it doesn't matter where your functions were defined and the header file never needs to know this.

like image 146
Jasper Avatar answered Sep 30 '22 12:09

Jasper


No, header files do not need to match any corresponding C source file. It is as you've said just convention.

like image 25
Kyle Burton Avatar answered Sep 30 '22 14:09

Kyle Burton