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
g++ -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
 
A

Attila Feher

Sameer said:
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

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


#ifdef __cplusplus
}
#endif

file "test.c" contains

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

main()

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

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

file "cpp.cc" contains

#include<iostream>
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif
int add(int a, int b)
{
int c;
c=a+b;
return c;
}

#ifdef __cplusplus
}
#endif
 

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,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top