Question

I am new to C++ and am interested in writing a simple app to download an HTTP file from the internet and process the contents of it). Anyway, I found a library called libcurl online. I went to the following website:

http://curl.haxx.se/download.html

  1. Should I download the source archive or the binary package (Win64 MinGW64)? What is the difference and pros/cons between using source archive vs binary pkg?
  2. Should I use the curlcpp binding since I am writing a C++ app? How can I incorporate the curlcpp binding with this?

Anyway, I downloaded curl-7.34.0.zip (Source Archive) and inside, it contains many folders. I suspect that the folders that I would need to include are:

  • include
  • lib
  • src

I used CodeBlocks IDE. I "recursively added" the folder "include/curl" into my project. It looks like this:

enter image description here

My main.cpp contains the following code (taken from the sample app on the website):

#include <iostream>
#include <stdio.h>
#include "curl/curl.h"
using namespace std;

int main()
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        /* 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));

        /* always cleanup */
        curl_easy_cleanup(curl);

    }
    return 0;
}

I received the following error message when trying to build/compile.

undefined reference to '_imp__curl_easy_init'
undefined reference to '_imp__curl_easy_setopt'
undefined reference to '_imp__curl_easy_perform'
undefined reference to '_imp__curl_easy_strerror'
undefined reference to '_imp__curl_easy_cleanup'

I am not sure what I am missing or if my folder directory is being used incorrectly. Do you have any suggestion to help me build this via Source Archive or Binary Package? Thanks.

Était-ce utile?

La solution

There are several problems here:

  1. Never. ABSOLUTELY never add headers from library directly to your project. Copy them to your include catalog.
  2. #include <curl.h>: you use <> which means - search in include catalog. Try replacing it with: #include "curl.h" - which means search through project and include catalog.

But you should do nr. 1 -as this code is not part of your project. It is part of curl project.

EDIT As posted in comment - it depends where you actually put the headers. If you put them in include\curl directory, then use #include "curl/curl.h".

EDIT AFTER QUESTION EDIT You need to specify .lib references. When you use curl you don't compile it. It is already compiled by some genius guys from curl to the form of .dlls. Your program doesn't know how to use them unless you tell it how to. To do that you must change your linker options - by providing so called lib files. lib files are there to tell linker which .dlls it should use.

I assume you have your curl catalog. Apart from include there should also be lib. Now what you need to do is to tell linker that you need libcurl.a from lib catalog of curl. Go ahead: Project build options | Linker settings. In Link libraries section specify the path to libcurl.a.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top