trouble with linking of c wrapper around c++ class (gcc)

S

selder21

Hi,

I am trying to make a wrapper in c so i can compile a program using
c++ code in gcc.

The next setup actually works, but when i try to use it with c++
classes i get "undefined reference to `__gxx_personality_v0' collect2:
ld returned 1 exit status"

WORKING SETUP:

test1a.cpp:

extern "C" int tomroot(int x)
{
int y = x * x;
return y;
}
_____________________________

test1b.c:

#include "stdio.h"

int tomroot(int);

int main() {
int x = 6;
printf("%d\n",tomroot(x));
}

_____________________________

commandline: g++ -c test1a.cpp; gcc -c test1b.c; gcc test1a.o test1b.o
-o test1

FAILING SETUP:

mcc.h:

#ifndef MCC_H
#define MCC_H

class mcc {

public:
int tomroot( int x );

};

#endif
_____________________________

mcc.cpp:

#include "mcc.h"

int mcc::tomroot( int x )
{
int y = x * x;
return y;
};
_____________________________

mccintermediate.cpp:

#include "mcc.h"

extern "C" int intermediatetomroot( int x )
{
mcc temp;
return temp.tomroot( x );
}
_____________________________

mccintermediatetestc.c:

#include "stdio.h"

int intermediatetomroot( int x );

int main()
{
printf("%d\n", intermediatetomroot( 5 ));
}
_____________________________

commandline: g++ -c mcc.cpp mccintermediate.cpp; gcc -c
mccintermediatetestc.c; gcc mcc.o mccintermediate.o
mccintermediatetestc.o -o mccintermediatetestc

which outputs, as mentioned above:
mccintermediate.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

Anybody has a clue ? Suggesting using g++ to compile the c code won't
do me no good, i need a c compiler ( eg gcc ) doing this job.

Thanks in advance, Tom.
 
V

Victor Bazarov

[...]
commandline: g++ -c mcc.cpp mccintermediate.cpp; gcc -c
mccintermediatetestc.c; gcc mcc.o mccintermediate.o
mccintermediatetestc.o -o mccintermediatetestc

Although it is off-topic here and next time you should ask in a G++
newsgroup, here is a possible solution: use 'g++' to link instead of
'gcc'.
which outputs, as mentioned above:
mccintermediate.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

Anybody has a clue ? Suggesting using g++ to compile the c code won't
do me no good, i need a c compiler ( eg gcc ) doing this job.

Whatever.

V
 
D

Derek

commandline: g++ -c mcc.cpp mccintermediate.cpp; gcc -c
mccintermediatetestc.c; gcc mcc.o mccintermediate.o
mccintermediatetestc.o -o mccintermediatetestc

which outputs, as mentioned above:
mccintermediate.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

Anybody has a clue ? Suggesting using g++ to compile the c code won't
do me no good, i need a c compiler ( eg gcc ) doing this job.

First, your example works with GCC 3.3.3 for Cygwin. However, in a
mixed C/C++ application it is sometimes required that main() be com-
piled as C++. You might have to compile main() with g++, though it
can delegate to a C main if you wish:

// main.cpp:
extern "C" int C_main(int, char**);
int main(int argc, char** argv) {
return C_main(argc, argv);
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top