从C程序调用的c ++库中的new和delete

Ste*_*rst 7 c c++ gcc g++

我有一系列c ++类存储在带有C接口的库中(参见下面的示例).我有一个C程序,通过C接口包含这个c ++库.这似乎工作得很好,直到我尝试用new和创建一个库在库中delete.

我正在使用gcc为C++库编译C代码和g ++,我在unbunu上用Eclipse创建了项目.

我得到的错误消息是

undefined reference to 'operator new(unsigned int)' 
undefined reference to 'operator delete(void*)' 
Run Code Online (Sandbox Code Playgroud)

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_ */
Run Code Online (Sandbox Code Playgroud)

Libary cpp文件

#include "CFoo.h"
CFoo g_foo ;

int CFoo::work() {
   CBar * b = new CBar(); 
   delete b; 
   return 1; 
}

int foo_bar( ) {
   return g_foo.work( );
}
Run Code Online (Sandbox Code Playgroud)

主c文件

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

我尝试了一些没有成功的事情,有什么想法吗?

编辑

看起来这是由Eclipse生成的自动生成的make文件的问题.一旦我手动将C applcations makefile更改为与g ++而不是gcc链接,我就能够构建applcation.请参阅下面的评论以获取更多信

Rob*_*obᵩ 10

引用unapersson:它不是在C++运行时链接.您应该使用"g ++"作为链接命令,而不是"gcc".