linking problem

S

Sameer

Hello Groups,

I am facing this linking problem while using a c++ function in a C code.
I get this error....
gcc -o a.out cpp.o test.o
test.o(.text+0x29):test.c: undefined reference to `add'
Kindly tell me what is the problem.

Regards,

file "cpp.h" contains

#if __cplusplus
extern "C" {
#endif
extern int add(int a, int b);
#if __cplusplus
}
#endif

file "test.c" contains

#include<stdio.h>
#include"cpp.h"

main()
{
int a=1, b=2 ,c;

c=add(a,b);
printf("%d\n", c);
}

file "cpp.cc" contains

#include<iostream>
using namespace std;


int add(int a, int b)
{
int c;
c=a+b;
return c;
}

Makefile

a.out: cpp.o test.o
gcc -o a.out cpp.o test.o

test.o: test.c cpp.h
gcc -c test.c

cpp.o: cpp.cc
g++ -c cpp.cc
 
J

Jeremy Yallop

rihad said:
Try #ifdef, not #if.

That shouldn't really make any difference. Undefined identifiers in
an "#if" directive are replaced with "0".

Jeremy.
 
C

CBFalconer

Sameer said:
I am facing this linking problem while using a c++ function in a
C code. I get this error....
gcc -o a.out cpp.o test.o
test.o(.text+0x29):test.c: undefined reference to `add'
Kindly tell me what is the problem.

In general, you can't use C++ in a C program. C++ modules have
modified names, to allow for the overloading.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top