我正在嘗試將一個大的C++程序鏈接到一個C「包裝器」,以允許與另一種編譯器理解C而不是C++(Haskell GHC,準確地說)的語言進行集成。但是我嘗試去做所以,無論是與GHC或GCC,遇到奇怪的問題。如何將C++文件鏈接到C程序?
的情況簡潔的模型,說我有在C主程序文件:
cmain.c
#include "header.h"
#include <stdio.h>
int main() {
printf("%d\n", cppfun(12));
return 0;
}
和.cpp文件中定義的輔助函數:
cppmodule .cpp
#include "header.h"
#include "further.h"
class foobar {
public:
int getfive() {return 5;}
};
extern "C" {
int cppfun(int foo) {
foobar fb;
return fb.getfive();
}
}
這麼多將編譯得很好。但是,如果相反,cppmodule.cpp指進一步.cpp文件,就像這樣:
cppmodule.cpp MK II
#include "header.h"
#include "further.h"
class foobar {
public:
int getfive() {return 5;}
};
extern "C" {
int cppfun(int foo) {
foobar fb;
return fb.getfive() + morecpp();
}
}
在新的.cpp文件是類似的東西;
morecpp.cpp
#include "further.h"
class moreobjects {
public:
int getsix() {return 6;}
};
#ifdef __cplusplus
extern "C" {
#endif
int morecpp() {
moreobjects mo;
return mo.getsix();
}
#ifdef __cplusplus
}
#endif
我突然得到當我試圖用像「GCC cmain.o cppmodule.o morecpp.o」的命令來編譯錯誤;用g ++編譯工作,但正如我所提到的,這種解決方案不適合我的目的。
錯誤我在嘗試編譯這個例子是
[email protected]:~/Projectsync/maketest$ gcc cmain.o cppmodule.o morecpp.o
cppmodule.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
同一種嘗試與我的實際項目的代碼的附加給人的形式
hopnode.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeI4nodeIPcS3_EEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeI4nodeIPcS3_EEE8allocateEmPKv]+0x4d): undefined reference to `operator new(unsigned long)'
/tmp/ccaoEEFM.o: In function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<char* const, node<char*, char*> > > >::allocate(unsigned long, void const*)':
hopnode.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKPc4nodeIS3_S3_EEEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKPc4nodeIS3_S3_EEEE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
hopnode.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKPc4nodeIS3_S3_EEEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKPc4nodeIS3_S3_EEEE8allocateEmPKv]+0x46): undefined reference to `operator new(unsigned long)'
任何見解將是錯誤的screenfulls不勝感激。
的可能重複的[什麼是的\ _ \ _ gxx \ _personality \ _v0 for?](http://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for) –
我看過這個問題,但如果一個答案我的問題包含在我未能理解的範圍內。 – Maxander
有很多重複,但也許我沒有選擇最好的一個。無論如何,解決方案是在我所鏈接的問題的第一個答案;要麼與G ++鏈接,要麼添加'-lstdC++'。 –