8

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.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • I thought that both gcc and g++ would just invoke the linker (called ld on my system) so there would be no difference. The difference comes in the object files produced by either a C compiler or a C++ compiler. – quamrana Jul 19 '11 at 08:44
  • Possible duplicate http://stackoverflow.com/questions/5853664/whats-the-difference-between-gcc-and-g-gcc-c/5854712#5854712 – Johan Lundberg Jan 31 '12 at 22:41
  • @JohanLundberg LOL, did you even read the questions? – Šimon Tóth Jan 31 '12 at 22:55
  • @Let_Me_Be huh, then I think I do/did not get what you meant by linking *process*. g++ is just calling gcc while adding -llibstdc++ and I thought that was clear from other questions. – Johan Lundberg Jan 31 '12 at 23:34
  • @JohanLundberg Untrue. See the accept and answer and attached comments. – Šimon Tóth Jan 31 '12 at 23:49
  • @Let_Me_Be To my understanding and from reading the man page and the answers what I said is correct. I don't think there's there's more to it than selecting the right language and the right libraries. That is, obviously there are differences between C and C++ but for the case of C++, it's to my understanding the same to use g++ and gcc -llibstdc++ (you get C++ either on files with C++ recognized file names or by specifying that it's C++ with -std=some_C++standard_you_need). – Johan Lundberg Feb 01 '12 at 07:18
  • @JohanLundberg This is a question about linking process, not compilation. Please learn to read properly. And again, read the accepted answer. – Šimon Tóth Feb 01 '12 at 11:08
  • @Let_Me_Be Why that tone? Also I did not say compilation so I'm not sure why you mentioned that. The g++ manual does not mention any difference with regards to linking (at least not that I found), when using the commands g++ or gcc except for adding C++ standard libraries. I asked for clarification if there is such a difference because I don't think so. If there is, we should be able to find a reference for it. – Johan Lundberg Feb 01 '12 at 11:48

3 Answers3

7

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 2
    Don't forget initialization of `static` and global objects with constructors and destructors; the magic for them is in the startup routine linked in only in C++ mode. – Fred Foo Jul 18 '11 at 15:29
  • Hmm, what should when I have something like `main.c` and `real_code.cpp`? `gcc -c main.c`, `g++ -c real_code.cpp` and `g++ real_code.o main.o`? – Šimon Tóth Jul 18 '11 at 15:32
  • Could you please clarify 'It may also set up exception handling.', and also, to be explicit, do you mean that gcc -llibstdc++ is different from g++ ? My g++ man page just say *g++ is a program that calls GCC and treats .c, .h and .i files as C ++ source files instead of C source files unless -x is used, and automatically specifies linking against the C ++ library.*. – Johan Lundberg Jan 31 '12 at 23:39
  • Support for polymorphism is definitely something that is added by G++ and not present the the GCC C linker. The C linker can link simple C++ code that doesn't contain virtual methods, but you need G++ for virtual method support (as well as exceptions, and perhaps other things). I discovered this while trying to get C++ compiling for PSoC microcontrollers, see this page http://mbedded.ninja/programming/microcontrollers/psoc/using-cplusplus-with-psoc-creator. – gbmhunter Aug 30 '14 at 00:01
0

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

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
-2

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.

M'vy
  • 5,696
  • 2
  • 30
  • 43