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测试.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.
我不知道怎么解决这个问题。
更多回答
You don't link with the library.
你没有链接到图书馆。
When using an external library you must link with it:
使用外部库时,必须与其链接:
$ gcc test.c -lcurl
The last option tells GCC to link (-l
) with the library curl
.
最后一个选项告诉GCC将(-L)与库卷曲链接。
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测试.c
will fail, missing the same symbols as before. I mention this because I came to this page for forgetting this fact.
将失败,缺少与以前相同的符号。我提到这一点是因为我来到这个页面是为了忘记这个事实。
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:
我也有同样的问题,但我使用了带有make文件的g++。这是一个链接器问题。您需要在编译器和链接器上添加选项-lcurl。在我的Make文件中:
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option
Gerard
杰拉德
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/来让链接器知道库在哪里。Ldconfig应该会在每次引导时获取并找到它们,但在一台新机器上可能需要一些刺激,比如将一个目录添加到您的/etc/ld.so.conf中。
I had the same problem and found another line among it : "libcurl-x86.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
" which means the lib was 64 bit, while i was compiling for 32 bit.
我也遇到了同样的问题,并在其中找到了另一行:“libcurl-x86.lib:Warning LNK4272:库机器类型‘x64’与目标机器类型‘x86’冲突”,这意味着当我编译32位时,库是64位的。
更多回答
@Accountantم If there's only a static curl
library, it will be linked statically. Otherwise if it's dynamic then it will be linked dynamically.
@Accountم如果只有一个静态卷曲库,它将被静态链接。否则,如果它是动态的,那么它将被动态链接。
Could you please tell me why gcc -lcurl test.c
will fail but gcc test.c -lcurl
worked fine?
你能告诉我为什么GCC-lcurl测试不及格,而GCC测试.c-lcurl运行良好吗?
@Lane The gcc -lcurl test.c
will fail because gcc links the files in order of input, so it cannot reference - for example curl_global_init
- in files appear after test.c
.
@Lane GCC-lcurl test.c会失败,因为GCC是按照输入的顺序链接文件的,所以不能引用--例如curl_global_init-in文件出现在test.c之后。
Many thanks to this post!!!! This saved my day. After reading this post I have just changed the order of linking libraries and I could able to compile and link successfully.
非常感谢这篇文章!这拯救了我的一天。读完这篇文章后,我刚刚改变了链接库的顺序,我可以成功地编译和链接。
the param order is wrong : gcc test.c -lcurl
参数顺序错误:GCC测试c-lcurl
我是一名优秀的程序员,十分优秀!