Please help me to run my own header file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 7
Reputation: yabuki is an unknown quantity at this point 
Solved Threads: 2
yabuki's Avatar
yabuki yabuki is offline Offline
Newbie Poster

Please help me to run my own header file

 
0
  #1
Jan 23rd, 2009
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<stdio.h>
#include "myfile.h"
main()
{
clrscr();
printf("The sum of 3 and 5 is %i",sum(3,5));
getch();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: Please help me to run my own header file

 
0
  #2
Jan 23rd, 2009
Use code tags to post your code.
instead of including myfile.h you must include myfile.c
Last edited by ajay.krish123; Jan 23rd, 2009 at 8:34 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help me to run my own header file

 
0
  #3
Jan 23rd, 2009
  1. // call this myfile.h
  2. #include <stdio.h>
  3. int sum ( int, int );
  4. int sum ( int x, int y )
  5. {
  6. return x + y;
  7. }

  1. // Program to be run: main.c
  2. #include <stdio.h>
  3. #include "myfile.h"
  4. int main ( void )
  5. {
  6. //clrscr();
  7. printf ( "The sum of 3 and 5 is %i", sum ( 3, 5 ) );
  8. getchar();
  9. return 0;
  10. //getch();
  11. }
Last edited by iamthwee; Jan 23rd, 2009 at 9:33 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 560
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 90
Murtan Murtan is offline Offline
Posting Pro

Re: Please help me to run my own header file

 
0
  #4
Jan 23rd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: yabuki is an unknown quantity at this point 
Solved Threads: 2
yabuki's Avatar
yabuki yabuki is offline Offline
Newbie Poster

Re: Please help me to run my own header file

 
0
  #5
Jan 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: yabuki is an unknown quantity at this point 
Solved Threads: 2
yabuki's Avatar
yabuki yabuki is offline Offline
Newbie Poster

Re: Please help me to run my own header file

 
0
  #6
Jan 23rd, 2009
Thanks to you ajay.krish123, iamthwee and Murtan for your advices I appreciate it
Last edited by yabuki; Jan 23rd, 2009 at 11:43 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC