Implementing libcurl

From Valve Developer Community
Jump to: navigation, search
Underlinked - Logo.png
This article needs more links to other articles to help integrate it into the encyclopedia. Please help improve this article by adding links that are relevant to the context within the existing text.
January 2024
English (en)Deutsch (de)
... Icon-Important.png

libcurl is a free library that can download data from the internet. It supports just about every protocol imaginable, and is available under a MIT/X derivative license.

Implementation

Windows

  1. Download the latest version of libcurl.
  2. Extract the \lib and \include folders to a convenient location.
  3. Open libcurl's VS project and change it to Release mode.
  4. Go to libcurl > Properties > C/C++ > Preprocessor > Definitions and add CURL_STATICLIB. Unless you really do want to use LDAP, add HTTP_ONLY too.
  5. Go to libcurl > Properties > C/C++ > Code Generation > Runtime Library and change it to read Multi-threaded (/MT).
  6. Build libcurl.
  7. Add libcurl.lib to your main project. The easiest way is to drag it onto the Solution Explorer.
  8. Go to Your Project > Properties > C/C++ > Preprocessor > Definitions and add CURL_STATICLIB, as you did to curl itself.
  9. Go to Your Project > Properties > C/C++ > General > Additional Include Directories and add the libcurl \include folder you extracted earlier.
  10. Go to Your Project > Linker > Input > Additional Dependencies and add ws2_32.lib. If you want to use LDAP, add wldap32.lib as well.
  11. #include "curl/curl.h" and start coding!
Note.pngNote:Remember that you will need to repeat steps 8-10 in both Release and Debug configurations.

Linux

Blank image.pngTodo:

Usage

Here is how to write the contents of a web page to the console:

#include "curl/curl.h"

// Called when curl receives data from the server
static size_t rcvData(void *ptr, size_t size, size_t nmemb, void *userdata)
{
	Msg((char*)ptr); // up to 989 characters each time
	return size * nmemb;
}

void PrintPage()
{
        CURL *curl;
	curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_URL, "developer.valvesoftware.com");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rcvData);
	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
}

ConCommand print_page("print_page", PrintPage);