Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ dynamic vs static linking discrepancy in 32bit vs 64bit compilations

Tags:

c++

g++

qt

In Windows 7 64bit I’m using MinGW-w64 (from MinGW-build project, package by NiXman). Specifically, I am using x64-4.8.0-release-win32-sjlj-rev2.7z. x64 = development machine. Win32= threading model. This can compile both 32bit and 64bit targets.

When I compile and empty cpp file with just a plain cp main and a printf line saying hello… there is inconsistency between whether I compile it as 32bit or 64bit.

When I compile as 32bit with g++ -m32 test.cpp

The dependencies are:

  • LIBGCC_S_SJLJ-1.DLL
  • LIBSTDC++-6.DLL
  • KERNEL32.DLL
  • MSVCRT.DLL

When I compile as 64bit with g++ -m64 test.cpp

The dependencies are only:

  • KERNEL32.DLL
  • MSVCRT.DLL

I don’t understand what the situation is with the LIBGCC_S_SJLJ-1 and LIBSTDC++-6 dependencies when I compile in 64bit mode. Are these two things not needed for 64bit C++ compilations… or are they automatically statically linked in?

If they are automatically linked in for one but not the other, what is the reason for this?

I know I can link LIBGCC and LIBSTDC++ statically for 32bit projects with -static-libgcc and -static-libstdc++. Though I’m not sure this is good practice or not.

I tried -shared-libgcc and -shared-libstdc++ so that my 64bit compilation would have a dynamic dependency on LIBGCC and LIBSTDC++ but g++ refuses to link these dynamically when using the –m64 flag (compiling as 64bit).

I’ve read that statically linking LIBGCC and LIBSTDC++ is a bad thing to do and that it prevents people from linking in other 3rd party dynamic libs safely because of something (I didn’t really understand the claim).

I would really appreciate if someone could shed light on this discrepancy in g++ behaviour and what the best practice is in this regards.

like image 425
user1118167 Avatar asked May 15 '13 15:05

user1118167


People also ask

When to use static linking vs dynamic linking?

Static linking includes the files that the program needs in a single executable file. Dynamic linking is what you would consider the usual, it makes an executable that still requires DLLs and such to be in the same directory (or the DLLs could be in the system folder).

Which is bigger in process memory a statically linked binary or a dynamically linked binary?

File size. While statistically linked files are larger in size, dynamically linked files are smaller in size.

What is a dynamically statically linked file?

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk. This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.

What does static linking do?

Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system. This option forces the linker to place the library procedures your program references into the program's object file.


1 Answers

Reading this http://sourceforge.net/apps/trac/mingw-w64/wiki/Native%20Win64%20compiler suggests to me that the native compiler has been built with --disable-shared flag and the dependencies are statically linked into your application. They are certainly required.

LIBGCC_S_SJLJ-1.DLL is required to handle exceptions, and LIBSTDC++-6.DLL is the C++ standard library.

I am not clear on why there is a 32/64 difference. Possibly because the backends were generated with different flags.

I don't see a real problem with statically linking these dependencies, in fact, the decision was made for with regards to 64 bit. I would do the same for 32 bit.

like image 119
Montdidier Avatar answered Sep 23 '22 12:09

Montdidier