C语言 对 curl_global_init、curl_easy_init 和其他函数的未定义引用(C)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16476196/
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
undefined reference to curl_global_init, curl_easy_init and other function(C)
提问by Juneyoung Oh
I am trying to use Curl in C.
我正在尝试在 C 中使用 Curl。
I visited Curl official page, and copied sample source code.
我访问了 Curl 官方页面,并复制了示例源代码。
below is the link: http://curl.haxx.se/libcurl/c/sepheaders.html
以下是链接:http: //curl.haxx.se/libcurl/c/sepheaders.html
when I run this code with command "gcc test.c",
当我使用命令“gcc test.c”运行此代码时,
the console shows message like below.
控制台显示如下消息。
/tmp/cc1vsivQ.o: In function `main':
test.c:(.text+0xe1): undefined reference to `curl_global_init'
test.c:(.text+0xe6): undefined reference to `curl_easy_init'
test.c:(.text+0x10c): undefined reference to `curl_easy_setopt'
test.c:(.text+0x12e): undefined reference to `curl_easy_setopt'
test.c:(.text+0x150): undefined reference to `curl_easy_setopt'
test.c:(.text+0x17e): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1b3): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1db): undefined reference to `curl_easy_setopt'
test.c:(.text+0x1e7): undefined reference to `curl_easy_perform'
test.c:(.text+0x1ff): undefined reference to `curl_easy_cleanup'
I do not know how to solve this.
我不知道如何解决这个问题。
回答by Some programmer dude
You don't link with the library.
你没有链接到图书馆。
When using an external library you must linkwith it:
使用外部库时,您必须与其链接:
$ gcc test.c -lcurl
The last option tells GCC to link (-l) with the library curl.
最后一个选项告诉 GCC 链接 ( -l) 与库curl。
回答by chirality
In addition to Joachim Pileborg's answer, it is useful to remember that gcc/g++ linking is sensitive to order and that your linked libraries must follow the things that depend upon them.
除了 Joachim Pileborg 的回答之外,记住 gcc/g++ 链接对顺序很敏感并且您的链接库必须遵循依赖它们的东西是很有用的。
$ gcc -lcurl test.c
$ gcc -lcurl test.c
will fail, missing the same symbols as before. I mention this because I came to this page for forgetting this fact.
将失败,缺少与以前相同的符号。我提到这一点是因为我来到这个页面是为了忘记这个事实。
回答by Gerard
I have the same problem, but i use g++ with a make file. This is a linker issue. You need to add option -lcurl on the compiler and on the linker. In my case on the make file:
我有同样的问题,但我将 g++ 与 make 文件一起使用。这是链接器问题。您需要在编译器和链接器上添加选项 -lcurl。在我的情况下,make 文件:
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option
Gerard
杰拉德
回答by Alan Corey
Depending how bad things are you might need an -L/somewhere in LDFLAGS to let the linker know where the libraries are. ldconfig is supposed to pick them up and find them on every boot but on a new machine it can take a little prodding, like adding a directory to your /etc/ld.so.conf.
根据事情的严重程度,您可能需要在 LDFLAGS 中使用 -L/somewhere 来让链接器知道库的位置。ldconfig 应该在每次启动时捡起它们并找到它们,但是在新机器上它可能需要一点刺激,比如向你的 /etc/ld.so.conf 添加一个目录。

