How to Build and Use Libcurl with VS2015 on Windows

Libcurl is a free, open source library for transferring data. It supports various protocols include FTP, FTPS, HTTP, HTTPS, GOPHER, TFTP, SCP, SFTP, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP, and RTMP. I’ve been looking for a cross-platform HTTP library for my project, and libcurl seems to be the best choice.

Libcurl Download

The latest curl version is 7.45.0: http://curl.haxx.se/download.html

How to Build and Distribute Libcurl with Visual Studio 2015 on Windows

Run Visual Studio Command Line Tool:

Visual Studio Command Line Tool

Change directory to winbuild.

To distribute your application, you can force nmake to link in the static Microsoft’s C RunTime (CRT) by passing RTLIBCFG=static.:

Set RTLIBCFG=static

Then build static libcurl with the following command:

nmake /f Makefile.vc mode=static VC=14 MACHINE=x64 DEBUG=no

The generated libraries and header files are located at ../builds.

libcurl build

Create a new win32 project in Visual Studio 2015 and copy all relevant header files and static libraries into the project.

Write a simple test:

#include "stdafx.h"
#include "libcurl/include/curl/curl.h"

#ifdef _DEBUG
#pragma comment(lib, "libcurl/lib/libcurl_a_debug.lib")
#else
#pragma comment(lib, "libcurl/lib/libcurl_a.lib")
#endif

int main()
{
	curl_global_init(CURL_GLOBAL_DEFAULT);
	CURL *curl = curl_easy_init();
	if (curl) {
		CURLcode res;
		curl_easy_setopt(curl, CURLOPT_URL, "http://www.dynamsoft.com");
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();
	printf("Press any key to continue\n");
	getchar();
    return 0;
}

When building the project, you may see the error - curl.obj : error LNK2019: unresolved external symbol:

libcurl error

To fix the error, add the macro CRUL_STATICLIB to preprocessor:

curl static lib

Now you can successfully build your project.

One more thing! Do not forget to change Runtime Library to Multi-threaded(/MT):

runtime library mt

Test the curl app with www.dynamsoft.com:

curl test

Source Code

https://github.com/yushulx/libcurl-sample

Search Blog Posts