Mr Jerry
Mr Jerry

Reputation: 1764

Php exntension: undefined symbol: curl_easy_setopt in Unknown on line 0

i am writing a php extension, when i run, i got an error:

undefined symbol: curl_easy_setopt in Unknown on line 0

this is my function included curl

std::string MyLib::download_file(std::string url) {
    std::string buffer;
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
        curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 0L);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
        curl_easy_setopt(curl, CURLOPT_HEADER, 0L);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            return "";
        }
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    return buffer;
}

in header file:

#include <curl/curl.h>

please help me

**This is compiler command and curl version

g++ -Wall -c -O2 -std=c++11 -fpic `php-config --includes`  -L/usr/lib64/libcurl.so.4 -lcurl  -o  main.o main.cpp

i tried with:

 g++ -Wall -c -O2 -std=c++11 -fpic `php-config --includes`  -L/usr/lib64 -lcurl  -o  main.o main.cpp

but it is same

ldd:

[root@extension]# ldd myextension.so
linux-vdso.so.1 =>  (0x00007fff550b2000)
libphpcpp.so => /usr/lib/libphpcpp.so (0x00007f6b375b1000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f6b372aa000)
libm.so.6 => /lib64/libm.so.6 (0x00007f6b37026000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f6b36e10000)
libc.so.6 => /lib64/libc.so.6 (0x00007f6b36a7b000)
/lib64/ld-linux-x86-64.so.2 (0x0000003b62e00000)

Curl version:

[root@extension]# curl-config --version
libcurl 7.19.7

Upvotes: 0

Views: 871

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You are writing a c++ program, and using a c library just add this

extern "C" {
    #include <curl/curl.h>
}

in c++ function names are mangled to reflect the arguments they take, so it makes it possible to overload functions, the libcurl.so with extern "C" you force the compiler to use c linkage, and hence prevent name mangling.

You also need to link the resulting binary with libcurl for that you need to add

-lcurl

to the compiler command.

Edit: I notice that you have this

-L/usr/lib64/libcurl.so.4 -lcurl 

it's wrong, make it right with

-L/usr/lib64 -lcurl
  • -L You specify the search path for libraries.
  • -l You specify the soname, which will be found at the path specified with -L.

Upvotes: 3

Related Questions