简体   繁体   English

如何在VS项目中静态链接没有dll文件的cpprest?

[英]How to statically link cpprest without dll files in VS project?

I finally linked Casablanca/CPPRESTSDK with VS17, thanks for this post Statically linking Casablanca/CPPREST SDK and some anothers. 最后,我将Casablanca / CPPRESTSDK与VS17链接在一起,感谢这篇文章静态地将Casablanca / CPPRESTSDK与其他链接。 But now I see, that when I start project, for some reason VS automatically generates 3 DLL files zlib1.dll SSLEAY32.dll LIBEAY32.dll in folder with .exe, but I actually linked staticly for not to have them at all. 但是现在我看到,当我开始项目时,由于某种原因,VS LIBEAY32.dll在.exe文件夹中自动生成3个DLL文件zlib1.dll SSLEAY32.dll LIBEAY32.dll ,但是我实际上是静态链接的,根本没有它们。 I also have these files in lib format in vcpkg-master\\installed\\x86-windows-static\\lib , can I somehow use them instead of dlls? 我在vcpkg-master\\installed\\x86-windows-static\\lib也有lib格式的这些文件,我可以以某种方式代替dll使用它们吗?

I had the same problem. 我有同样的问题。 I have a Visual Studio solution with projects using MFC in a shared DLL and using the multi-threaded DLL runtime library. 我有一个Visual Studio解决方案,其中的项目使用共享DLL中的MFC并使用多线程DLL运行时库。 I wanted to link statically to cpprestsdk without three extra DLLs to distribute. 我想静态链接到cpprestsdk,而无需分发三个额外的DLL。 Linking to the static version of cpprestsdk wasn't working: I couldn't link because of conflicting runtime libraries ( /MT - multithreaded and /MD - multi-threaded DLL). 链接到cpprestsdk的静态版本不起作用:由于运行时库冲突( /MT多线程和/MD多线程DLL),我无法链接。 I couldn't find a pre-compiled version which used the /MD switch (multi-threaded DLL), but exported as a library. 我找不到使用/MD开关(多线程DLL)但导出为库的预编译版本。 So I had to compile it myself. 所以我必须自己编译。

My solution: 我的解决方案:

  • download vcpkg ( https://github.com/microsoft/vcpkg ). 下载vcpkghttps://github.com/microsoft/vcpkg )。 This is a package C and C++ library package manager. 这是一个程序包C和C ++库程序包管理器。 You will need to 'bootstrap' it. 您将需要“引导”它。 See the Quick Start. 请参阅快速入门。
  • install necessary dependencies for cpprestsdk: `vcpkg install --triplet x86-windows zlib openssl boost-system boost-date-time boost-regex boost-interprocess websocketpp brotli 为cpprestsdk安装必要的依赖项:`vcpkg install --triplet x86-windows zlib openssl boost-system boost-date-time boost-regex boost-interprocess websocketpp brotli
  • download cpprestsdk from github ( https://github.com/Microsoft/cpprestsdk ) 从github下载cpprestsdk( https://github.com/Microsoft/cpprestsdk
  • generate a Visual Studio solution file ( https://github.com/microsoft/cpprestsdk/wiki/How-to-build-for-Windows ). 生成一个Visual Studio解决方案文件( https://github.com/microsoft/cpprestsdk/wiki/How-to-build-for-Windows )。 I wanted to generate an x86 version, so I had to use the following command cmake ../Release -A win32 -DCMAKE_TOOLCHAIN_F ILE=d:\\jw\\git\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake . 我想生成一个x86版本,所以我不得不使用以下命令cmake ../Release -A win32 -DCMAKE_TOOLCHAIN_F ILE=d:\\jw\\git\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake
  • open cpprestsdk.sln solution and do the following for Release and Debug configurations in the cpprest project: 打开cpprestsdk.sln解决方案,并对cpprest项目中的Release和Debug配置执行以下操作:
    • change the configuration type to Static library 将配置类型更改为静态库
    • change the target file extension to .lib . 将目标文件扩展名更改为.lib
    • build debug and release versions. 构建调试和发行版本。

I could then use the generated libraries in my solution. 然后,我可以在解决方案中使用生成的库。

  • add the cpprestsdk include directory to my project 将cpprestsdk包含目录添加到我的项目中
  • add the cpprestsdk libraries to the linker Input 将cpprestsdk库添加到链接器输入
  • add the zlib and openssl libraries from the cpprestsdk packages directory. 从cpprestsdk packages目录添加zlib和openssl库。
  • add the libraries bcrypt.lib , winhttp.lib and crypt32.lib to the linker Input too ( Statically linking Casablanca/CPPREST SDK ) 也将库bcrypt.libwinhttp.libcrypt32.lib到链接器输入( 静态链接Casablanca / CPPREST SDK
  • you also need to add the preprocessor flag _NO_ASYNCRTIMP to the project where you use cpprestsdk. 您还需要将预处理器标志_NO_ASYNCRTIMP添加到使用cpprestsdk的项目中。

I hope this helps someone save some of the hours I lost getting it to work! 我希望这可以帮助某人节省我失去的一些工作时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM