Public Group

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
How do i stick source up there like you did?

##### Share on other sites
Put the code inbetween [*source][*/source] tags, except without the *.

##### Share on other sites
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.

##### Share on other sites
Nothing seems wrong there. Try removing the extern first of all.

Also, what is the exact error you're getting?

##### Share on other sites

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

Im using Dev-Cpp for my compiler.

##### Share on other sites
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.

1. 1
Rutin
22
2. 2
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633308
• Total Posts
3011293
• ### Who's Online (See full list)

There are no registered users currently online

×