new and delete in a c++ library being called from a C program
I have a series of c++ classes stored in a library with a C interface (see example below). And I have a C program that includes this c++ libary via the C interface. This seems to work well until I tried to create a class in the libary with new
and delete
.
I am using gcc to compile the C code and g++ for the C++ libary, I crated the projects with Eclipse on unbunu.
The error message that I get is
undefined reference to 'operator new(unsigned int)'
undefined reference to 'operator delete(void*)'
Libary H file
#ifndef CFOO_H_
#define CFOO_H_
#ifdef __cplusplus
class CBar {
public:
int i ;
};
class CFoo {
public:
int work();
};
extern CFoo g_foo ;
extern "C" {
#endif /* __cplusplus */
int foo_bar( ) ;
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CFOO_H_ */
Libary cpp file
#include "CFoo.h"
CFoo g_foo ;
int CFoo::work() {
CBar * b = new CBar();
delete b;
return 1;
}
int foo_bar( ) {
return g_foo.work( );
}
Main c file
void * __gxx_personality_v0 ;
int main(void) {
printf( "foo_bar 10 =%d\n", foo_bar() ) ;
return 0;
}
I have tried a few things with out success, any thoughts?
Edit
It looks like it was a problem with the auto generated make files produced by Eclipse. Once I manualy changed the C applcations makefile to link with g++ instead of gcc I was able to build the applcation. See comments below for mo开发者_开发技巧re information.
Quoting unapersson: It's not linking in the C++ runtime. You should use "g++" as the link command, rather than "gcc".
精彩评论