Linking libcurl with static libc - c

I am trying to compile a C program while linking dynamically libcurl and statically libc.
My Makefile looks like:
SRC=myprogram.c
LDFLAGS+= -static libc.a -static-libgcc -Wl,-static -lc
LDFLAGS+= -linfluxdb -lcurl -lm -ljson-c
I need to link libc dynamically because libc version is not the same between centos7 and centos6, so I will include the chosen one in the binary.
I could have linking everything statically but it's not working for libcurl; I am having errors in the linker resolving several libcurl functions.
I tried to add "dynamic" flag for libcurl
LDFLAGS+= -Wl,-Bdynamic -lcurl
But I still have a dynamic reference to libc. Ldd outputs:
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff3811b2000)
Through this question (Static linking libcurl using c) I knew that libcurl depends on libc. Is it a way to force the usage of static libc everywhere ?

Related

How does gcc on Linux know it is libc.so.6 that contains printf function?

If I do gcc hello_world.c, how does gcc/ld know it is libc.so.6 that should be linked against? Does gcc/ld go through the default shared library paths(however the linker was configured, plus some gcc additional flavors), and then scan symbol table of each .so file?
It doesn't. Unless you use -nostdlib, -lc is in the default libraries GCC passes to the linker for link commands. For other libraries, you need to explicitly request them when linking.

Does gcc links to libc.a or libc.so by default?

I am using gcc 5.4.0 on Ubuntu 16.04 64 bit. When I compile a program:
gcc -o prog prog.c
GCC automatically links to the C standard library, so I don't have to specifically do that.
How can I see which C library does gcc link against to, libc.a or libc.so, or something else?
In what circumstance does it link to libc.so? Does libc.so need to be specified at run time like other shared libraries?
Thanks in advance.
How can I see which C library does gcc links against to, libc.a or libc.so, or something else?
You can use ldd command to see all linked shared libraries. If libc.so is found, it's dynamic linking.
In what circumstance does it links to libc.so?
gcc uses dynamic linking and links to libc.so by default. If you want static linking, pass -static flag.
Does libc.so need to be specified at run time like other shared libraries?
Normally no, since it's configured by compiler automatically.

How to statically link libc while keeping dynamic loading?

I want to build a statically linked application that could use dynamically loadable plugins.
The problem is that I can't get rid of the libc.so dependency.
I'm using musl libc and compiling like
/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -Wl,-E -fPIC -I... -static-libgcc -Wl,-Bstatic -ldl -lc -lgcc source.c -o output_bin foo.a bar.a -Wl,-Bdynamic
readelf -d shows that executable depends on libc.so, so executable doesn't work on other machines without musl libc.
Is it possible to include libc symbols into elf executable and link all external plugins to elf itself, without external .so dependencies? How to achieve this?
Maybe you should try compiling it without the stdlib using -nostdlib parameter.

gcc/clang library compiling, include other library

If you would create a library with c, that depends on another library, say cURL, you would compile it to a static library like this:
clang myLibrary.c -lcurl -c
ar myLibrary.a myLibrary.o
But, if you were to include this library in your application, you would get an error because some functions were not found (the cURL ones).
Is there a way to include libraries (in this case cURL) in the library at compile time?
I'm working on a linux machine

Static linking libcurl using c

I am using Libcurl in my application with C and GNU compiler on a linux machine.
Application also uses OpenSSL libraries and some other libraries. I am trying to statically link the libraries, except for lcurl linking with other libraries works fine.
I am trying to compile as below.
gcc -static -I. -o test test.c -lz -lssl -lcrypto -lcurl
Am I doing anything wrong?
Is there a way to force static linking of some librairies (libc for exemple) ?
Libcurl is itself linked against a ton of other libraries, most of which aren't included in your compile command line. For instance, mine (on Debian Squeeze) links against:
libc
libcom_err
libcrypto
libdl
libgcrypt
libgnutls
libgpg-error
libgssapi_krb5
libidn
libk5crypto
libkeyutils
libkrb5
libkrb5support
liblber-2.4
libldap_r-2.4
libpthread
libresolv
librt
libsasl2
libssh2
libssl
libtasn1
libz
(You can get a similar list for yourself by running ldd on the library on Linux, or otool -L on Darwin.)

Resources