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

Jun*_* Oh 45 c curl libcurl

我想在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'
Run Code Online (Sandbox Code Playgroud)

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

Som*_*ude 86

您没有链接到库.

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

$ gcc test.c -lcurl
Run Code Online (Sandbox Code Playgroud)

最后一个选项告诉GCC link(-l)与库curl.

  • @Accountantm 如果只有一个静态“curl”库,它将被静态链接。否则,如果它是动态的,那么它将被动态链接。 (2认同)

小智 23

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

$ gcc -lcurl test.c

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

  • 你能告诉我为什么`gcc -lcurl test.c` **会失败**,但是`gcc test.c -lcurl`可以正常工作吗? (3认同)
  • 非常感谢这篇文章!!!!这救了我的命。读完这篇文章后,我刚刚更改了链接库的顺序,并且能够成功编译和链接。 (2认同)