8

Yet another C/C++ integration question: I am trying to update some legacy C library (let's call it libcl.a) with functionality that I have in a C++ library (let's call it libcppl.a). The liblc.a library is being used all over in my environment and is linked into many C projects, using GCC (in the C compiler mode):

>> gcc prog.c -lcl

The libcl.a currently consists of the cl.o object file (created with gcc from cl.c+cl.h).

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

Because the existing applications are written in C, and the build scripts are using GCC, I'd like to keep the transition to the updated library as simple as possible. Thus, I want to keep using GCC as the main compiler but still be able to link with the updated library.

Finding this answer, I could link C++ objects into GCC C project using -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 the libstdc++ in the compile command line.

What I tried to do was to include libstdc++ in the cl library, by doing:

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

However, when building 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 C++ standard library that was archived together with my objects?

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

3. Is there a way to overcome this problem?

Community
  • 1
  • 1
ysap
  • 7,723
  • 7
  • 59
  • 122
  • Why do you want to remove `libstdc++` from the command line? – David Rodríguez - dribeas Jan 28 '13 at 14:20
  • @DavidRodríguez-dribeas - because I already have build scripts that I don't want to change. – ysap Jan 28 '13 at 14:55
  • If it is that costly to change your build scripts, your main issue is not the question, but how to fix your build system. Programming is among other things adapting to a changing environment, and this should be a simple change in the build script. Solve the underlying issue and the symptom will go away. – David Rodríguez - dribeas Jan 28 '13 at 15:26
  • @DavidRodríguez-dribeas - yes, it may be that costly. It is not just my scripts, but others too, who rely on that library. I'd prefer to try and port the new functionality to C before forcing the general public to use C++ compiler. Otherwise, you are right, as is Michael. – ysap Jan 28 '13 at 22:11

1 Answers1

7

You can separately compile prog.c using gcc, then use g++ to link:

# 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 telling g++ to compile prog.c as a C source file using the -x option:

g++ -x c prog.c -lcl

See https://stackoverflow.com/a/5854712/12711 for more information on the difference in behavior between gcc and g++.

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thanks, but as I wrote in the question, I want to use GCC and not G++ for building the projects. – ysap Jan 28 '13 at 03:26
  • 1
    @ysap: hmm - I didn't see that, just the desire to avoid calling out `libstdc++` explicitly. Even so, I think it would be better to use `g++` or link in `libstdc++` explicitly than to attempt a hack like putting `libstdc++` into another archive (even if you can get it to work). I think that people maintaining the build down the road will not appreciate something oddball like that very much. – Michael Burr Jan 28 '13 at 03:31
  • +1, Agree with Michael, it is easier to integrate C in a C++ project than the other way around. The simplest thing is to switch to C++ and then compile specific parts (even if they are 90% of the code) with the C compiler. – David Rodríguez - dribeas Jan 28 '13 at 03:54
  • Michael, @DavidRodríguez-dribeas, generally, you are correct and I'd do that. However, there are reasons why I want to keep the "pure C environment". Unfortunately, this answer, and the comments, do not answer any of my 3 questions. – ysap Jan 28 '13 at 10:02
  • @ysap: hopefully someone else will be able to answer your questions. – Michael Burr Jan 28 '13 at 10:13