G
G
Gregory2015-08-19 19:48:58
linux
Gregory, 2015-08-19 19:48:58

How to build a static binary with libcurl?

Good day.
There is the following code:

#include <stdio.h>
#include <curl/curl.h>

int main(void){
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();

  if(curl == NULL)
    return 0;

  curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/getAccessAttributes?id=1");

  res = curl_easy_perform(curl);

  if(res != CURLE_OK) {
    fprintf(stderr, "Error: %s\n", curl_easy_strerror(res));
  	return 0;
  }

  curl_easy_cleanup(curl);
  return 0;

}

Everything works great if you build it with dynamically linked libraries:
gcc test.c -lcurl -o test
Now I want to build a static program:
gcc test.c /tmp/curl/lib/libcurl.a -static -lcurl -lssl -lcrypto -ldl -lm -lz -DCURL_STATICLIB -I/tmp/curl/include -o test

Assembled with a creak:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
(.text+0x11 ): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/curl/lib/libcurl.a(libcurl_la-netrc.o): In function `Curl_parsenetrc':
netrc .c:(.text+0x3c8): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/curl/lib/libcurl.a(libcurl_la-curl_addrinfo.o) : In function `Curl_getaddrinfo_ex':
curl_addrinfo.c:(.text+0x60): warning: Using 'getaddrinfo'in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

But it only works if you specify not example.com as a host, but an ip address.
Otherwise, the program cannot resolve the domain name:
$ ./test
Error: Couldn't resolve host name

How can this problem be overcome?
Thank you!
Z.Y.
I assembled the static libcurl in advance using the following method:
wget http://curl.haxx.se/download/curl-7.44.0.tar.lzma
tar xf curl-7.44.0.tar.lzma
cd curl-7.44.0
./configure --disable-shared --enable-static --prefix=/tmp/curl --disable-ldap --disable-sspi --without-librtmp --disable-ftp --disable-file --disable-dict --disable-telnet --disable-tftp --disable-rtsp --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-smb --without-libidn
make && make install

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory, 2015-08-20
@ximik777

libcurl needs to be configured with "--enable-ares" option.
The program will then get the gethostbyname function and be able to resolve the domain name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question