views:

574

answers:

2

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.

+8  A: 
  1. Open your favorite text editor
  2. Create a new file named whatever_you_want.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
$
Pablo Santa Cruz
you might want to mention header guards
luke
@luke: Thanks a lot. Have no idea what header guards are. Going to google it now. Thanks again.
Pablo Santa Cruz
@luke: oh! I see. Good point. Going to add them.
Pablo Santa Cruz
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
@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
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?
Plus 1 for adding comments. Why not do it properly and use DoxyGen while you are about it?
Mawg
Hmmm, shouldn't those functions be 'extern' in the header file?
Mawg
@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 `<stddef.h>` (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
Hi. I am learning about makefiles now. How will a makefile for the above example look like?
kk
A: 

Keep in mind that you can also place the header file in another director so long as you give the path in the include statement. Just make sure you keep the path relative instead of absolute. That way your code remains portable.

For example:

include "../my_headers_directory/whatever.h"

tells the program to include whatever.h that in the parent directory's my_headers_director.

stratz