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

  1. November 22, 2023 at 14:06

    Hello,
    is it possible to provide the git-repo https://git01.codeplex.com/casablanca? I have to use Visual Studio 2013 and C++ REST SDK. I have installed the sdk as nugget, but I get a debug assertion: _pFirstBlock == pHead.

    Best regards

    Felix

  2. Divaker shukla
    June 12, 2019 at 06:47

    Getting below error please help….

    Severity Code Description Project File Line Suppression State

    Error LNK2001 unresolved external symbol “public: __thiscall web::http::client::http_client::http_client(class web::uri const &,class web::http::client::http_client_config const &)” (??0http_client@client@http@web@@QAE@ABVuri@3@ABVhttp_client_config@123@@Z) Driver D:\Divaker Driv\kylin-master – Copy\odbc\Driver\Common.lib(REST.obj) 1

    Error LNK2001 unresolved external symbol “public: enum web::json::value::value_type __thiscall web::json::value::type(void)const ” (?type@value@json@web@@QBE?AW4value_type@123@XZ) Driver D:\Divaker Driv\kylin-master – Copy\odbc\Driver\Common.lib(JsonConverter.obj) 1

    Error (active) E1097 unknown attribute “no_init_all” Common C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winnt.h 7597
    Error LNK1120 2 unresolved externals Driver D:\Divaker Driv\kylin-master – Copy\odbc\Driver\Win32\Release\driver.dll 1

  3. December 7, 2017 at 23:08

    Greetinmgs from Ohio! I’m bored to death
    at work so I decided to chwck out your blog on my iphone
    dhring lunch break. I lovee thee information you provide here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my
    phine .. I’m nnot even uusing WIFI, just 3G .. Anyways, superb blog!

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

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

  5. 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.

  6. 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

  7. January 21, 2016 at 19:42

    This was very helpful to me, thanks.

  8. 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!

  9. 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

  10. 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?

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

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

  12. 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.

  1. April 6, 2020 at 23:37
  2. August 6, 2017 at 08:39
  3. June 10, 2017 at 19:56
  4. June 10, 2017 at 19:47
  5. May 25, 2015 at 12:51
  6. April 2, 2014 at 23:33

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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.