Why can't I link the mixed C / C ++ static library to the C interface using gcc?

I have a mixed C / C ++ library.

Outside, it provides a C interface using extern C. Inside there are templates and classes. Creating a library using "ar" did not pose a problem. The file is called libo-client.a.

However, when linking the .a file using gcc (not g ++), I get a lot 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 compilation / link line is as follows:

 gcc $(CFLAGS) $(INCLUDES) test2.c libo-client.a -o test2 

Where test2 is my test harness.

This problem does not occur when I use g ++. However, I am going to link this library to a C project that is compiled with gcc. How do I get around this? What is the reason for this?

EDIT:

Despite the fact that I do not use the standard C ++ library, I obviously need some things, such as the new / delete operator, etc., and there are exceptions inside.

I am associating this thing with the Xen hypervisor, so I'm not sure which options I have, but completely rewrite this thing or maybe try to compile Xen with g ++ instead?

+4
source share
5 answers

The easiest way to solve your problem is to link with g++ ; it gets the right libraries in place.

The problem is that C ++ has many requirements that C does not do, and does more work before calling main() to make sure things are properly initialized.

If you insist on linking to the C compiler, at least you will need to enable the C ++ support library using your link command.

+9
source

Your C ++ library is still a C ++ library, which means that it contains external links to various utility functions from the standard C ++ library. To resolve these links, you need to link your final executable to the C ++ standard library. gcc does not do this automatically. Either specify the standard C ++ library for gcc , or use the g++ compiler instead.

+2
source

The archive is still only a bunch of object files (.o), so when linking them to an executable or shared library, you still need to link to g++ to allow characters for the C ++ runtime (for example, when you see the messages about errors).

In order for your C functions to have C-linkage, there must be enough of them to wrap them in extern "C" blocks. Things can get complicated, for example, Windows, with it myriad of link macros (STDCALL, WINAPI, etc.), which expand to other things (some specific to the compiler), such as __stdcall, __declspec (export), etc.

+1
source

The errors you see appear due to the lack of communication with the standard C ++ library, which has a symbol definition for the new operator, deleting the operator, etc. Try installing stdc++ say gcc $(CFLAGS) $(INCLUDES) test2.c libo-client.a -o test2 -lstdc++ . Better option suggested by Jonathan Leffler to use g++ instead of gcc

+1
source

Writing C ++ files that can be used with C without any additional requirements required by C ++ is very difficult. Probably even compiler dependent. Your library requires a C ++ runtime, and to initialize a C ++ runtime, presumaby requires special support before executing caling main () in your program. Your chimera is in a C ++ program when you link C files, even if it is main () and everything else is outside your library.

0
source

All Articles