Search code examples
c++curlstatic-librarieslibcurlstatic-linking

Can't run my compiled c++ executable on another machine. Says "libcurl.dll not found"


I'm trying to statically link the libcurl library into my executable so I can run my executable on other machines without need of dll files since all the necessary files are baked inside my executable

I've installed curl from vcpkg

vcpkg install curl

Here are the screenshots of \include and \lib folders

Configured my C++ project in Visual Studio to statically link libcurl:

Configuration Properties: [Release]

  • C/C++,
    • General, Additional Include Dirs -> C:\dev\vcpkg\installed\x64-windows\include
    • Preprocessor, Preprocessor Definitions -> CURL_STATICLIB
    • Code Generation, Runtime Library -> Multi-threaded (/MT)
  • Linker:
    • General, Additional Library Dirs -> C:\dev\vcpkg\installed\x64-windows\lib
    • Input, Additional Dependencies -> libcrypto.lib; libssl.lib; libcurl.lib

Building the solution in Release mode, Visual Studio compiling it without any issues. Executable runs ok on my machine.

But doesn't run another machine:

The code execution cannot proceed because libcurl.dll was not found.
Reinstalling the program may fix this problem.

I wonder what I'm doing wrong? Maybe curl cannot be statically combined? If theres something wrong with my config, then why do other libraries I'm including doesn't raise an error?


Also, what other libraries you would recommend for making http requests in c++? I want easy-to-use ones that can be statically compiled into an executable without any issues.


UPDATE

Reading the guide of @DeanVanGreunen posted in the comments of this post, I've realized that I can install x64-windows-static versions of packages from vcpkg. So, I've installed curl:x64-windows-static which installed the 'static' version of curl into separate x64-windows-static folder inside the vcpkg directory.

I configured project solution to link libcurl.lib located in the folder. I tried both manual (tweaking the configuration properties) and automatic (bootstapping by vcpkg integrate install command and changing the .vcxproj file according to the guide) linking solutions. Both didn't work, now I can't even build the application!

Here is the build output log: https://pastebin.com/JVx4Z2P4


Solution

  • Thanks to the @Alan Birtles for leaving the link to the github/vcpkg issue page in the comments, that solved the problem. I will be leaving step-by-step solution to the problem here for people who face the same issue in the future.


    Solution

    1. Install x64-windows-static versions of packages using vcpkg:

      vcpkg install curl:x64-windows-static
      
    2. Change include and library directory paths in configuration properties:

      • from ...\vcpkg\installed\x64-windows\...
      • to ...\vcpkg\installed\x64-windows-static\...

      If you have enabled vcpkg integration with Visual Studio, you just need to edit .vcxproj file according to the guide (thanks to @Dean Van Greunen for suggestion):

      Find <PropertyGroup Label=”Globals”> tag and include this inside:

      <VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
      

      And you are ready to go!

    3. Link additional neccesary libraries that are needed to successfully link static libraries. You can achieve this using auto-linking in your code:

      #pragma comment(lib, "Ws2_32.Lib")
      #pragma comment(lib, "Wldap32.Lib")
      #pragma comment(lib, "Crypt32.Lib")
      
    4. Compile and enjoy!