user1118167
user1118167

Reputation: 363

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

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:

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

The dependencies are only:

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.

Upvotes: 9

Views: 948

Answers (1)

Montdidier
Montdidier

Reputation: 1210

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.

Upvotes: 1

Related Questions