bob.smart
bob.smart

Reputation: 45

How to build a Shared Library but use static glibc on Linux?

I'm trying to create a shared library on centos using gcc 4.8.2

shared library code:

//reload.c
int func(int num){
    return num++;
}

link command:

gcc -fPIC -shared reload.c -o reload.so

use ldd command:

linux-vdso.so.1 =>  (0x00007ffe6aa93000)
libc.so.6 => /usr/lib64/libc.so.6 (0x00007f27feb97000)
/lib64/ld-linux-x86-64.so.2 (0x00007f27ff169000)

Now, Want to statically link glibc, how to write it?

like it:

ldd xxx.so
    not a dynamic executable

I tried the build options, but the error.

gcc -fPIC -shared reload.c -o reload.so -Wl,-Bstatic -lc
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

thank you very much

Upvotes: 3

Views: 1593

Answers (1)

Ctx
Ctx

Reputation: 18410

You do not have dependencies to glibc at all for your code above, so the easiest way is to compile with the flag -nostdlib:

$ gcc -fPIC -shared reload.c -o reload.so -nostdlib
$ ldd reload.so
statically linked

Upvotes: 2

Related Questions