Jump to content
  • Advertisement
Sign in to follow this  
Xloner12

Header Files

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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 number
void square_num(int number);
// Function to find if a number is positive
int 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!