0

I am very new to C++ and I was searching around how to download a file in C++ using cURL. I basically found this code:

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.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);
        curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

but everytime I run it I get:

/usr/bin/ld: CMakeFiles/a.run.dir/main.cpp.o: in function `main':
/home/kroyz/Dokumente/code/main.cpp:101: undefined reference to `curl_easy_init'
/usr/bin/ld: /home/kroyz/Dokumente/code/main.cpp:104: undefined reference to `curl_easy_setopt'
/usr/bin/ld: /home/kroyz/Dokumente/code/main.cpp:105: undefined reference to `curl_easy_setopt'
/usr/bin/ld: /home/kroyz/Dokumente/code/main.cpp:106: undefined reference to `curl_easy_setopt'
/usr/bin/ld: /home/kroyz/Dokumente/code/main.cpp:107: undefined reference to `curl_easy_perform'
/usr/bin/ld: /home/kroyz/Dokumente/code/main.cpp:108: undefined reference to `curl_easy_cleanup'

(Please correct me if I'm using any wrong tags)

Thanks in advance, Kroyz

Kroyz
  • 21
  • 2
  • Are you linking the library when you're trying to run the code? – Ryan Zhang Aug 09 '22 at 12:09
  • Bad news: the shown code is not C++. – Sam Varshavchik Aug 09 '22 at 12:11
  • "everytime I run it I get: ..." No, you don't run this code as long as the executable is not built. –  Aug 09 '22 at 12:13
  • @SamVarshavchik OP is using `size_t` and `#include`, so they're using C++. – Jason Aug 09 '22 at 12:13
  • @SamVarshavchik: bad news, `#include ` is C++. –  Aug 09 '22 at 12:15
  • No self-respecting C++ compiler will assign a string literal to a `char *`. This is not valid C++. – Sam Varshavchik Aug 09 '22 at 12:20
  • @SamVarshavchik it's a warning (`-Wwrite-strings`), but it is still compilable. Just tested it on `g++` with `--std=c++17`, ran fine (albeit not ideal). It is C++, just not ISO C++. – Rogue Aug 09 '22 at 12:24
  • `g++` also compiles variable-length arrays. Which are not C++ either, @rogue. – Sam Varshavchik Aug 09 '22 at 12:26
  • 1
    I'm not trying to have an argument, but simply demonstrating that the whole discussion is a little bit off-point for the sake of OP's issue. There may be other issues present, but they're not at the heart of his issue (I believe a lack of a `-dev` lib for OP to work against, as `curl_easy_init` is not in the common distribution for `curl/curl.h`) – Rogue Aug 09 '22 at 12:35

0 Answers0