C++ 的库路径。失败?

发布于 2024-12-10 12:55:59 字数 975 浏览 0 评论 0原文

我正在尝试在 Mac 上执行此代码。我已经安装了卷曲。当我搜索curl时,我可以在/usr/include/curl/curl.h下找到它。下面是我想要运行的程序。取自此处

#include <iostream>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

当我尝试编译程序时。见下文。

g++ simple.cpp 
Undefined symbols:
  "_curl_easy_perform", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_init", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_setopt", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_cleanup", referenced from:
      _main in cciFNPkt.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

这是怎么回事?为什么程序找不到该文件的路径?

我该如何修复它?

Im trying to execute this code on a mac. I have installed the curl. When i search for curl, i can find it under /usr/include/curl/curl.h. Below is the program i want to run. Taken from here.

#include <iostream>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

when i try to compile the program. see below.

g++ simple.cpp 
Undefined symbols:
  "_curl_easy_perform", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_init", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_setopt", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_cleanup", referenced from:
      _main in cciFNPkt.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

What s going on? why doesnt the program finds the path of this file?

How can i fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-12-17 12:55:59

您忘记链接 libcurl 库:

g++ simple.cpp -lcurl

这样的头文件(通常)仅声明一组函数存在,但不包含实际函数。对于 OS X Lion 上的curl,这些函数位于 libcurl 中(/usr/lib/libcurl.dylib,它指向 /usr/lib/libcurl.4.dylib)。您需要告诉链接器(通过编译器前端)该库的信息,这可以通过 -lcurl 来完成。

You’ve forgotten to link the libcurl library:

g++ simple.cpp -lcurl

A header file such as <curl/curl.h> (typically) only declares that a group of functions exist but does not contain the actual functions. In the case of curl on OS X Lion, those functions are in libcurl (/usr/lib/libcurl.dylib, which points to /usr/lib/libcurl.4.dylib). You need to tell the linker (via the compiler frontend) about that library, which can be done via -lcurl.

追星践月 2024-12-17 12:55:59

程序确实找到了包含文件,但没有找到库文件。
库文件应该类似于 libcurl.dylib 或 libcurl.a
使用它来链接它:

g++ simple.cpp -lcurl

The program does find the include file but not the library file.
The library file should be something like libcurl.dylib or libcurl.a
Use this to link with it:

g++ simple.cpp -lcurl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文