BD at Rivenhill
BD at Rivenhill

Reputation: 12953

How to force gcc to link like g++?

In this episode of "let's be stupid", we have the following problem: a C++ library has been wrapped with a layer of code that exports its functionality in a way that allows it to be called from C. This results in a separate library that must be linked (along with the original C++ library and some object files specific to the program) into a C program to produce the desired result.

The tricky part is that this is being done in the context of a rigid build system that was built in-house and consists of literally dozens of include makefiles. This system has a separate step for the linking of libraries and object files into the final executable but it insists on using gcc for this step instead of g++ because the program source files all have a .c extension, so the result is a profusion of undefined symbols. If the command line is manually pasted at a prompt and g++ is substituted for gcc, then everything works fine.

There is a well-known (to this build system) make variable that allows flags to be passed to the linking step, and it would be nice if there were some incantation that could be added to this variable that would force gcc to act like g++ (since both are just driver programs).

I have spent quality time with the gcc documentation searching for something that would do this but haven't found anything that looks right, does anybody have suggestions?

Upvotes: 4

Views: 1497

Answers (3)

BD at Rivenhill
BD at Rivenhill

Reputation: 12953

Many thanks to bmargulies for his comment on the original question. By comparing the output of running the link line with both gcc and g++ using the -v option and doing a bit of experimenting, I was able to determine that "-lstdc++" was the magic ingredient to add to my linking flags (in the appropriate order relative to other libraries) in order to avoid the problem of undefined symbols.

For those of you who wish to play "let's be stupid" at home, I should note that I have avoided any use of static initialization in the C++ code (as is generally wise), so I wasn't forced to compile the translation unit containing the main() function with g++ as indicated in item 32.1 of FAQ-Lite (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html).

Upvotes: 0

Scott Wisniewski
Scott Wisniewski

Reputation: 25031

The problem is that C linkage produces object files with C name mangling, and that C++ linkage produces object files with C++ name mangling.

Your best bet is to use extern "C" before declarations in your C++ builds, and no prefix on your C builds.

You can detect C++ using

#if __cplusplus

Upvotes: 0

Steve-o
Steve-o

Reputation: 12866

Considering such a terrible build system write a wrapper around gcc that exec's gcc or g++ dependent upon the arguments. Replace /usr/bin/gcc with this script, or modify your PATH to use this script in preference to the real binary.

#!/bin/sh

if [ "$1" == "wibble wobble" ]
then
  exec /usr/bin/gcc-4.5 $*
else
  exec /usr/bin/g++-4.5 $*
fi

Upvotes: 1

Related Questions