我似乎无法将 g++ 链接到我安装的 libcurl

问题描述 投票:0回答:2

我是 C++ 编程新手。我正在尝试使用curl 来编译代码。我的 Visual Studio 代码能够找到该文件(因为它也会自动完成),但是当我运行“构建任务 g++”时,它会显示以下内容:

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

我不明白。我用vcpkge安装了curl并集成了它,所以Visual Studio Code实际上可以找到它。

我在 g++ 的tasks.json 参数中使用了它:

"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"

            ],

我也尝试添加“-lcurl”,但 cmd 实际上响应说“找不到 lcurl”。 我现在挣扎了很多个小时(正如我所说,C++ 新手),我真的很沮丧。

这是我正在尝试编译的代码:

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

int main()
{
    CURL *curl;

    curl = curl_easy_init();
    curl_easy_cleanup(curl);

    return 0;
}

谁知道如何解决这个问题?我能找到的所有解决方案都不起作用,所以我决定创建自己的帐户并尝试这种方式......

提前非常感谢,如果您需要更多信息,请告诉我! :)

c++ curl visual-studio-code g++ vcpkg
2个回答
1
投票

总结:您在参数中缺少 lcurl 标志;

为未来的读者回答这个问题。

环境:Windows 11+(但可能适用于较旧的系统),使用 Visual Studio 代码和 C++ 编译器(例如 g++),有一些任务需要执行。

  1. 按照如下方式创建curl代码,包括curl标头,例如下面的curl.cpp示例文件。
#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. 另存为任何名称,例如test.cpp 位于 C:/documents/test_program/test.cpp 等位置

  2. 从curl官方网站下载curlhttps://curl.se/download.html

  3. 将文件夹解压到与 test_program 中的代码相同的位置,例如“C:/documents/test_program/curl 顶层目录中应该有 2 项 “C:/documents/test_program/curl “C:/documents/test_program/test.cpp

  4. 创建一个指向curl/bin文件夹的Windows用户或系统PATH环境变量,例如:“C:/users/indira/documents/test_program/curl/bin”

  5. 构建文件并选择您的编译器,例如g++。代码将失败,因为它尚未正确配置。系统可能会提示您并定向到正在创建的新tasks.json文件,但是如果它没有自动创建,请执行以下步骤 下面的 6 和 7.,否则跳到步骤 8。如果您已经有tasks.json。

  6. 在 VScode 中点击“视图”-“命令面板”或输入键 CTRL + SHIFT + P

  7. 然后搜索并单击“任务:配置任务”-“从模板创建tasks.json文件”,这将创建一个tasks .json文件。

  8. 打开tasks.json 文件并在“args”参数中输入 /* 下面的curl 文件用于curl 网站

            -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. 保存并构建软件。现在,您将不会丢失curl标头或丢失库错误。如果您遇到包含错误,请记住您必须指向curl.h,这样它可能适合您 卷曲/包括/卷曲/curl.h 或者 卷曲/卷曲.h 无论您选择什么选项,都添加到#include <>以及tasks.json中
  2. 如果您在命令行/控制台编译,您可能不会得到任何输出。相反,直接运行 .exe 文件来观察结果,并使用 cin.get() 暂停。

0
投票

我被同样的问题困扰了将近 1 个月(我当然不是唯一的一个,因为有很多关于这个主题的帖子)。 我不是专业人士,所以我只会告诉你什么配置对我有用。 您已经声明您的curl 安装成功,所以让我们跳过这一部分。 在你的tasks.json中,在 "args": [ 内,你应该有这样的行:

"args":[

            "-I",
            "C:\\(path of curl you have used to installation)\\include",
            "-L",
            "C:\\(path of curl you have used to installation)\\lib",
            "-lcurl",
            "-mwindows",
            "-lgdi32",
            "-municode"

此外,您必须在代码中写入以下句子:

#define CURL_STATICLIB

试试这个,因为它就是这样。让我知道情况如何。希望它也适合你。 ;)

© www.soinside.com 2019 - 2024. All rights reserved.