Tuesday, February 15, 2011

Creating C Headers

Here are a few steps that will help in creating a C header file.

1. Create your .h file. Your header file should only include function declarations.

// my_math.h
int sum(int,int);


To avoid multiple definitions, when a header file is included in other files, use the #ifndef preprocessor directive.

// my_math.h
#ifndef my_math_h
#def my_math_h


int sum(int,int);


#endif

2. Create the function definition file.
Here you provide the definition to the functions declared in the header file.

// my_math.c
#include "my_math.h"
int sum(int x, int y){
   return x+y;


3. Compile your program:

gcc -c my_match.c my_math.o

Note that were are not linking this program, hence the -c option. (not to run the linker).

4. Create Your main program that uses the header.

#include "my_math.h"
int main()
{
     int s=0;
    s = sum(10,4);
}


click here for more info on static and shared linking. 







No comments:

Post a Comment