我有一系列的C++类存储在一个具有C接口的库中(见下面的示例)。我有一个包含这个C++库的C程序,通过C接口使用它似乎很好用。但是当我尝试在库中创建一个带有
我使用gcc编译C代码和g++编译C++库,在Ubuntu上使用Eclipse创建项目。
我收到的错误消息是:
主C文件
new
和delete
的类时,出现了问题。我使用gcc编译C代码和g++编译C++库,在Ubuntu上使用Eclipse创建项目。
我收到的错误消息是:
undefined reference to 'operator new(unsigned int)'
undefined reference to 'operator delete(void*)'
库头文件
#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_ */
库的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( );
}
主C文件
void * __gxx_personality_v0 ;
int main(void) {
printf( "foo_bar 10 =%d\n", foo_bar() ) ;
return 0;
}
我尝试了几件事情,但没有成功,有什么想法吗?
编辑
看起来这是由Eclipse自动生成的make文件出了问题。一旦我手动更改了C应用程序的makefile以链接g++而不是gcc,我就能够构建应用程序。有关更多信息,请参见下面的评论。
main
的翻译单元应该被编译为C++代码,而不是C代码。 - ascheplermain
注释,或者提供一个链接吗?谢谢! - Robᵩ