New and delete in a C ++ library called from a C program

I have a number 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 through the C interface. It seems to work well until I tried to create a class in libary with new and delete .

I use gcc to compile C and g ++ code for the C ++ library, I have broken projects from Eclipse to unbunu.

The error message I get is

 undefined reference to 'operator new(unsigned int)' undefined reference to 'operator delete(void*)' 

File Libary H

 #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 file c

 void * __gxx_personality_v0 ; int main(void) { printf( "foo_bar 10 =%d\n", foo_bar() ) ; return 0; } 

I tried several things without success, no thoughts?

Edit

It seems like this was a problem with auto-generated make files created by Eclipse. As soon as I manually changed the makefile using C ++ to associate it with g ++ instead of gcc, I was able to create an application. See the comments below for more information.

+7
source share
1 answer

Quote: unapersson: it does not bind at runtime with C ++. You should use "g ++" as the link command, not "gcc".

+10
source

All Articles