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();
}

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

Member Avatar for iamthwee
// 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();
}

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?

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.

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

I also used that tip, thanks it worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.