动态链接LibCURL时出错

时间:2014-11-13 21:03:20

标签: c curl libcurl dlopen dlsym

我正在尝试动态链接libcurl,但我在easy_performing时遇到了Segfault。
以下是全球范围声明:

#define LIBCURL_PATH_64      "/usr/lib64/libcurl.so"
void *hLibCurl = NULL;
CURL *curl;
CURL* (*curl_easy_init)(void);
CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
CURLcode (*curl_easy_perform)(CURL *);
const char* (*curl_easy_strerror)(CURLcode);
struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);

然后我动态链接它:

  hLibCurl = dlopen(LIBCURL_PATH_64, RTLD_NOW);
  *(void**) (&curl_easy_init) = dlsym(hLibCurl, "curl_easy_init");
  *(void**) (&curl_slist_append) = dlsym(hLibCurl, "curl_slist_append");
  *(void**) (&curl_easy_setopt) = dlsym(hLibCurl, "curl_easy_setopt");
  *(void**) (&curl_easy_strerror) = dlsym(hLibCurl, "curl_easy_strerror");
  *(void**) (&curl_easy_perform) = dlsym(hLibCurl, "curl_easy_perform");
  if ( (curl = (*curl_easy_init)()) == NULL ){
    LogMsg("CurlInit easy_init failed");
    return -1;
  }

在这一点上它不会退出或失败 所以这是节目开始的地方。设置像这样的curlopts时,我没有错误:

  if ((*curl_easy_setopt)(curl, CURLOPT_POST, 1) != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_POST, 1) failed");
    return -1;
  }
  if ((*curl_easy_setopt)(curl, CURLOPT_URL, "https://ws.service.com.br/services-nac/services/SomeService?wsdl") != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_URL) failed");
    return -1;
  }
  if ((*curl_easy_setopt)(curl, CURLOPT_PORT, 443) != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_PORT, 443) failed");
    return -1;
  }

但是,当easy_performing我遇到了段错误时:

   if ( (res = (*curl_easy_perform)(curl)) != CURLE_OK ){
      return -1;
   }

我很确定该错误很容易执行,但我决定继续生成core_dump。 当我gdb这个核心时,我实际上在easy_init上出错:

  

程序因信号11,分段故障而终止。   来自curlapp的curl_easy_init()中的#0 0x00007f3d2f9cd3f8

任何人都知道这里可能出现什么问题?

提前致谢!


[编辑]

我删除了curl_easy_perform块,程序正常运行。 我不确定如果它因为curl_easy_perform()而失败,或者只是因为在这个函数中事情发生了。换句话说,它可能意味着'init'和'setopts'仅适用于easy_perform。 (这只是猜测)

[EDIT2]

更改了dlsym块:

  curl_easy_init = 
    (CURL* (*)(void)) 
      dlsym(hLibCurl, "curl_easy_init");
  curl_slist_append = 
    (struct curl_slist*(*)(struct curl_slist *, const char *)) 
      dlsym(hLibCurl, "curl_slist_append");
  curl_easy_setopt = 
    (CURLcode (*)(CURL *, CURLoption, ...))
      dlsym(hLibCurl, "curl_easy_setopt");
  curl_easy_perform = 
    (CURLcode (*)(CURL *))
      dlsym(hLibCurl, "curl_easy_perform");

在函数调用中:

 if ( curl_easy_setopt(curl, CURLOPT_POST, 1) != CURLE_OK) {
    if ( DEBUG_DETAILS ) vTrace("curl_easy_setopt(curl, CURLOPT_POST, 1) failed");
    return -1;
  }

但我仍然在curl_easy_init()获得Segfault 有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

首先加载并致电curl_global_init,如以下所述:http://curl.haxx.se/libcurl/c/curl_global_init.html