How to combine a C ++ object file in a C project compiled with gcc (not g ++)?

Another C / C ++ integration issue: I'm trying to update some deprecated C library (let it libcl.a ) with the functionality that I have in the C ++ library (let it name it libcppl.a ). The liblc.a library liblc.a used throughout my environment and is linked in many C projects using GCC (in C compiler mode):

 >> gcc prog.c -lcl 

Currently, libcl.a consists of the libcl.a object file (created using gcc from cl.c + cl.h ).

libcppl.a consists of the cppl.o object file (created using g++ from cppl.cpp + cppl.h ).

Since existing applications are written in C, and build scripts use GCC, I would like to keep the transition to the updated library as simple as possible. Thus, I want to continue to use GCC as the main compiler, but I can still contact the updated library.

Finding this answer , I could bind C ++ objects in a GCC C project with -lstdc++ :

 >> gcc -c cl.c -o cl.o >> g++ -c cppl.c -o cppl.o >> ar rcs libcl.a cl.o cppl.o >> gcc prog.c -lcl -lstdc++ 

However, I want to eliminate the explicit mention of libstdc++ on the compilation command line .

I tried to include libstdc ++ in the cl library by doing:

 >> ar rcs libcl.a cl.o cppl.o /usr/lib/libstdc++.so.6 

However, when I create the application, I get:

 >> gcc prog.c -lcl In file included from cppl.cpp:2: ./libcl.a(cppl.o):(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status 

1. Why doesn't the gcc linker find the standard C ++ library that was archived along with my objects?

2. Is there a restriction on using ar with libraries (as opposed to object files)?

3. Is there a way to overcome this problem?

+6
source share
1 answer

You can separately compile prog.c with gcc and then use g++ for reference:

 # compile the main program to an object file gcc prog.c -o prog.o #use g++ to link everything (it'll automatically pull in libstdc++) g++ prog.o -lcl 

Alternatively, you can compile and link prog.c in one step by explicitly prog.c g++ to compile prog.c as the source C file using the -x option:

 g++ -xc prog.c -lcl 

For more information on the difference in behavior between gcc and g++ see fooobar.com/questions/13043 / ....

+5
source

All Articles