对curl\u global\u init、curl\u easy\u init和其他函数(C)的未定义引用
我试图在C中使用Curl 我访问了Curl官方页面,并复制了示例源代码 以下是链接: 当我使用命令“gcc test.c”运行此代码时 控制台显示如下消息对curl\u global\u init、curl\u easy\u init和其他函数(C)的未定义引用,c,curl,libcurl,C,Curl,Libcurl,我试图在C中使用Curl 我访问了Curl官方页面,并复制了示例源代码 以下是链接: 当我使用命令“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:(.t
/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将(
-l
)链接到库curl
除了Joachim Pileborg的答案之外,记住GCC/g++链接对顺序很敏感,并且链接的库必须遵循依赖于它们的内容
$gcc-lcurl test.c
将失败,丢失与以前相同的符号。我之所以提到这一点,是因为我来到这一页时忘记了这一事实。我也有同样的问题,但我使用了带有make文件的g++。 这是一个链接器问题。 您需要在编译器和链接器上添加选项-lcurl。 在我的make文件中:
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option
CC?=gcc
CXX?=g++
CXXFLAGS+=-I../src/-I./-DLINUX-lcurl根据情况的不同,您可能需要在LDFLAGS中的某个位置使用-L/来让链接器知道库的位置。ldconfig应该在每次开机时都能找到它们,但在一台新机器上可能需要一点刺激,比如在你的/etc/ld.so.conf.中添加一个目录,你能告诉我为什么gcc-lcurl test.c
会失败,但是gcc-test.c-lcurl
工作正常吗?@Lanegcc-lcurl test.c
会失败,因为gcc按照输入顺序链接文件,因此,它不能在test.c
之后出现的文件中引用(例如curl\u global\u init
)!!!!这救了我一天。读了这篇文章后,我刚刚改变了链接库的顺序,我可以成功地编译和链接。@accountaryم如果只有一个静态curl
库,它将被静态链接。否则,如果它是动态的,那么它将被动态链接。
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option