isinstance
isinstance
采纳率25%
2016-06-20 13:11 阅读 2.8k
已采纳

C语言写python扩展的时候,undefined symbol: curl_easy_perform

10

今天又写来一个扩展
然后是一个使用来libcurl包的C模块,想用这个模块替代不稳定的python httplib,然后编译啥的也没有问题,可是最后import进去的时候
报错了
undefined symbol: curl_easy_perform
这是C的源代码

#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include <python2.7/Python.h>

size_t memory_callback(void *data, size_t size, size_t nmemb, void *content)
{
    size_t realsize = size *nmemb;
    char *p = *(char **)content;
    size_t len = p ? strlen(p) : 0;
    *(char **)content = realloc(p, len + realsize + 1);
    p = *(char **)content;
    memcpy(p + len, data, realsize);
    p[len + realsize] = '\0';

    return realsize;
}

size_t get_content(char *appid, char *secret_key, char *q, char *from ,char *to, char **content)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl)
  {
    char myurl[1000] = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
    char salt[60];
    int a = rand();
    sprintf(salt,"%d",a);
    char sign[120] = "";
    strcat(sign,appid);
    strcat(sign,q);
    strcat(sign,salt);
    strcat(sign,secret_key);
    printf("%s\n",sign);
    unsigned char md[16];
    int i;
    char tmp[3]={'\0'},buf[33]={'\0'};

    MD5((const unsigned char *)sign, strlen((const char *)sign), md);

    for (i = 0; i < 16; i++)
    {
        sprintf(tmp,"%2.2x",md[i]);
        strcat(buf,tmp);
    }
    printf("%s\n",buf);
    strcat(myurl,"appid=");
    strcat(myurl,appid);
    strcat(myurl,"&q=");
    strcat(myurl,q);
    strcat(myurl,"&from=");
    strcat(myurl,from);
    strcat(myurl,"&to=");
    strcat(myurl,to);
    strcat(myurl,"&salt=");
    strcat(myurl,salt);
    strcat(myurl,"&sign=");
    strcat(myurl,buf);
    printf("%s\n",myurl);

    /* always cleanup */
    curl_easy_setopt(curl, CURLOPT_URL, myurl);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memory_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)content);

    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
    curl_easy_cleanup(curl);
    }
    return 0;
}

static PyObject *xhttp(PyObject *self, PyObject *args)
{
    char *content = NULL;
    char *appid;
    char * secret_key;
    char *q;
    char *from;
    char *to;
    if(!PyArg_ParseTuple(args, "s|ssss", &appid, &secret_key, &q, &from, &to))
    {
        return NULL;
    }
    get_content(appid, secret_key, q, from, to, &content);

    return Py_BuildValue("s", content);
    if(content) free(content);
}

static PyMethodDef httpMethods[] =
{
    {"xhttp", (PyCFunction)xhttp, METH_VARARGS},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initxhttp(void)
{
    Py_InitModule("xhttp", httpMethods);
}

然后用来

python ./setup.py build

不行

gcc -fpic -lcurl -lssl -c -l /usr/include/python2.7 -l /usr/lib/python2.7/config xhttp.c 
gcc -share -o xhttp.so xhttp.o

最后是可以生成build目录和.o .so文件,但是就是从python shell 中import 进去的时候,会报错

大神下班顺便回答个问题呗~~~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    isinstance isinstance 2016-06-21 02:56

    好吧,我自己回答自己,是这样的
    指定没有对库文件
    正确的应该所这样

    gcc -shared -I/usr/include/python2.7 -fPIC xhttp.c -lcurl -lssl -pthread -o xhttp.so
    
    点赞 评论 复制链接分享
  • oyljerry oyljerry 2016-06-20 13:32

    先看看你的编译的so文件依赖是否都正确 python可能加载的时候找不到libcurl的库文件

    点赞 评论 复制链接分享

相关推荐