0

I've found this thread while looking for a way to download a URL to my pc: Download file using libcurl in C/C++

I downloaded curl for windows and merged all 3 folders to their existing counterparts in the MinGW directory: include, lib, and bin.

Now, whenever I compile the code, I got the following errors that I know they are due to not linking the proper libraries, but in the link I provided their is no sufficient info on what to link:

obj\Debug\main.o
In function main':
D:\CPP Scrap\curl1\main.cpp:11: undefined reference to __imp__curl_easy_init'
D:\CPP Scrap\curl1\main.cpp:13: undefined reference to __imp__curl_easy_setopt'
D:\CPP Scrap\curl1\main.cpp:14: undefined reference to __imp__curl_easy_perform'
D:\CPP Scrap\curl1\main.cpp:17: undefined reference to __imp__curl_easy_cleanup'
=== Build finished: 4 errors, 0 warnings ===

It shows that the header curl/types.h is being included but I cannot find it in the [latest] version of curl I downloaded, so I'm guessing I have to link with something, question is, what is it?

Code:

#define CURL_STATICLIB 
#include <stdio.h> 
#include <curl/curl.h> 
#include <curl/types.h> 
#include <curl/easy.h> 
#include <string> 

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { 
    size_t written; 
    written = fwrite(ptr, size, nmemb, stream); 
    return written; 
} 

int main(void) { 
    CURL *curl; 
    FILE *fp; 
    CURLcode res; 
    char *url = "http://localhost/aaa.txt"; 
    char outfilename[FILENAME_MAX] = "C:\\bbb.txt"; 
    curl = curl_easy_init(); 
    if (curl) { 
        fp = fopen(outfilename,"wb"); 
        curl_easy_setopt(curl, CURLOPT_URL, url); 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); 
        res = curl_easy_perform(curl); 
        curl_easy_cleanup(curl); 
        fclose(fp); 
    } 
    return 0; 
}
Community
  • 1
  • 1
ToniAz
  • 430
  • 1
  • 6
  • 24
  • Did you mean to show us the error messages? Looks like you forgot to add them. – Ernest Friedman-Hill Oct 24 '11 at 17:12
  • I'll guess you're getting undefined symbol errors, because you're missing `curllib.lib`. Does your download have these files? Have you set up your compilation / IDE to link with that file? – wkl Oct 24 '11 at 17:22
  • @birryree I downloaded the curl files in a zip from their official website. I do not have **curllib.lib**, but I do have **libcurl.a**, and linking with it doesn't eliminate the errors. Note, the curl files are also missing **curl/types.h** – ToniAz Oct 25 '11 at 06:54
  • @ErnestFriedman-Hill These are the errors: ` obj\Debug\main.o||In function `main':| D:\CPP Scrap\curl1\main.cpp|11|undefined reference to `__imp__curl_easy_init'| D:\CPP Scrap\curl1\main.cpp|13|undefined reference to `__imp__curl_easy_setopt'| D:\CPP Scrap\curl1\main.cpp|14|undefined reference to `__imp__curl_easy_perform'| D:\CPP Scrap\curl1\main.cpp|17|undefined reference to `__imp__curl_easy_cleanup'| ||=== Build finished: 4 errors, 0 warnings ===| ` – ToniAz Oct 25 '11 at 06:55

1 Answers1

0

I don't know the details for Windows (I never used that system, but I am using GNU/Linux since 1993 with happiness), but you probably need to link the libcurl library (maybe using files like libcurl.lib and/or libcurl.dll or something similar).

But did you consider to use GNU/Linux as your development platform? If you want to use C++ & Curl, it makes a lot of sense (because Linux has a good free C++ compiler, GCC, and because libcurl is probably developed mostly for and on Linux). And the ability to glance into all the source code of your system enables you to learn a lot of things.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks for asnwering. I am actually using the GNU minimcal compiler for Windows operating systems (MinGW ofcourse). So I don't think there must be much of a difference. I am linking to **libcurl.a**, but not to any dll. It still doesn't work, gives me an undefined error to the **curl_easy** functions – ToniAz Oct 25 '11 at 06:48