I want to be able to create a collection of functions in a header file that I could #include in one of my C Programs.
-
43with a text editor? – stefanB May 14 '10 at 01:45
-
12If I follow properly, what you want is to create a library, similar to the standard C libraries so you include a header file with function definitions, then link against that library when building your final executable. Is that right? – Conspicuous Compiler May 14 '10 at 02:06
2 Answers
- Open your favorite text editor
- Create a new file named whatever.h
- Put your function prototypes in it
DONE.
Example whatever.h
#ifndef WHATEVER_H_INCLUDED
#define WHATEVER_H_INCLUDED
int f(int a);
#endif
Note: include guards (preprocessor commands) added thanks to luke. They avoid including the same header file twice in the same compilation. Another possibility (also mentioned on the comments) is to add #pragma once
but it is not guaranteed to be supported on every compiler.
Example whatever.c
#include "whatever.h"
int f(int a) { return a + 1; }
And then you can include "whatever.h" into any other .c file, and link it with whatever.c's object file.
Like this:
sample.c
#include "whatever.h"
int main(int argc, char **argv)
{
printf("%d\n", f(2)); /* prints 3 */
return 0;
}
To compile it (if you use GCC):
$ gcc -c whatever.c -o whatever.o
$ gcc -c sample.c -o sample.o
To link the files to create an executable file:
$ gcc sample.o whatever.o -o sample
You can test sample:
$ ./sample
3
$

- 849
- 13
- 28

- 176,835
- 32
- 241
- 292
-
7
-
@luke: Thanks a lot. Have no idea what header guards are. Going to google it now. Thanks again. – Pablo Santa Cruz May 14 '10 at 01:50
-
-
oh sorry, i call them that (not sure if its standard). a header guard is when you wrap your header file in a #ifndef _HEADER_FILE_NAME_H_ #define _HEADER_FILE_NAME_H_ //define header in the ifndef, it ensure the header is only included once preventing compiler errors #endif – luke May 14 '10 at 01:52
-
2@luke They're officially called include guards, but your term is pretty common. There's also `#pragma once`, which is non-standard but widely supported and (in my opinion) much simpler – Michael Mrozek May 14 '10 at 01:57
-
1That is nice, but can't I say what a function does in the header file to? Is there any other form of modular programming? – user340838 May 14 '10 at 02:03
-
Plus 1 for adding comments. Why not do it properly and use DoxyGen while you are about it? – Mawg says reinstate Monica May 14 '10 at 02:28
-
Hmmm, shouldn't those functions be 'extern' in the header file? – Mawg says reinstate Monica May 14 '10 at 02:29
-
@mawg: personally, I always add the 'extern', but it is technically superfluous. The other item is to make sure that the header can be used on its own. For example, suppose one of the functions uses a `size_t` argument. If you include `
` (the smallest header that typedef's `size_t`), then the header can be included anywhere. See the NASA Goddard Space Flight Center coding standard (specifically, the C coding standard). – Jonathan Leffler May 14 '10 at 03:10 -
Hi. I am learning about makefiles now. How will a makefile for the above example look like? – kk. Jul 05 '10 at 12:57
-
It should be noted that the basic header file structure does not need the #ifndef, #define or #end. Also, it is not really clear to a beginner what those things do. Maybe some clarification? – Eric Brotto Feb 13 '11 at 13:08
-
Instead of first creating two object files and then link them to make executable, is there any short way to do so? – Abhishek Gupta Jul 14 '12 at 18:11
-
I am asking because while writing make file we usually mention .h file when making object file. We don't mention it while making executable. As given in GNU Make Doc Chapter-2 ,Topic- A simple make file – Abhishek Gupta Jul 14 '12 at 18:21
-
@PabloSantaCruz I am new to `C`and `header files`. I have a doubt. Is it necessary that the header file name and the file containing the definition of the prototypes have the same file name ? In your case `whatever.h`and `whatever.c`. or is there a `stdio.h` and `stdio.c`always ? – noufal Oct 10 '13 at 07:10
-
-
1@PabloSantaCruz then where would the compiler search for the function definition for the prototypes declared in the `.h` file? because we are not giving any clue where the functions are defined. – noufal Oct 11 '13 at 04:04
-
Header files can contain any valid C code, since they are injected into the compilation unit by the pre-processor prior to compilation.
If a header file contains a function, and is included by multiple .c
files, each .c
file will get a copy of that function and create a symbol for it. The linker will complain about the duplicate symbols.
It is technically possible to create static
functions in a header file for inclusion in multiple .c
files. Though this is generally not done because it breaks from the convention that code is found in .c
files and declarations are found in .h
files.
See the discussions in C/C++: Static function in header file, what does it mean? for more explanation.