一起喝酒和libcurl

swig and libcurl together

本文关键字:libcurl 喝酒 一起      更新时间:2023-10-16

我正在学习制作PHP扩展,我看到了一篇关于swig的博客文章。我试着用libcurl创建一个代码,但是我无法编译它。

%{
    #include <stdio.h>
    #include <curl/curl.h>
    bool wsper(char* url, char* postdata){
        CURL* curl = curl_easy_init();
        CURLcode res;
        if(curl){
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
            res = curl_easy_perform(curl);
            if(res == CURLE_OK){
                curl_easy_cleanup(curl);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
%}
%module wsper
extern bool wsper(char* url, char* postdata);

运行以下命令后,我没有遇到任何错误

swig -php file.c
g++ `php-config --includes` -fpic -lcurl -lcurlpp -c
wsper_wrap.c g++ -shared file_wrap.o -o file.so

但是当我尝试运行php文件时,我有一个错误说:

未定义符号:curl_easy_perform在未知行0

您的链接扩展错误。在使用-c调用g++的地方使用-lcurl -lcurlpp。

当您使用-shared和-o file.so调用它时,您需要使用-fpic -lcurl -lcurlpp。

。最后的链接应该是:

g++ -shared file_wrap.o -o file.so  -lcurl -lcurlpp