Creating a shared library from a static library using GNU toolchain (gcc/ld)

12,690

Solution 1

It seems you need to specify the archive as an argument, not as a library. So make that libget_zero_static.a instead of -lget_zero_static. At least it works for me this way:

gcc -shared -o libget_zero_shared.so \
-Wl,--whole-archive                  \
libget_zero_static.a                 \
-Wl,--no-whole-archive

Solution 2

You can just try another way: link twice, once for static lib, and the other for shared lib. That should be easier and more common.

Share:
12,690

Related videos on Youtube

user442585
Author by

user442585

Updated on September 18, 2022

Comments

  • user442585
    user442585 over 1 year

    I am having trouble generating a shared object from a static library. While I know there are other alternatives, I am now bothered (as opposed to stuck) by why this isn't working and how to make it work.

    Below is very simple source code I am using.

    get_zero.c

    #include "get_zero.h"
    
    int
    get_zero(void)
    {
            return 0;
    }
    

    get_zero.h

    int get_zero(void);
    

    main.c

    #include <stdio.h>
    #include <string.h>
    
    #include "get_zero.h"
    
    int
    main(void)
    {
        return get_zero();
    }
    

    The goal is create two functionally equal applications using libget_zero_static and libget_zero_shared.

    Here are my compilation/linking steps:

    gcc -c -fPIC get_zero.c
    ar cr libget_zero_static.a get_zero.o
    gcc -shared -o libget_zero_shared.so -L. -Wl,--whole-archive -lget_zero_static -Wl,-no--whole-archive
    

    And this is the error I get:

    /usr/bin/ld: cannot find -lgcc_s
    /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libc.a(init-first.o): relocation R_X86_64_32 against `_dl_starting_up' can not be used when making a shared object; recompile with -fPIC
    /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libc.a(init-first.o): could not read symbols: Bad value
    collect2: ld returned 1 exit status
    

    This is on a 64-bit Ubuntu system.

    I read about the whole-archive option here, and it seemed like this questions should have removed all of my road blocks. How to create a shared object file from static library.

  • user442585
    user442585 over 11 years
    Yes. That would be the normal route to go, but as I stated comments I think this method should be possible. I just don't understand why it isn't working.
  • user442585
    user442585 over 11 years
    This did not fix the issue for me. Are you running 32-bit? I am pretty sure this did work for me on a 32-bit system.
  • MvG
    MvG over 11 years
    @user442585: 64 bit using gcc 4.6.3 and ld 2.23. Also works using several other gcc versions, so I'd say it might be due to the ld version. What error do you get? Same one as you pasted in your question? Could the -fPIC from the compiler flags have gotten lost this time?
  • user442585
    user442585 over 11 years
    Never figured this one out, oh well..... You put in the most effort so you get the answer credit :)