未定义引用curl_global_init,curl_easy_init和其他函数(C)

问题描述 投票:40回答:5

我想在C中使用Curl。

我访问了Curl官方页面,并复制了示例源代码。

以下是链接:http://curl.haxx.se/libcurl/c/sepheaders.html

当我使用命令“gcc test.c”运行此代码时,

控制台显示如下消息。

/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'

我不知道如何解决这个问题。

c curl libcurl
5个回答
84
投票

您没有链接到库。

使用外部库时,必须与之链接:

$ gcc test.c -lcurl

最后一个选项告诉GCC链接(-l)和库curl


22
投票

除了Joachim Pileborg的回答之外,记住gcc / g ++链接对顺序敏感并且链接库必须遵循依赖于它们的东西是有用的。

$ gcc -lcurl test.c

会失败,错过与以前相同的符号。我提到这个是因为我来到这个页面是因为忘记了这个事实。


1
投票

我有同样的问题,但我使用g ++与make文件。这是一个链接器问题。您需要在编译器和链接器上添加选项-lcurl。在我的情况下,在make文件中:

CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl  <- compile option
LDFLAGS += -lrt -lpthread -lcurl      <- linker option

杰拉德


0
投票

根据LDFLAGS中可能需要-L /某处的内容,让链接器知道库的位置。 ldconfig应该在每次启动时找到并找到它们,但在新机器上它可能需要一点点刺激,比如在/etc/ld.so.conf中添加一个目录。

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