Raven
Raven

Reputation: 2257

How do I include curl library in my C project in Code Blocks (Windows)?

The question seems straight forward. I tried a lot of things just to include curl in my C project using the code::blocks ide but to no avail.

I would like to use cURL's library for my console app project that needs http capabilities. If anyone had successfully done so, then your help is very much appreciated. :)

What happened previously:

-I copied all cURL files to my project and linked the libraries (the ones with .a or .lib ext.)

-Then when I build the project. A lot of undefined reference showed up.

This is the code I was testing:

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

    int main()
    {
     curl_global_init( CURL_GLOBAL_ALL );
     CURL * myHandle;
     CURLcode result; 
     myHandle = curl_easy_init ( ) ;

     curl_easy_setopt(myHandle, CURLOPT_URL, "http://www.example.com");
     result = curl_easy_perform( myHandle );
     curl_easy_cleanup( myHandle ); 
     printf("LibCurl rules!\n");
     return 0;
    }

Here are the errors:

||=== Fa, Release ===|
obj\Release\main.o:main.c|| undefined reference to `_imp__curl_global_init'|
obj\Release\main.o:main.c|| undefined reference to `_imp__curl_easy_init'|
obj\Release\main.o:main.c|| undefined reference to `_imp__curl_easy_setopt'|
obj\Release\main.o:main.c|| undefined reference to `_imp__curl_easy_perform'|
obj\Release\main.o:main.c|| undefined reference to `_imp__curl_easy_cleanup'|
||=== Build finished: 5 errors, 0 warnings ===|

Upvotes: 4

Views: 20747

Answers (2)

veen_99
veen_99

Reputation: 47

The following steps are for windows 32 users. And the following steps worked for me after several hours. This may or may not work for you as I got it after several trial and errors. So thats why I am writing the steps in elaborate manner.

step1: start code blocks create an empty project with name proj_curl

step2: Open on this url www.paehl.com/open_source/?CURL_7.50.0

  • in this site download

a) click Download without ssl

b) click Download libcurl.dll (all versions) only

c) click Download the Libray (all version) only

step3: open on this url curl.haxx.se/download/curl-7.50.1.zip - a zip file will be downloaded automatically. (sorry folks this site is not allowing me to insert more than 2 links, bear with me.)

Note: Now Totally You have 4 zip files downloaded

  1. curl_750_0.zip (which contains curl.exe file)
  2. curl_DLL_ONLY.7z(which contains folders such as nossl, RTMP, ssh...etc.)
  3. Curl_lib.7z(which contains lib folder)
  4. curl-7.50.1.zip(which contains curl-7.50.1 folder)

step4: create a folder in c drive(c:\libs\curl) extract all the folders files of the 4 zip files into the curl folder in c. Make sure to create each subdirectory for each zip file.

step5:(Very important)

In the curl folder of C drive, - click on curl.exe(a command prompt just appears for few millisecs.) - Go to code blocks -> Settings -> Compiler... -> Linker settings tab -> Add

"C:\libs\curl\lib\nossl\libcurl.a"

"C:\libs\curl\lib\nossl\libcurldll.a"

then -> search directories tab -> Compiler enter image description here

then -> search directories tab -> Linker enter image description here

Click ok

Step6: - open the folder "nossl" which came in the curl_DLL_ONLY.7z file.

  • copy the libcurl.dll file to the bin\debug folder of the code blocks project.

Step7: Now test the working of the curl library with your c program.

Note: if you encounter an error "The program cant start because zlib1.dll is missing from your computer. Try reinstalling the program to fix this problem."

Solution: - copy and paste the url sourceforge.net/projects/mingw/files/latest/download

  • install the mingw-get-setup.exe by marking all the libraries.(takes about 10 min to install)

  • after installation, open C:\MinGW\bin, copy the zlib1.dll to the same directory as you did in step6 for libcurl.dll.

Thats it...

Upvotes: 3

j4x
j4x

Reputation: 3716

Go to your project "Build Options" -> "Linker" tab and so you have two choices:

  1. If your library is (correctly) installed system-wide, write in "other linker options" the libs as if you were using your compiler directly. For GCC you'd write -lcurl. You may also use this with a path instruction like Wl,-rpath,/path/to/your/library -lMyLib. Obviously it depends on the compiler and system setup.

  2. Add the library in "Link libraries" on the left. Click the "Add" button and browse to your library file.

Take a look at this A.3 — Using libraries with Code::Blocks for some pictures. Googling around will show you more.

Upvotes: 4

Related Questions