Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to curl_global_init, curl_easy_init and other function(C)

Tags:

c

curl

libcurl

I am trying to use Curl in C.

I visited Curl official page, and copied sample source code.

below is the link: http://curl.haxx.se/libcurl/c/sepheaders.html

when I run this code with command "gcc test.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.

like image 277
Juneyoung Oh Avatar asked May 10 '13 06:05

Juneyoung Oh


People also ask

What is Curl_easy_init?

Description. This function must be the first function to call, and it returns a CURL easy handle that you must use as input to other easy-functions. curl_easy_init initializes curl and this call MUST have a corresponding call to curl_easy_cleanup(3) when the operation is complete.

What is Curl_global_init?

Description. This function sets up the program environment that libcurl needs. Think of it as an extension of the library loader. This function must be called at least once within a program (a program is all the code that shares a memory space) before the program calls any other function in libcurl.

What is Curl_easy_perform?

curl_easy_perform performs the entire request in a blocking manner and returns when done, or earlier if it fails. For non-blocking behavior, see curl_multi_perform. You can do any amount of calls to curl_easy_perform while using the same easy_handle.


2 Answers

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.

like image 77
Some programmer dude Avatar answered Oct 14 '22 17:10

Some programmer dude


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.

$ gcc -lcurl test.c

will fail, missing the same symbols as before. I mention this because I came to this page for forgetting this fact.

like image 35
chirality Avatar answered Oct 14 '22 19:10

chirality