我试图在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'
我不知道如何解决这个问题。
您不与库链接。
使用外部库时,必须与之链接:
$ gcc test.c -lcurl
最后一个选项告诉GCC将(-l
)与库curl
链接起来。
除了Joachim Pileborg的回答之外,记住GCC/G++链接对顺序很敏感,并且链接库必须遵循依赖于它们的东西是很有用的。
$gcc-lcurl test.c
将失败,丢失与之前相同的符号。 我提到这一点是因为我来到这一页是为了忘记这一事实。
我也有同样的问题,但是我使用g++和make文件。 这是一个链接器问题。 您需要在编译器和链接器上添加选项-lcurl。 在我的例子中,在make文件上:
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option
杰拉德