C/C++ Windows 程式設計 網路

Use the CURL Library in Code::Blocks on Windows Platform

In current days, our college students meet the problem while compiling the CURL library in Code:: Blocks on Windows(i.e. Windows 7) platform.

I noted the process that how to write the sample program with CURL library and compile it successfully. There are some steps which we should do: (This is a simple example run on Windows 7 64bits, Code:: Blocs 12.11 and CURL 7.31.0 )

Step 1. Go to the CURL download page, and download the Win32 Generic libcurl library.

download_link

Step 2. Extract it(curl-7.31.0-devel-mingw32.zip)  into the folder what you want. In this example, I extract it into D:Develop Toolscurl-7.31.0-devel-mingw32

Step 3. Open your Code:: Blocks, creating one project(I’ve named it CURLExample here) and write some simple codes shown bellow:

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

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

Step 4. Navigate the toolbar. Go to ProjectBuild options.

BuildOptions

Step 5. Here we introduce an easy way to compile it using DLL. After opening the build option dialog, you will see the Compiler Settings, Linker Settings…. Switch to Linker Settings, clicking Add button and type curldll.

BuildOptions_Link Options

This option is just tell the linker to use curldll library. Moreover, the sample code include the curl/curl.h. But the MinGW compiler doesn’t know where the libraries and include header files are. Let’s refer to the next two steps for more details.

Step 6. Switch to Search directories. Click the Compiler label and add path D:Develop Toolscurl-7.31.0-devel-mingw32include. (This action is to ask the compiler should search the header directory additionally.)

BuildOptions_SearchforIncludeDir

Step 7. Switch to Linker label and add the linker path D:Develop Toolscurl-7.31.0-devel-mingw32lib. (This path is to ask the linker should search the library directory, which contains the libcurl.a file.)

BuildOptions_SearchforLinkerDir

Step 8. After that, you can compile the simple code. Enjoy it!

Notice If you want to use the static library combination rather than dynamic linking libraries(DLL). You should can refer to this issue publiched on StackOverflow.

Leave a Reply

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

Back To Top