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
what about 3-rd parties of casablanca?
libeay32.dll
ssleay32.dll
Use the nt.mak file instead of ntdll.mak.
Was having trouble with unresolved errors until I found this article Thanks a bunch.
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?
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
You have to add Crypt32.lib, Bcrypt.lib too.
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!
This was very helpful to me, thanks.
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
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.
to resolve the linking errors related to “compression” define CPPREST_EXCLUDE_COMPRESSION when building
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!
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