Header Files

Started by
11 comments, last by Zahlman 18 years, 8 months ago
Ok ive read a few tutorials including the one from here. Also ive read my book on c like 3 times over. Im having trouble with understanding how exactly to implement header files. I mean I know what there good for and i get the basics. But this is something i could really use a visual on. Ive try'ed working it out on a few short small blips like making a new line and stuff just to learn it but i keep geting a link error. undefiened refernce to the functions that I try calling. I could really use some help. or perhaphs a pointing towards a visual for learning how to implement header files. Im working in C btw not C++ if there was a differnce for the header part.
Do or do not there is no try -yoda
Advertisement
Okay, well let me give you an example. Suppose you wanted to make some math functions and store them in a header file. One function would square a number, the other would find out if a number is positive. Well, first you would create a header file (with the ending .h). So for instance lets call this one SomeMath.h (don't use math.h or you will get errors). Anyways here is the source for the header file:

SomeMath.h:
#ifndef __SOMEMATH_H__#define __SOMEMATH_H__#include <stdlib.h>#include <stdio.h>// Function to square a numbervoid square_num(int number);// Function to find if a number is positiveint is_positive(int number);#endif


Next, you would create what is called an implementation file. An implementation file is a .c file that implements the functions defined in the header file. Here would be the source for this particular header file:

SomeMath.c:
#include "SomeMath.h"void square_num(int number) {return (number*number);}int is_positive(int number) {   if(number > 0) return 1;   return 0;}


Finally, to use this header file, you simply have to include it in your main source file. Remember to use "" to include the file rather than <>. Here is an example for this header file:

main.c:
#include "SomeMath.h"int main(){    printf("The square root of 2 is: %d", square_num(2));    return 0;}


Remember, that in this example, both the SomeMath header file and implementation file have to be in the same folder as the main source file.
-----------------------------Play Stompy's Revenge! Now!
Ok.

Im creating a simple newline function, so that i can learn this right. Im doing every thing pretty much as you just showed yet im still getting a link error. I dont think its seeing the function properly

How does the header file know where to look for the implementation of the code?
Do or do not there is no try -yoda
Post your code so that I can see what the problem is.

The header file will usually look through any file in your project for the implementation. Just keep the source files in the same directory.
-----------------------------Play Stompy's Revenge! Now!
How do i stick source up there like you did?
Do or do not there is no try -yoda
Put the code inbetween [*source][*/source] tags, except without the *.
-----------------------------Play Stompy's Revenge! Now!
Ok here we go

First off is my Header newlines.h

#ifndef  __NEWLINES_H__#define  __NEWLINES_H__#include <stdio.h>#include <stdlib.h>/* my proto type of newline*/extern void newline(void);#endif /*__NEWLINES_H__ */


secondly the implementation


#include "newlines.h"/*My new line function*/void newline(void)  {    printf("\n");    return 0;      }


and lastly my main body


#include <stdio.h>#include "newlines.h"/*main body, loops and newlines 10 times*/int main(void)  {    int x = 1;        while(x < 10)      {        newline();        x = x+1;      }   }  


thats it.
Do or do not there is no try -yoda
Nothing seems wrong there. Try removing the extern first of all.

Also, what is the exact error you're getting?
-----------------------------Play Stompy's Revenge! Now!

[Linker error] undefined reference to `newline()'


Im using Dev-Cpp for my compiler.
Do or do not there is no try -yoda
First, make sure that all those files are in the same directory. Second, make sure all those files are in the same Dev-C++ project.
-----------------------------Play Stompy's Revenge! Now!

This topic is closed to new replies.

Advertisement