我有一系列使用C接口存儲在庫中的C++類(請參見下面的示例)。我有一個C程序,通過C接口包含這個C++庫。這似乎工作得很好,直到我試圖在new
和delete
的庫中創建一個類。從C程序調用C++庫中的新增和刪除
我使用gcc編譯C++庫的C代碼和g ++,我在unbunu上用Eclipse編寫了項目。
,我得到的錯誤信息是
undefined reference to 'operator new(unsigned int)'
undefined reference to 'operator delete(void*)'
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文件
#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 applcations makefile與g ++而不是gcc鏈接,我就能夠構建應用程序。請參閱下面的評論以獲取更多信
這是一個鏈接問題,而不是代碼問題。你能給我們提供關於你如何鏈接你的代碼的信息嗎? – Jerome 2011-05-03 18:17:30
它不在C++運行庫中鏈接。你應該使用「g ++」作爲鏈接命令,而不是「gcc」。 – 2011-05-03 18:19:08
如果程序中有任何C++代碼,則包含'main'的翻譯單元應該被編譯爲C++代碼,而不是C代碼。 – aschepler 2011-05-03 18:23:41