Home > C++, Web Programming, Windows Development > How to statically link the C++ REST SDK (Casablanca)

How to statically link the C++ REST SDK (Casablanca)


You are trying to use the C++ REST SDK (Casablanca) in your Windows application. You have one of the following problems:

  • you need Windows XP support
  • when your code executes you receive a debug assertion: _pFirstBlock == pHead
  • you get unpredictable behaviour or random crashing
  • you need to build an application which links against static libraries

You have 30 minutes to solve the problem. Here is how:

The issue is that the C++ REST SDK only supports dynamic linking. The solution is to re-build the SDK with static linking.

Install Git for Windows

Git is a source code repository management tool which you’ll need to access the C++ REST SDK source tree.

Download Git SCM.

Run the installer. Leave all the options at their defaults except on the page where you select what to add to the PATH environment variable. Choose to add the Git executable to your path.

Obtain the source code

Open a command prompt (press Start+R, type cmd and press Enter).

Use cd to navigate to the directory where you want to place the code and mkdir to create a directory to store the code, eg:

cd C:\Users\MyUserName\Documents\Visual Studio 2013\Projects
mkdir cpprest

Use git to obtain the source tree:

git clone https://git01.codeplex.com/casablanca cpprest

Replace cpprest with the target directory to store the code.

Open solution in Visual Studio

There are four .sln files. Select the correct one as follows:

  • casablanca110.desktop.sln – Windows XP / Vista / 7, Visual Studio 2012
  • casablanca110.WinRT.sln – Windows 8 / Metro, Visual Studio 2012
  • casablanca120.desktop.sln – Windows XP / Vista / 7, Visual Studio 2013
  • casablanca120.WinRT.sln – Windows 8 / Metro, Visual Studio 2013

In the Source folder, you will see projects named casablanca1x0 and casablanca1x0.xp. The former is for Windows Vista and Windows 7, the latter is for Windows XP. Right-click on the project you wish to build and choose Properties.

Change from dynamic to static linking

Select All Configurations and All Platforms from the drop-downs at the top of the Properties dialog.

In Configuration Properties -> General -> Project Defaults, change the Configuration Type option to Static library (.lib).

In Configuration Properties -> C/C++ -> Code Generation, change the Runtime Library option to Multi-threaded Debug (/MTd) for the Debug configuration and Multi-threaded (/MT) for the Release configuration.

In Configuration Properties -> C/C++ -> Preprocessor for All Configurations, add the text _NO_ASYNCRTIMP; to the Preprocessor Definitions option. This prevents the compiler from trying to export the functions to a DLL.

Click OK to save the changes.

Build the project

Right-click the project and choose Build. Make sure you do this for each configuration and platform you want to build for.

In the folder you created for the source tree, open Binaries\Win32\Debug and rename cpprest120d_2_0.lib to cpprest120_2_0.lib (remove the ‘d’). Note that the library name will include 110 rather than 120 for Visual Studio 2012 builds and the two final digits representing the SDK version may be different. This article is based on version 2.0.

Prepare your application

Open the solution for the application you want to build, right-click on it and choose Properties.

Select All Configurations and All Platforms from the drop-downs at the top of the Properties dialog.

In Configuration Properties -> VC++ Directories, add a new entry in Include Directories as follows:

C:\PathToCppRESTSDK\Release\include

In Configuration Properties -> VC++ Directories, add a new entry in Library Directories as follows:

C:\PathToCppRESTSDK\Binaries\Win32\$(Configuration)

In Configuration Properties -> C/C++ -> Preprocessor, add the text _NO_ASYNCRTIMP; to the Preprocessor Definitions option. This prevents the compiler from trying to find the C++ REST SDK functions from a DLL.

In Configuration Properties -> Linker -> Input, add winhttp.lib and cpprest120_2_0.lib (replacing the numbers with the correct Visual Studio and C++ REST SDK version numbers) to the Additional Dependencies option.

Build your project.

If all goes well, you should now have a statically linked version of the SDK which is linked in your application with no linker errors.

I hope you found this guide useful!

References

C++ REST SDK – Statically Linked Runtime

C++ REST SDK – How to setup, build and run tests on Windows

Git for Windows

Advertisements
  1. fruler
    December 17, 2014 at 19:27

    what about 3-rd parties of casablanca?
    libeay32.dll
    ssleay32.dll

    • Homer J. Simpson
      December 18, 2014 at 22:12

      Use the nt.mak file instead of ntdll.mak.

  2. Homer J. Simpson
    December 18, 2014 at 22:12

    Was having trouble with unresolved errors until I found this article Thanks a bunch.

  3. savita
    February 16, 2015 at 15:06

    Hi,

    I followed all the steps.. But while compiling my application i get below errors around 100 :

    Error 1 error LNK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MTd_StaticDebug’ doesn’t match value ‘MDd_DynamicDebug’ in HttpClient_Staticrest.obj

    Error 2 error LNK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MTd_StaticDebug’ doesn’t match value ‘MDd_DynamicDebug’ in HttpClient_Staticrest.obj c:\Users\maplelabs\Desktop\HttpClient_Staticrest\HttpClient_Staticrest\cpprest120_2_4.lib(uri_builder.obj) HttpClient_Staticrest

    Anybody got any idea?

  4. savita
    February 16, 2015 at 15:13

    Hi,
    Able to resolve that issue. But i am getting linker error like below:

    Error 1 error LNK2019: unresolved external symbol _CryptProtectMemory@12 referenced in function “public: __thiscall web::details::win32_encryption::win32_encryption(class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > const &)” (??0win32_encryption@details@web@@QAE@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z) c:\Users\maplelabs\Desktop\HttpClient_Staticrest\HttpClient_Staticrest\cpprest120_2_4.lib(web_utilities.obj) HttpClient_Staticrest

    Error 2 error LNK2019: unresolved external symbol _CryptUnprotectMemory@12 referenced in function “public: class std::unique_ptr<class std::basic_string<wchar_t,struct std::char_traits,class std::allocator >,class web::details::zero_memory_deleter> __thiscall web::details::win32_encryption::decrypt(void)const ” (?decrypt@win32_encryption@details@web@@QBE?AV?$unique_ptr@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@Vzero_memory_deleter@details@web@@@std@@XZ) c:\Users\maplelabs\Desktop\HttpClient_Staticrest\HttpClient_Staticrest\cpprest120_2_4.lib(web_utilities.obj) HttpClient_Staticrest

    Has copied the cpprest120d_2_4.lib to cpprest120_2_4.lib in Debug folder.. Still same error.
    Please help

  5. freyatsao
    October 6, 2015 at 11:42

    Hi,
    I build cpprestsdk 2.5.0 library under VS2012 with Static library (.lib), Multi-threaded (/MT) and add the text _NO_ASYNCRTIMP. And I use the library in my MFC project which adds Crypt32.lib and Bcrypt.lib too. But still had errors like below:

    error LNK2001: unresolved external symbol “public: class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > __thiscall web::http::details::http_msg_base::_extract_string(void)” (?_extract_string@http_msg_base@details@http@web@@QAE?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@XZ)

    error LNK2001: unresolved external symbol “public: class web::json::value __thiscall web::http::details::http_msg_base::_extract_json(void)” (?_extract_json@http_msg_base@details@http@web@@QAE?AVvalue@json@4@XZ)

    error LNK2001: unresolved external symbol “public: virtual void __thiscall web::http::details::http_msg_base::_complete(unsigned __int64,class std::exception_ptr)” (?_complete@http_msg_base@details@http@web@@UAEX_KVexception_ptr@std@@@Z)
    error LNK2019: unresolved external symbol “public: __thiscall web::http::client::http_client::http_client(class web::uri)” (??0http_client@client@http@web@@QAE@Vuri@3@@Z) referenced in function __catch$?DoWork@BaseAPI@@QAEXXZ$0

    error LNK2019: unresolved external symbol “public: class pplx::task __thiscall web::http::client::http_client::request(class web::http::http_request,class pplx::cancellation_token)” (?request@http_client@client@http@web@@QAE?AV?$task@Vhttp_response@http@web@@@pplx@@Vhttp_request@34@Vcancellation_token@6@@Z) referenced in function “public: class pplx::task __thiscall web::http::client::http_client::request(class std::basic_string<wchar_t,struct std::char_traits,class std::allocator >,class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > const &,class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > const &,class std::basic_string<wchar_t,struct std::char_traits,class std::allocator >,class pplx::cancellation_token)” (?request@http_client@client@http@web@@QAE?AV?$task@Vhttp_response@http@web@@@pplx@@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@ABV78@10Vcancellation_token@6@@Z)

    Does anyone has any ideas? Or what I need to include?
    Please give me some suggestions. Thanks a lot!

  6. January 21, 2016 at 19:42

    This was very helpful to me, thanks.

  7. Pedram
    January 6, 2017 at 07:00

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol _inflateInit2_ referenced in function “public: __thiscall web::http::details::compression::stream_decompressor::stream_decompressor_impl::stream_decompressor_impl(enum web::http::details::compression::compression_algorithm)” (??0stream_decompressor_impl@stream_decompressor@compression@details@http@web@@QAE@W4compression_algorithm@2345@@Z) update_service C:\Users\myname\documents\visual studio 2015\Projects\my_project\my_project\cpprest140_2_9.lib(http_helpers.obj) 1

  8. Alexey
    February 5, 2017 at 12:31

    Hello,

    Getting these errors:

    unresolved external symbol “public: __cdecl web::details::win32_encryption::~win32_encryption(void)” (??1win32_encryption@details@web@@QEAA@XZ)

    unresolved external symbol “public: __cdecl web::uri::uri(wchar_t const *)” (??0uri@web@@QEAA@PEB_W@Z)

    unresolved external symbol “public: __cdecl web::websockets::client::details::websocket_client_task_impl::websocket_client_task_impl(class web::websockets::client::websocket_client_config)” (??0websocket_client_task_impl@details@client@websockets@web@@QEAA@Vwebsocket_client_config@234@@Z)

    Can anyone help?
    Thanks.

  9. Oleg Shirokikh
    April 7, 2017 at 06:03

    to resolve the linking errors related to “compression” define CPPREST_EXCLUDE_COMPRESSION when building

  1. April 2, 2014 at 23:33
  2. May 25, 2015 at 12:51

Share your thoughts! Note: to post source code, enclose it in [code lang=...] [/code] tags. Valid values for 'lang' are cpp, csharp, xml, javascript, php etc. To post compiler errors or other text that is best read monospaced, use 'text' as the value for lang.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: