skip to Main Content

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:devvcpkginstalledx64-windowsinclude
    • Preprocessor, Preprocessor Definitions -> CURL_STATICLIB
    • Code Generation, Runtime Library -> Multi-threaded (/MT)
  • Linker:
    • General, Additional Library Dirs -> C:devvcpkginstalledx64-windowslib
    • 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

2

Answers


  1. Chosen as BEST ANSWER

    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 ...vcpkginstalledx64-windows...
      • to ...vcpkginstalledx64-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!


  2. If you already succeeded with dynamic linking then your UPDATE to static linking is a suitable workaround, but still is not the answer. The professional way is to keep dynamic linking as it was and use dlls. You already succeeded to build them dynamically, so keep it this way. There is no need and no good reason to inflate your exe.
    First of all, from this Says "libcurl.dll not found", it states explicitly that libcurl.dll can’t be found. But it is there, in the place where libcurl is installed. So, make the dlls visible to your application. In the place where libcurl is installed, find the directory containing libcurl.dll, for instance it is located here c:pathlibcurldlls, maybe somewhere else. Now you choose one of Options which is best to you.
    Option 1, before launching your application, update the system path, it is ";" separated

    set PATH=%PATH%;c:pathlibcurldlls
    yourapplication.exe #now it should not fail
    

    Option 2, in Windows Settings search for System Variables and open the Edit System Environment Variables
    enter image description here
    Click environment variables and add your dll path to Path
    enter image description here
    You can add it to User path if you want it to be visible only under your user, or to System path if you want it to be visible for all users. Windows merges the two Path variables before running an application. Now you will be able to run your application.
    Option 3 Don’t alter path, but place your curl dll files into some place where your path already points. Now you will be able to run your application.
    Option 4 Place your dll files in the same folder with your compiled exe file. Now you will be able to run your application.
    Option 5 Make it visible to your IDE, for instance if it is VisualStudio, open the project properties, then in Debugging Options add your variable Path, inherit it from System Defaults, then add path to dlls there. Then you will be able to run it from your IDE
    enter image description here
    Option 6 If you want to distribute your application, package it with copies of curl and other dll files, in the same folder with your exe. Now you will be able to run your application on a different computer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search