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.
A static library is basically a set of object files that were copied into a single file. This single file is the static library. The static file is created with the archiver (ar). First, calc_mean.c is turned into an object file: gcc -c calc_mean.c -o calc_mean.o.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With