954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help me to run my own header file

I created my own header file in C by creating the ff. files: myfile.h, myfile.c and main.c.Afterwards, I compiled myfile.c so I already have the object file for it, but the problem is when I compiled and run main.c this error message appeared: "Undefined symbol _sum". Please help me to fix this error.See the complete source code of mine below.

// library header file: myfile.h
#ifndef MY_FILE
#define MY_FILE
int sum(int x, int y);
#endif

// library implementation file: myfile.c
#include "myfile.h"
int sum(int x, int y)
{
return x+y;
}

// Program to be run: main.c
#include
#include "myfile.h"
main()
{
clrscr();
printf("The sum of 3 and 5 is %i",sum(3,5));
getch();
}

yabuki
Newbie Poster
7 posts since Jan 2009
Reputation Points: 10
Solved Threads: 2
 

Use code tags to post your code.
instead of including myfile.h you must include myfile.c

ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: 6
Solved Threads: 9
 
// call this myfile.h
#include <stdio.h>
int sum ( int, int );
int sum ( int x, int y )
{
  return x + y;
}
// Program to be run: main.c
#include <stdio.h>
#include "myfile.h"
int main ( void )
{
//clrscr();
  printf ( "The sum of 3 and 5 is %i", sum ( 3, 5 ) );
  getchar();
  return 0;
//getch();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Putting code in headers is generally bad form....if you include that same header in more than one file in a compilation unit you get duplicate symbols.

The original layout of the files should have worked, did you include myfile.obj or myfile.o when you compiled main.c?

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Hello this is Yabuki, I tried your advice and it really works.I've been trying to run my own header file for a week but it isn't working but thanks for your advice it's working now.

yabuki
Newbie Poster
7 posts since Jan 2009
Reputation Points: 10
Solved Threads: 2
 

Thanks to you ajay.krish123, iamthwee and Murtan for your advices I appreciate it

yabuki
Newbie Poster
7 posts since Jan 2009
Reputation Points: 10
Solved Threads: 2
 

I also used that tip, thanks it worked!

xmelx
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You