本文介绍了如何使用静态libcurl编译程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下代码:

#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 fine if you collect it from the dynamic link library:

gcc test.c -lcurl -o test

现在我想提出一个静态程序:

Now I want to raise 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

他走错了路

仅当您指定主机IP地址而不是example.com时,该程序才有效.否则,程序将无法解析域名:

The program works only if you specify a host ip address and not example.com.Otherwise the program can not resolve the domain name:

$ ./test
Error: Couldn't resolve host name

如何解决名称解析问题?谢谢!

How can I solve the problem of name resolution? Thanks!

P.S.静态libcurl我收集了以下方法:

P.S.Static libcurl I collected 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

推荐答案

动态libcurl的外观如下.

This is how it looks with dynamic libcurl.

$ ldd test
    linux-vdso.so.1 (0x00007ffcca3aa000)
    libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x00007f5536ed6000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f5536b3e000)
    libssl.so.1.0.0 => /usr/lib64/libssl.so.1.0.0 (0x00007f55368d3000)
    libcrypto.so.1.0.0 => /usr/lib64/libcrypto.so.1.0.0 (0x00007f5536503000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f55362ed000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f5537138000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f55360e9000)

如果只想静态链接curl,请不要使用-static.只需将libcurl.a添加为目标文件之一或使用-lcurl,就无需指定两次.之后

If you want to link only curl statically, do not use -static. Just add libcurl.a as one of the objects files or use -lcurl, you don't need to specify it twice.After

gcc test.c curl-7.44.0/lib/.libs/libcurl.a -lssl -lcrypto -ldl -lm -lz -DCURL_STATICLIB -o test

你会得到

$ ldd test
    linux-vdso.so.1 (0x00007ffd9a0a7000)
    libssl.so.1.0.0 => /usr/lib64/libssl.so.1.0.0 (0x00007f947cc7e000)
    libcrypto.so.1.0.0 => /usr/lib64/libcrypto.so.1.0.0 (0x00007f947c8ae000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f947c6aa000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f947c3b1000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f947c19b000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f947be03000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f947cee9000)

-static表示您要全部静态链接.然后,您需要将所有库准备为静态,结果将是:

-static means that you want link all statically. Then you need all libraries prepared as static and the result will be:

$ ldd test
    not a dynamic executable

这篇关于如何使用静态libcurl编译程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:10
查看更多