Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

c++ - 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!
");
     return 0;
    }

Here are the errors:

||=== Fa, Release ===|
objReleasemain.o:main.c|| undefined reference to `_imp__curl_global_init'|
objReleasemain.o:main.c|| undefined reference to `_imp__curl_easy_init'|
objReleasemain.o:main.c|| undefined reference to `_imp__curl_easy_setopt'|
objReleasemain.o:main.c|| undefined reference to `_imp__curl_easy_perform'|
objReleasemain.o:main.c|| undefined reference to `_imp__curl_easy_cleanup'|
||=== Build finished: 5 errors, 0 warnings ===|
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...