1

I am new to programming in c++. I am trying to compile a code using curl in it. My visual studio code is able to find the file (as it autocompletes as well), but when I run "build task g++" it says the following:

C:\Users\matth\AppData\Local\Temp\cceVTTmY.o:test.cpp:(.text+0xf): undefined reference to `_imp__curl_easy_init'
C:\Users\matth\AppData\Local\Temp\cceVTTmY.o:test.cpp:(.text+0x21): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status

I don't get it. I installed curl with vcpkge and integrated it, so visual studio code can actually find it.

I used this in my args for tasks.json for g++:

"args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "C:\\Program Files\\vcpkg-master\\installed\\x86-windows\\include",
                "-L",
                "C:\\Program Files\\vcpkg-master\\installed\\x86-windows\\lib",
                "-static"

            ],

I have also tried adding '-lcurl' but then the cmd actually responds saying 'cant't find lcurl'. I am struggling for a lot of hours now (as I said, new to c++) and I am getting really frustrated.

This is the code I am trying to compile:

#define CURL_STATICLIB
#include <curl/curl.h>

int main()
{
    CURL *curl;

    curl = curl_easy_init();
    curl_easy_cleanup(curl);

    return 0;
}

Anyone that knows how to fix this problem? All solutions I could find didn't work, so I decided to make my own account and try it this way...

Thanks a lot in advance, if you need more info, tell me! :)

matzzz
  • 11
  • 1
  • you forget to add the curl lib to the arguments `-lcurl` – rioV8 Jan 24 '21 at 01:20
  • @rioV8 what do you mean? As I mentioned if I add "-lcurl" to the arguments, it says it cannot find -lcurl. Or do i have to do something different? – matzzz Jan 24 '21 at 01:35
  • then you have to find the correct directory where `libcurl` is, and add this also to the argument list with `-L` – rioV8 Jan 24 '21 at 04:27
  • The thing is, libcurl.lib is in that directory, but still cant find it. Man libcurl is really being annoying right now hahaha. – matzzz Jan 24 '21 at 10:35
  • you are using `g++` with Visual Studio libraries. That will not work. Search for `libcurl` that belongs to g++ – rioV8 Jan 24 '21 at 12:46

1 Answers1

1

summary: you are missing lcurl flag in args;

To answer the question for future readers.

Environment: Windows 11+ (but may work on older systems), using Visual Studio code, and C++ compiler such as g++ there are a few tasks to perform.

  1. Create your curl code as per below including the curl header such as below curl.cpp example file.
#include <iostream>
#include <curl/curl.h>
using namespace std;
int main() 
{
  CURL *curl;
  CURLcode res;
  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  if (curl) 
  {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");

    // Important. Read on first.
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // Disable SSL certificate verification
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // Disable hostname verification

    // this is the actual Curl command.
    res = curl_easy_perform(curl);

    if (res == CURLE_OK) 
    {
        // to test if Curl worked. If you get no output then run the program.exe with cin.get() for results.
        cout << "Curl worked" << endl;
    }
    else {
        cout << "Error: curl failed: curl_easy_perform() returned error: " << curl_easy_strerror(res) << endl;
    }
    
    cin.get();
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  cin.get();
  return 0;
}
  1. Save as any name e.g. test.cpp in a location such as C:/documents/test_program/test.cpp

  2. Download curl from the official curl website https://curl.se/download.html

  3. Extract folder in the same location as the code in test_program e.g. "C:/documents/test_program/curl there should then be 2 items in top directory "C:/documents/test_program/curl "C:/documents/test_program/test.cpp

  4. Make an windows user or system PATH environmental variable that points to the curl/bin folder e.g.,: "C:/users/indira/documents/test_program/curl/bin"

  5. Build file and select your compiler e.g. g++. Code will fail as it's not configured correctly yet. You may be prompted and directed to a new tasks.json file being created, however if it doesn't automatically create follow steps 6 and 7. below, otherwise skip to step 8. if you have tasks.json already.

  6. In VScode click on "view" - "command palate" or enter keys CTRL + SHIFT + P

  7. Then search and click on "tasks:configure tasks" - "Create tasks.json file from template" this will then create a tasks .json file.

  8. Open your tasks.json file and in the "args" parameter enter /* curl files below for curling websites

            -I. = Include xxx (for headers)
            -L. = Link library directory
            -l = link library itself
    
             e.g.
    
            "-I./curl/include",
            "-L./curl/lib",
            "-lcurl",
    
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I${fileDirname}",
                /* curl files below for curling websites
                -I. = Include xxx (for headers)
                -L. = Link library directory
                -l = link library itself
                */
                "-I./curl/include",
                "-L./curl/lib",
                "-lcurl"                
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
  1. Save and build, software. You will now have no missing curl header, or missing library errors. If you get include errors remember you have to point to curl.h so it could be for you curl/include/curl/curl.h or curl/curl.h whatever option you pick add to #include <> and also in tasks.json
  2. If you compile at command line/console, you may not get any output. Instead run the .exe file directly to observe results with a cin.get() to pause.