如何在 linux 中将 libcurl 链接到我的 C++ 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6302282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:26:20  来源:igfitidea点击:

How do I link libcurl to my c++ program in linux?

c++linuxeclipselibcurl

提问by Austin Witherspoon

I need to use libcurl in a piece of software I am writing on my ubuntu machine. I am using Eclipse to write and compile all of the software. When I put the libcurl files in the same folder as the .cpp file, and include the curl.h file in the header, When I attempt to compile the program, It comes up with these errors:

我需要在我的 ubuntu 机器上编写的一个软件中使用 libcurl。我正在使用 Eclipse 编写和编译所有软件。当我将 libcurl 文件与 .cpp 文件放在同一个文件夹中,并在头文件中包含 curl.h 文件时,当我尝试编译程序时,会出现以下错误:

Building target: sms
Invoking: GCC C++ Linker
g++  -o"sms"  ./src/sms.o   
./src/sms.o: In function `main':
/home/geekman/workspace/sms/Debug/../src/sms.cpp:38: undefined reference to `curl_easy_init'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:42: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:44: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:46: undefined reference to `curl_easy_perform'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:47: undefined reference to `curl_easy_cleanup'
collect2: ld returned 1 exit status
make: *** [sms] Error 1

I took the contents of the include folder from libcurl, and placed them in the same folder as the .cpp file. then in the header of the .cpp file, I typed:

我从 libcurl 中取出 include 文件夹的内容,并将它们放在与 .cpp 文件相同的文件夹中。然后在 .cpp 文件的标题中,我输入:

#include <curl/curl.h>

I also tried:

我也试过:

#include "curl/curl.h"

Any ideas on the problem? Thanks.

关于这个问题的任何想法?谢谢。

采纳答案by Adam Rosenfield

Your header file inclusions are just fine; your problem is occurring at the linking step. In order to link against libcurl, you need to add the -lcurlcommand line option, assuming it's installed in a standard directory:

你的头文件包含就好了;您的问题发生在链接步骤。为了链接 libcurl,您需要添加-lcurl命令行选项,假设它安装在标准目录中:

g++ -o sms ./src/sms.o -lcurl

If it's not installed in a standard directory, you also need to add the -L/path/to/libcurl, e.g. something like:

如果它没有安装在标准目录中,您还需要添加-L/path/to/libcurl,例如:

# Assuming that /home/geekman/workspace/libcurl is where libcurl.a is located
g++ -o sms ./src/sms.o -L/home/geekman/workspace/libcurl -lcurl

Also note that the -lcurloption has to appear afterthe list of object files you're linking, otherwise it won't link properly.

另请注意,该-lcurl选项必须出现您链接的目标文件列表之后,否则将无法正确链接。

回答by ckruse

You have to link the library to your program. With gcc (and most other compilers) you can specify the libraries to link with -lname_wo_lib, e.g. -lcurl

您必须将库链接到您的程序。使用 gcc(和大多数其他编译器),您可以指定要链接的库-lname_wo_lib,例如-lcurl

回答by Sam

An alternate answer (the first one is excellent). Consider using the output returned by "pkg-config --libs libcurl" as an argument to your compiler.

另一个答案(第一个很好)。考虑使用“pkg-config --libs libcurl”返回的输出作为编译器的参数。

For example,

例如,

CPPFLAGS=`pkg-config --libs libcurl`

g++ $CPPFLAGS myfile.o

CPPFLAGS=`pkg-config --libs libcurl`

g++ $CPPFLAGS myfile.o

Pkg-config is a standard way for open source libraries to communicate to you how to link against them / #include their files.

Pkg-config 是开源库向您传达如何链接它们/#include 其文件的标准方式。

回答by Taha Yavuz Bodur

Also see GNU GCC Manual - Options for Linkingfor a detailed explanation of the options Adam Rosenfield said. For standard search directories, see An Introduction to GCC - for the GNU Compilers gcc and g++ - Setting Search Paths.

另请参阅GNU GCC 手册 - 链接选项以获取有关 Adam Rosenfield 所说的选项的详细说明。有关标准搜索目录,请参阅GCC 简介 - 对于 GNU 编译器 gcc 和 g++ - 设置搜索路径

回答by Alex Pantalones

You can try to use curl-config --libs.

您可以尝试使用curl-config --libs.

回答by adeel41

Anyone who is using ecplise CDT then you need to do following. First on terminal enter

任何使用 ecplise CDT 的人都需要执行以下操作。首先在终端输入

curl-config --libs

On my machine, the result is

在我的机器上,结果是

-L/usr/lib/i386-linux-gnu -lcurl

then do according to this screenshot and you will be able to compile. btw don't forget to add header files in your code

然后按照这个截图做,你就可以编译了。顺便说一句,不要忘记在代码中添加头文件

enter image description here

在此处输入图片说明

So you enter library folder path without -L and library name without -l because they will be automatically added by linker.

所以你输入不带 -L 的库文件夹路径和不带 -l 的库名称,因为它们会被链接器自动添加。