为什么我不能链接,具有使用gcc C接口的混合C / C ++静态库? [英] Why can't I link a mixed C/C++ static library that has a C interface using gcc?
问题描述
我有一个混合的C / C ++库。
I have a mixed C/C++ library.
在外面看,它使用的extern C.提供了一个C接口内部有模板和类。创建具有AR图书馆不构成问题。该文件称为荔波-client.a。
On the outside it provides a C interface using extern C. Internally there are templates and classes. Creating the library with "ar" posed no problems. The file is called libo-client.a.
不过,连接 .A
文件使用gcc(不是G ++)我得到很多的错误看起来像这样的时候:
However, when linking the .a
file using gcc (not g++) I get lots of errors that look like this:
libo-client.a(mysocket.o):(.rodata._ZTV7mStream[vtable for mStream]+0x10): undefined reference to `__cxa_pure_virtual'
...
mysocket.cpp:(.text+0x15ad): undefined reference to `operator new[](unsigned long)'
mysocket.cpp:(.text+0x15c1): undefined reference to `operator delete(void*)'
mysocket.cpp:(.text+0x167a): undefined reference to `__cxa_allocate_exception'
mysocket.cpp:(.text+0x16a6): undefined reference to `__cxa_throw'
...
我的编译/链接行看起来是这样的:
My compile/link line looks like this:
gcc $(CFLAGS) $(INCLUDES) test2.c libo-client.a -o test2
在哪里test2的是我的测试工具。
Where test2 is my test harness.
当我使用G ++不会发生此问题。不过,我要这个接口将库是用gcc编译的C项目。我该如何解决这个问题?什么是它的原因是什么?
This problem does not occur when I'm using g++. However, I'm going to interface this library into C project which is compiled with gcc. How do I get around this? What's the cause of it?
编辑:
虽然我没有使用标准C ++库这显然需要一些新的东西像运营商/删除等,并有例外内部。
Even though I'm not using the standard C++ library it's obviously needing some things like operator new/delete etc. and there are exceptions internally.
我连接这件事对Xen管理程序,所以我不知道我有什么选择,但彻底重写这个东西或者尝试使用G编译的Xen ++呢?
I'm linking this thing against the Xen hypervisor so I'm not sure what options i have but to totally rewrite this thing or perhaps try compiling Xen with G++ instead?
推荐答案
在你的问题,它与 G ++
链接最简单的方法;这得到正确的库到位。
The simplest way around your problem it is to link with g++
; this gets the correct libraries in place.
问题是,C ++有很多的C不要求,并要求前做更多的工作的main()
,以确保事情正确初始化。
The problem is that C++ has a lot of requirements that C does not, and does more work before calling main()
to ensure that things are properly initialized.
如果你坚持用C编译器连接,最起码你必须包括与您的链接命令C ++的支持库。
If you insist on linking with the C compiler, at the very least you'd have to include the C++ support library in with your link command.
这篇关于为什么我不能链接,具有使用gcc C接口的混合C / C ++静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!