943,667 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1939
  • C RSS
Jan 23rd, 2009
0

Please help me to run my own header file

Expand Post »
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();
}
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
yabuki is offline Offline
7 posts
since Jan 2009
Jan 23rd, 2009
0

Re: Please help me to run my own header file

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.
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008
Jan 23rd, 2009
0

Re: Please help me to run my own header file

  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jan 23rd, 2009
0

Re: Please help me to run my own header file

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?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 23rd, 2009
0

Re: Please help me to run my own header file

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
yabuki is offline Offline
7 posts
since Jan 2009
Jan 23rd, 2009
0

Re: Please help me to run my own header file

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
yabuki is offline Offline
7 posts
since Jan 2009
Oct 16th, 2010
0

header files

I also used that tip, thanks it worked!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xmelx is offline Offline
1 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: array statement question.
Next Thread in C Forum Timeline: Finding Largest sum Subarray





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC