1

I found same codes to read datas from a web page for C++. It was like that:

#include <iostream>
#include <string>
#include <C:\curl\curl.h>
// Right path, I am sure.

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;
    }
    return 0;
}

But it does not work as it is expected. Here is the error messages:

1>------ Build started: Project: deneme, Configuration: Debug Win32 ------
1>  deneme.cpp
1>deneme.obj : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function _main
1>deneme.obj : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function _main
1>deneme.obj : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function _main
1>deneme.obj : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function _main
1>C:\Users\nazif\Documents\Visual Studio 2010\Projects\deneme\Debug\deneme.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How can I fix this?

Basicly, I want to get content of a certain web page with using curl in my C++ code.

5
  • Euh, you're not compiling cURL. You're compiling your own program that uses cURL, is it that? Commented Jan 4, 2013 at 14:00
  • Looks like you're not linking with the curl library. Commented Jan 4, 2013 at 14:00
  • @H2CO3 you are right, I changed the title of my question. Commented Jan 4, 2013 at 14:03
  • @JoachimIsaksson there is #include section at the top. It is linking, isn't it? Commented Jan 4, 2013 at 14:05
  • @user1948773 You need the .h files for the compiler and a .lib file for the linker. Both need to be used (see dandan78 below) Commented Jan 4, 2013 at 14:25

2 Answers 2

2

The #include <C:\curl\curl.h> just includes the relevant function prototypes etc. What you also have to do is update your project settings to tell the linker to link to the appropriate curl library.

Since you're using Visual Studio, right-click on your project and select properties. Then go to Configuration Properties/Linker. Then go to to Linker -> Input and enter the name of you lib file (there's probably a curl.lib in C:\curl\) in the Additional Dependencies field.

Sign up to request clarification or add additional context in comments.

2 Comments

But there is no such a file as curl.lib
Where did you download curl from? It's possible that you have to build curl to get the lib file.
0

Follow these instructions

MSVC 6 IDE


A minimal VC++ 6.0 reference workspace (vc6curl.dsw) is available with the source distribution archive to allow proper building of the two included projects, the libcurl library and the curl tool.

1) Open the vs/vc6/vc6curl.dsw workspace with MSVC6's IDE.

2) Select 'Build' from top menu.

3) Select 'Batch Build' from dropdown menu.

4) Make sure that the eight project configurations are 'checked'.

5) Click on the 'Build' button.

6) Once the eight project configurations are built you are done.

Dynamic and static libcurl libraries are built in debug and release flavours, and can be located each one in its own subdirectory, dll-debug, dll-release, lib-debug and lib-release, all of them below the 'vs/vc6/lib' subdirectory.

In the same way four curl executables are created, each using its respective library. The resulting curl executables are located in its own subdirectory, dll-debug, dll-release, lib-debug and lib-release, below 'vs/vc6/src' subdir.

These reference VC++ 6.0 configurations are generated using the dynamic CRT.

Intentionally, these reference VC++ 6.0 projects and configurations don't use third party libraries, such as OpenSSL or Zlib, to allow proper compilation and configuration for all new users without further requirements.

If you need something more 'involved' you might adjust them for your own use, or explore the world of makefiles described above 'MSVC from command line'.

Once it's done building, you'll have a .lib file in one of the folders

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.