Sunday 25 December 2011

using libcurl in c/c++ program

  Using libcurl in c/c++ program on window machine.



1) Download libcurl:
      Go to curl.haxx.ex website and download the most appropriate package as per your system.
For this tutorial i've downloaded libcurl msvc build without ssl support.

When finish download , fire up your miscrosoft visual studio version 6 or higher( recommended).

Create new project:
   Win32>>Win32 Console Application

  

Press "OK" and "NEXT"

Now, in the Application Setting do uncheck "Precompiled headers" and then pess "Finish".



Now, before we begin writing any code, it's time to configure our visual studio to know libcurl whereabouts.

Configure Environment:
Go to Tools >> Options
           In the dialogue box so opened expand "Projects and Solutions" and choose "VC++ directories".




Add path to libcurl`s "include" folder in there. Do make sure that when you add library path select "library files" from 'show directories for:' drop down list and "include files" before adding libcurl`s include folder path.

When you're done, it's time to write our program.


2) Open curlTry2.cpp file and write a simple curl program. To ease this, you can copy a simple curl program from the curl examples given inside download libcurl files. (docs/examples)
Copy simple.c and paste it in curlTry.cpp program.

3) Run the program
             If you try to build your project now, it'll not build.
You'll see some linking error as follow:

1>------ Build started: Project: curlTry2, Configuration: Debug Win32 ------
1>Compiling...
1>curlTry2.cpp
1>stdafx.cpp
1>Generating Code...
1>Linking...
1>curlTry2.obj : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function _main
1>curlTry2.obj : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function _main
1>curlTry2.obj : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function _main
1>curlTry2.obj : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function _main
1>C:\Users\skm08030\Documents\Visual Studio 2008\Projects\curlTry2\Debug\curlTry2.exe : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at "file://c:\Users\skm08030\Documents\Visual Studio 2008\Projects\curlTry2\curlTry2\Debug\BuildLog.htm"
1>curlTry2 - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Correcting linking error:
             Error comes because you haven't specified the libcurl.lib files to it.
To do so, just right click on the project and select properties.



If it again throw the same linking error, then your lib and include dir path given under "VC++ directory" is not properly set.Please verify them. 

Try running your program again, this time it may demand for zlib1.dll file.

Download and copy dll file in project folder where curlTry2.cpp file is located. If you've already downloaded zlib1.dll files but it's giving you this error:
       ordinal 55 coun't not be located in dynamic link library zlib1.dll
then try downloading and adding zlib1.dll file from this link. 
Now, run your program. If everything goes right then it should work.



Reference:
http://curl.haxx.se/libcurl/c/

Windows developers using Microsoft Visual Studio, might enjoy:
             -> Rosso Salmanzadeh's Using libcurl in Visual Studio [PDF] guide.
             -> Andrei Jakab's Using libcurl with SSH support in Visual Studio 2008 [PDF]

No comments:

Post a Comment