8

I am trying to create a Windows-based VC++ DLL (in VS 2015) that statically links to the Casablanca CPPREST SDK. That is, I would like a single DLL output which contains the CPP REST library along with my code.

I have referred to this thread, however it seems rather dated (and has broken links):

https://katyscode.wordpress.com/2014/04/01/how-to-statically-link-the-c-rest-sdk-casablanca/

I have tried to download the Casablanca repo from GitHub and compile the "cpprestsdk140.static" project which produces a lib file. The problem is I get a number of unresolved externals when linking it with my project.

Numerous people have commented on the unresolved link errors in the above-mentioned URL. There are also numerous threads on Github with people saying they cannot link simple projects with the static library.

Does anyone have a clear set of steps that can help resolve this?

Simple Guy
  • 568
  • 5
  • 21

3 Answers3

13

OK, I pooled together a number of suggestions from the various threads and have the following set of steps in order to successfully link to the CPP REST static library:

  1. Download Casablanca SDK from https://github.com/Microsoft/cpprestsdk. (via GIT Clone or Zip).
  2. Open the VS solution and right-click on the properties of the cpprestsdk140.static.
  3. In the C++ -> Preprocessor definitions, add CPPREST_EXCLUDE_COMPRESSION. The full list looks like: _NO_ASYNCRTIMP;_ASYNCRT_EXPORT;_PPLX_EXPORT;WIN32;_MBCS;_USRDLL;CPPREST_EXCLUDE_COMPRESSION;%(PreprocessorDefinitions)
  4. In Librarian -> General -> Additional dependencies, add crypt32.lib;winhttp.lib; (See https://github.com/Microsoft/cpprestsdk/issues/344)
  5. Press OK, then build the cpprestsdk140.static project. You will end up with a libcpprest140d_2_9.lib (for Debug build) in the Binaries directory.

Now, in your own project:

  1. If you have previously used the Nuget version of CPPREST, firstly ensure you remove any references in the Nuget package manager.
  2. Right-click your project properties and go to C++ -> Additional Include Directories and enter the path for the CPPREST SDK include files. They currently reside in cpprestsdk\Release\include.
  3. Now go to C++ -> Preprocessor definitions, add _NO_ASYNCRTIMP (See https://github.com/Microsoft/cpprestsdk/issues/124).
  4. Go to Linker -> Input and add libcpprest140d_2_9.lib (along with pathname, if applicable). For the release version, it appears you also need to add crypt32.lib;winhttp.lib.
  5. Build your project and hopefully all is well ;)

I hope this helps someone (I'm sure it will)!

Simple Guy
  • 568
  • 5
  • 21
  • 1
    As a side note, if you link to `bcrypt.lib;crypt32.lib;winhttp.lib;` for the cpprest static library itself, then you wouldn't need to link to these libraries again in your own project – Oleg Shirokikh Dec 20 '17 at 00:43
  • This worked like a charm for vc142! I didn't end up needing the step for CPPREST_EXCLUDE_COMPRESSION. The static libs need to be available in NuGet to save a lot of time. I did notice WIN32 was set on the 64-bit build. – tgraupmann Jul 17 '20 at 01:12
  • I put my compiled v142 x64 binaries here - https://github.com/tgraupmann/Cpp_BoostJsonZipPostJwt/releases – tgraupmann Jul 17 '20 at 01:42
  • There's one more detail needed. Somehow a dependency on the DLL version of the library is being added instead of the static libraries for `libcrypto-1_1-x64.dll` and `libssl-1_1-x64.dll`. It's working, but I'd like to statically link both. – tgraupmann Jul 17 '20 at 03:32
  • Looks like I just need to build static libs for those two libraries. Ther openssl-vc142 and static lib Nuget packages still had a dependency on the DLLs. – tgraupmann Jul 17 '20 at 19:52
  • This video is the most comprehensive instructions for building shared and static versions of OpenSSL - https://www.youtube.com/watch?v=PMHEoBkxYaQ - This has all the steps and way better than the OpenSSL instructions and Windows notes. – tgraupmann Jul 19 '20 at 16:49
7

One more addition to the Simple Guy's answer: If you use http_listener class in your project it is likely you will need Httpapi.lib to fix linker errors.

And also I want to propose an easier way to compile a static cpprestdk library. I've successfully used it to build my project in VS2017.

  1. install vcpkg from https://github.com/Microsoft/vcpkg
  2. Compile cpprestsdk using following commands:

    Step 1. vcpkg install cpprestsdk:x86-windows-static

    Step 2. vcpkg install cpprestsdk:x64-windows-static

  3. Follow the second part of Simple Guy's answer starting from your project settings. Additionally, use my advice from above and BobC to fix linker errors.

Enjoy!

Om Choudhary
  • 492
  • 1
  • 7
  • 27
AlexS
  • 71
  • 1
  • 3
  • Thank you. Since compression is not excluded during compilation of cpprestsdk using vcpkg, zlib.lib also needs to be added. – Rami A. Feb 27 '18 at 09:58
  • I used your approached and linked the resulting library but I get link time errors, should I only link to the resulting `cpprest_2_10.lib` library? Do I need to add additional libs? – Rathma Apr 09 '18 at 10:58
  • added httpapi.lib in my project for using cpprestsdk 2.10.x, it resolve my last linker error :) – hghew Sep 21 '18 at 06:53
  • If anyone else is working on a project that requires runtime library of multi-threaded debug for Debug configuration and multi-threaded for Release and ran into "mismatched runtime library" errors, you can add `x64-windows-static` to .vcproj as shown in https://devblogs.microsoft.com/cppblog/vcpkg-updates-static-linking-is-now-available/ to fix the issues. Release will use the static build. – MakotoE Nov 22 '19 at 20:22
5

In addition to what Simple Guy said, you will probably need to add bcrypt.lib to the Linker->Input->Additional Dependencies since the OAuth1.obj file in CPPRest depends on some of those functions.

BobC
  • 91
  • 3