Dominick Navarro
Dominick Navarro

Reputation: 752

swig and libcurl together

I am learning to make PHP extensions and I came upon a blog post about swig. I tried to create a code with libcurl but I cannot compile it.

%{
    #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);

After running the following commands I haven't encountered any errors

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

but when I try to run the php file, I have an error saying:

undefined symbol: curl_easy_perform in Unknown line 0

Upvotes: 0

Views: 130

Answers (1)

Your linker augments are wrong. You call g++ with -lcurl -lcurlpp at the point where your are calling it with -c.

You need to use -fpic -lcurl -lcurlpp when you call it with -shared and -o file.so.

I.e the final linking should be:

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

Upvotes: 1

Related Questions