60

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.

2
  • 43
    with a text editor? Commented May 14, 2010 at 1:45
  • 12
    If 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? Commented May 14, 2010 at 2:06

2 Answers 2

139
  1. Open your favorite text editor
  2. Create a new file named whatever.h
  3. 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 $ 
Sign up to request clarification or add additional context in comments.

17 Comments

you might want to mention header guards
@luke: Thanks a lot. Have no idea what header guards are. Going to google it now. Thanks again.
@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
That is nice, but can't I say what a function does in the header file to? Is there any other form of modular programming?
@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.
|
3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.