Search code examples
gccstaticmingwldshared

Mingw gcc, "-shared -static" passing together


When studying Scintilla's makefile for MinGW under windows, I noticed that it is passing -shared and -static together as LDFLAGS to gcc.

LDFLAGS=-shared -static -mwindows $(LDMINGW)


I googled, and only find some information from clang: https://reviews.llvm.org/D43811

[MinGW, CrossWindows] Allow passing -static together with -shared
In these combinations, link a DLL as usual, but pass -Bstatic instead of -Bdynamic to indicate prefering static libraries.


My question is: Would GCC do the same?
I haven't find any proof yet.


Solution

  • You can pass both -static and -shared in a GCC linkage. Their combined effect is the same as you found described in your llvm link, and this has always been the case for GCC.

    -shared directs a GCC linkage to produce a shared library rather than a program, which it achieves by passing on the option -shared to its invocation of the linker.

    -static directs a GCC linkage to ignore shared libraries when resolving input library options -lname. By default -lname would be resolved by searching the specified or default linker search directories for either the shared library libname.so (on Windows, [lib]name.dll) or the static library libname.a (on Windows also [lib]name.lib) and to prefer the shared library if both of them are found in the same directory. -static simply excludes all shared libraries from the search. GCC achieves this by passing the option -Bstatic through to its invocation of the linker at a position in the generated linker commandline that precedes all of the -lname options.

    The GNU linker documentation of -Bstatic is explicit that this option is consistent with -shared and that the effect is to produce a shared library all of whose dependent libraries have been statically resolved.

    -Bstatic

    -dn

    -non_shared

    -static

    Do not link against shared libraries. This is only meaningful on platforms for which shared libraries are supported. The different variants of this option are for compatibility with various systems. You may use this option multiple times on the command line: it affects library searching for -l options which follow it. This option also implies --unresolved-symbols=report-all. This option can be used with -shared. Doing so means that a shared library is being created but that all of the library’s external references must be resolved by pulling in entries from static libraries.

    (emphasis mine).

    Although static linkage of shared library is in principle just a linkage restricted in the same way as static linkage of a program, in practice it frequently encounters a snag on Unix and Linux because all the object code linked into an ELF shared library libname.so must be Position Independent Code, as produced by the GCC compilation option -fPIC, whereas object files that are destined to be archived in static libraries are customarily not compiled with -fPIC. Linkages using -shared ... -static are thus apt to fail because necessary static libraries contain non-PIC object files.

    You do not have this worry with GCC on Windows, however, because there is no such distinction as PIC v. non-PIC in Windows PE object code.