Curl programming in C

Posted by Joys of Programming on in C/C++

Have you ever used curl or wget on the command line? If not, check what is curl? Both curl and wget are useful command line tools to download a page. Using these tools you can download a whole website to your desktop. Curl can be also be used to post data to a website. Curl is command line tool making use of the libcurl library. libcurl library is used to download or upload web pages. It is a full fledged library with many capabilities.

For curl programming, you must include the curl header files in your program: curl/curl.h

#include <stdio.h>
#include <curl/curl.h>

int main()
{
   CURL *curl;
   curl = curl_easy_init();
   /*
     * Add your processing
     */
   curl_easy_cleanup(curl);
}

Now if you compile this program in the following way

gcc curl.c

You may probably end up having the following error

Undefined symbols:
  "_curl_easy_cleanup", referenced from:
      _main in ccCyxU7X.o
  "_curl_easy_init", referenced from:
      _main in ccCyxU7X.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

This is because the libcurl library is not included by default. So to compile and link the libcurl library, you must make use of -l option of gcc which helps in dynamic linking of the libraries

gcc curl.c -l curl

Now your program will be properly compiled and linked

Reblog this post [with Zemanta]


Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.