Šimon Tóth
Šimon Tóth

Reputation: 36451

Any difference in linking with gcc vs. g++?

Are there any differences in the linking process between gcc and g++?

I have a big C project and I just switched part of the code to C++. The code isn't using std C++ library yet, so -llibstdc++ isn't needed for now.

Upvotes: 10

Views: 5983

Answers (3)

Chris Dodd
Chris Dodd

Reputation: 126448

gcc and g++ are both just driver programs that don't do anything other than calling other programs, so you can use the -v option to see exactly what they do -- what other programs they invoke with what args. So you can see exactly what the difference is between linking with gcc and g++ for the specific version and architecture of gcc that you happen to have installed. You can't rely on that staying the same if you want portability, however.

Depending on what you are doing, you might also be interested in the -### argument

Upvotes: 0

Mark B
Mark B

Reputation: 96291

The main difference is that (assuming the files are detected as C++) g++ sets up the flags needed for linking with the C++ standard library. It may also set up exception handling. I wouldn't rely on the fact that just because your application doesn't use the standard library that it isn't needed when compiled as C++ (for example the default exception handler).

EDIT: As pointed out in comments you'll have trouble with any constructors (that do work) for static objects as well as not getting virtual function tables (so if you're using those features of C++ you still need to link that library).

EDIT2: Unless you're using C99 specific code in your C project I would actually just switch to compiling the whole thing as C++ as the first step in your migration process.

Upvotes: 7

M'vy
M'vy

Reputation: 5774

I think that the g++ linker will look for the CPP mangled function names, and it is different from the C ones. I'm not sure gcc can cope with that. (Provided you can explicitly use the C version rather than the C++ one).

Edit:

It should work if you have

extern "C" {
<declarations of stuff that uses C linkage>
}

in your code and the object file has been compiled with g++ -c. But I won't bet on this.

Upvotes: -2

Related Questions