0

I'm trying to compile this code in Codeblocks 16.01 on Ubuntu but it returns error message with undefined reference to 'curl_easy_init'.

But when I run in terminal gcc -L/usr/lib/x86_64-linux-gnu main.c -o curl -lcurl doesn't return any errors.

How can I fix this?

#include <stdio.h>
#include <curl/curl.h>

void fileUpload()
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL,"http://test1:test1@www.idehn.tec.ac.cr/geoserver/rest/layers.xml");
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        FILE* file = fopen( "layers.txt", "w");

        curl_easy_setopt( curl, CURLOPT_WRITEDATA, file) ;

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);

        fclose(file);
    }
}


int main() {

    //Call to the method that charge the url content to a file with all the layers.
    fileUpload();
    return 0;
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
Ryon94
  • 1

1 Answers1

0

I think you've forgot to tell codeblocks to link with libcurl:

  • Build options...
  • Linker settings
  • Add (button)
  • Enter curl

See it in images here : https://stackoverflow.com/a/5881751/1212012

Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Thank you, finally I make it work by adding -lcurl on Linker Setting -> Other Linker options – Ryon94 Sep 15 '17 at 18:24