By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

linking problem

P: n/a
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


Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sameer wrote:
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

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


--
Attila aka WW
Jul 19 '05 #2

P: n/a

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bl**********@newstree.wise.edt.ericsson.se...
Sameer wrote:
Hello Groups,

#ifdef __cplusplus
extern "C" {
#endif

--
Attila aka WW

Thanks Attila,
Sameer.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.