Search code examples
gccshared-librariesmingwstatic-linking

Building a shared library created a static library instead


I am trying to build libass into a shared library with static linking under MinGW-w64. When I configure with

./configure --disable-static --enable-shared

it generates the shared library (dynamically-linked) as expected. However, when I attempt to force static linking by setting

LDFLAGS=-static

instead of generating a statically-linked shared library (.dll with no dependency), it generates a static library (.a).

I am almost certain that I have all the dependent static libraries and no error or warning message is shown in the make process.

Can anyone please shed some light on what I'm doing wrong?


Solution

  • libtool says No.

    The package's stock autotools ltmain.sh script parses the linkage flags and if it finds -static it will not build a shared library, just a static one.

    Which is the most it could reasonably do, because you can't statically link a shared library. A shared library must consist entirely of Position Independent (PIC) code or the linkage will fail, whereas a static linkage will call for the linkage of non-PIC object files, contributed by the non-PIC standard and runtime libraries, if nothing else.

    foo.c

    #include <stdio.h>
    
    void foo(void)
    {
        puts("foo");
    }
    

    Build a dynamically linked shared library:

    $ gcc -c -fPIC foo.c
    $ gcc -shared -o libfoo.so foo.o
    $ file libfoo.so
    libfoo.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), \
    dynamically linked, BuildID[sha1]=1adff7204d84d138a80bc4b6f3f38211e4b42812, \
    not stripped
    

    Attempt to build a statically linked shared library:

    $ gcc -c -fPIC foo.c
    $ gcc -shared -static -o libfoo.so foo.o
    /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/6/crtbeginT.o: \
    relocation R_X86_64_32 against hidden symbol `__TMC_END__' cannot be used \
    when making a shared object
    /usr/bin/ld: final link failed: Nonrepresentable section on output
    collect2: error: ld returned 1 exit status