Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am getting undefined reference to sum in 1.c and 2.c. My code:

head.h

Objective-C
#include <stdio.h>
    #ifdef HEAD_H
    #define HEAD_H
    	void sum(int a, int b)
    	{
    		int sum=0;
    		sum=a+b;
    		printf("sum is %d",sum);
    	}
    	
    #endif


1.c

Objective-C
#include "head.h"

   int main()
   {
   sum(1,2);
   callsum();
   }


2.c:

Objective-C
#include "head.h"
    void callsum(void)
    {
    sum(3,4);
    }


I have defined my function and used that header file, still why is this error?
Posted
Comments
Sergey Alexandrovich Kryukov 2-Aug-14 20:44pm    
Defining (instead of just declaring) in a header file is possible, but is a big misuse of headers.
You really need to understand what a C compiler does, and what a linker does.
—SA

1 solution

Ahem!
#ifdef HEAD_H
#define HEAD_H
you sure?
Did you mean
C++
#ifndef HEAD_H
#define HEAD_H
by any chance?
 
Share this answer
 
Comments
Philippe Mori 2-Aug-14 23:18pm    
Well spotted... even though there are many other problem as indicated by SA.
Jeevan83 3-Aug-14 3:45am    
Oh am I using #ifdef ? What kind of a fool am I? Sorry for wasting everyone's valuable time and mine too. Please spare this girl for the last time! Too ashamed to keep my question here. Only God can save me! How can I delete it?
OriginalGriff 3-Aug-14 5:08am    
Easy mistake to make - I always end up reading when I *meant* to write instead of what I *did*.
I wouldn't worry about deleting the question - it might help someone else in the same position, and it's considered rude to delete them anyway! :laugh:
Jeevan83 3-Aug-14 13:17pm    
Hey don't laugh it's hard to manage kitchen and computer sigh!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900