【问题标题】:How to properly install libcurl and link it to a c++ program?如何正确安装 libcurl 并将其链接到 c++ 程序?
【发布时间】:2012-07-24 07:04:59
【问题描述】:

我一直在尝试让 C++ 程序使用 libcurl,但无法弄清楚。在使用 C++ 开发时,我通常使用 Visual Studio,但这个项目使用 vi 一个 ssh 会话到使用 VI 和 g++ 的 centos 机器。我已经运行了 yum install curl、yum install libcurl、yuminstall curl-devel 和 yum install libcurl-devel,但仍然无法编译程序。

API 上的文档非常好,我可以找到正确安装 libcurl 后如何使用它的信息,但是安装它被证明是一件痛苦的事情。

代码是:

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


string data; //will hold the urls contents

size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{ //callback must have this declaration
    //buf is a pointer to the data that curl has for us
    //size*nmemb is the size of the buffer

    for (int c = 0; c<size*nmemb; c++)
    {
        data.push_back(buf[c]);
    }
    return size*nmemb; //tell curl how many bytes we handled
}

int main(void) {

 CURL* curl;

    curl_global_init(CURL_GLOBAL_ALL);
    curl=curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_URL, "https://domain.com");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");

    curl_easy_perform(curl);

    cout << endl << data << endl;
    cin.get();

    curl_easy_cleanup(curl);
    curl_global_cleanup();


    return 0;
}

我在尝试编译时收到以下错误:

/tmp/ccfeybih.o: In function `main':
helloworld.cpp:(.text+0x72): undefined reference to `curl_global_init'
helloworld.cpp:(.text+0x77): undefined reference to `curl_easy_init'
helloworld.cpp:(.text+0x96): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xb1): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xcc): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xe7): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xf3): undefined reference to `curl_easy_perform'
helloworld.cpp:(.text+0x132): undefined reference to `curl_easy_cleanup'
helloworld.cpp:(.text+0x137): undefined reference to `curl_global_cleanup'
collect2: ld returned 1 exit status

我找不到从这里去的地方。

【问题讨论】:

  • 您需要在 g++ 调用选项中添加链接标志。像g++ ... -lcurl 这样的东西。或者您可以为您的项目创建 makefile。
  • 你会知道任何关于如何做到这一点的好教程吗?
  • 例如,来自 google 的第一个 link
  • 非常感谢您的提问,我也遇到了同样的问题,我一直在寻找解决方案。

标签: c++ libcurl


【解决方案1】:

您遇到的错误是链接器错误。链接器无法找到 curl 库。 您需要指定链接器在链接时搜索适当库的路径。

在这种情况下(如果您在标准 lib 目录中安装了 lib curl,例如 /usr/lib、r /usr/local/lib),以下应该可以工作:

g++ you_file_name.cpp -lcurl

否则,您必须指定可以找到库的目录的路径。 例如:

g++ -L/curl/lib/dir -lcurl you_file_name.cpp.

当有多个库要链接时,这些事情会变得复杂,因此最好使用 CMake 等构建系统来协助管理包含目录/库路径和其他各种事情。

【讨论】:

  • 非常感谢您的回答,我遇到了同样的问题,我一直在寻找解决方案。
【解决方案2】:

针对 libcurl 构建的正确方法是使用 pkg-config(就像许多现代软件包一样)。如果是直接g++ 呼叫,则为:

g++ $(pkg-config --cflags libcurl) helloworld.cpp $(pkg-config --libs libcurl)

这将自动处理库和头文件不在系统包含/库目录中的非常见(但仍然正确)的 libcurl 安装。如果没有 pkg​​-config,您将需要始终自己找出正确的路径。


如果你开始使用自动工具,你会使用configure.ac:

PKG_CHECK_MODULES([CURL], [libcurl])

然后在Makefile.am:

helloworld_CPPFLAGS = $(CURL_CFLAGS)
helloworld_LIBS = $(CURL_LIBS)

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2021-12-06
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多