1

Issue:

I've recently started developing (C++) on Win10. I have a CMake project that builds perfectly fine (both debug and release build), however if I try to run it with the intention of debugging, I get VCRUNTIME140_1D.dll missing error.

The issue lies in the absence of the debug version of the VC++ runtime 140.1 (hence the D prefix in the error message).

I start the binary in two ways:

  • PowerShell - where it silently crashes (it's a very typical uncooperative behaviour in PowerShell that I still need time to get used to)
  • Debugging mode in VS2017 - for the actual debugging. This is where the actual error (in the form of a message box) appears on my screen.

Setup:

Using Visual Studio 2017, Win10. Debugging is required during the development stage but later on a release build will be created and shipped to the customer.

Using CMake 3.12 (shipped with VS2017). Full CMakeLists.txt can be seen at the end.

The libraries I am linking against are

  • libtorch 1.12.1 - latest version of libtorch C++ binaries with dependencies (straight from the official website of PyTorch). Currently unable to identify compiler.
  • opencv 4.6.0 - latest version of OpenCV binaries (straight from the official website of OpenCV). Two versions available - VC14 and VC15. Using VC15, assuming that it refers to VC++ shipped with Visual Studio 2015, that is v140/v140.1 of the MSVC toolkit.

Both dependencies are available in debug and release versions. However I would like to (if possible) link against the release versions of the 3rd party libraries and concentrate on debugging my own code (the size of libtorch's debug build is insane - for both CPU and CUDA you get whopping 12GB!).

Ideas:

I looking on my C:\ and all I can find were

  • vcruntime140d.dll
  • vcruntime140.dll
  • vcruntime140_1.dll

I also read that /MD vs /MDd as flags play an important role.

I've encountered a couple of posts online mentioning that I need to install Visual Studio 2019.

Questions:

Following two questions are important here:

  • Which components do I need to install (VS2019 or otherwise) in order to obtain just this DLL and is it sufficient to have it in order to be able run in debug mode?

  • Can I use (for debugging only) a VC++ Runtime shipped with VS2019 Community Edition in a VS2017 Professional Edition project? Currently I have only access to the Pro version of VS2017.


My project file in CMake can be seen below:

CMakeLists.txt

cmake_minimum_required (VERSION 3.12 FATAL_ERROR)

project(pytroch
  DESCRIPTION "CMake example for PyTorch (libtorch C++) integration"
  LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 14)

set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")

set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/debug/cpu/share/cmake/Torch")
#set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/release/cpu/share/cmake/Torch")
find_package(Torch REQUIRED)
if(TORCH_FOUND)
    message(STATUS "Found Torch")
else()
    message(CRITICAL_ERROR "Unable to find Torch")
endif(TORCH_FOUND)

set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/deps/opencv/4.6.0/")
find_package(OpenCV 4.6.0 REQUIRED)

add_library(pytorch_cv_utils SHARED "${SRC_DIR}/pytorch_cv_utils.cpp")
target_include_directories(pytorch_cv_utils PUBLIC  ${INCLUDE_DIR} ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
target_link_libraries(pytorch_cv_utils PUBLIC ${TORCH_LIBRARIES} ${OpenCV_LIBS})

add_executable(pytroch_load_model
    "${SRC_DIR}/pytroch_load_model.cpp"
)
target_include_directories(pytorch_cv_utils PUBLIC  ${INCLUDE_DIR} ${TORCH_INCLUDE_DIRS})
target_link_libraries(pytroch_load_model PRIVATE pytorch_cv_utils)

# Copy Torch dependencies to binary folder
file(GLOB LIBTORCH_DLLS
#  "${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/release/cpu/lib/*.dll"
  "${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/debug/cpu/lib/*.dll"
)
file(COPY
    ${LIBTORCH_DLLS}
    DESTINATION "${CMAKE_BINARY_DIR}/bin/"
)

# Copy OpenCV dependencies to binary folder
file(GLOB OPENCV_DLLS
  "${CMAKE_SOURCE_DIR}/deps/opencv/4.6.0/x64/vc15/bin/*.dll"
)
file(COPY
    ${OPENCV_DLLS}
    DESTINATION "${CMAKE_BINARY_DIR}/bin/"
)
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • 1
    Just to clarify, you're trying to run (or debug) your own application, on your development computer? Then you should already have the debug "runtime", as it should be part of the Visual Studio installation. – Some programmer dude Oct 18 '22 at 11:18
  • Yes, I want to debug on my own computer and then ship a release build. As far as I know one cannot ship the debug version of VC++ Runtime to end users anyway. – rbaleksandar Oct 18 '22 at 11:20
  • Perhaps the problem is that you need to set the (non-VS) environment to sset the `PATH` etc.? If running from the command line, there's usually a special entry in the start-menu to start a command-line environment with the correct environment set. IIRC there's also a BAT file that can be invoked to set the environment as well. If you run from inside Visual Studio that environment should already be set up for you. Does it work when you run from inside VS? – Some programmer dude Oct 18 '22 at 11:21
  • I get this error when running the binary in debug mode in VS2017 itself. In PowerShell Windows doesn't even bother to tell me anything and just crashes silently (something I really had to get used to coming from Linux terminal). – rbaleksandar Oct 18 '22 at 11:23
  • I also cannot find the `vcruntime140_1d` DLL anywhere meaning that `PATH` or not `PATH`, this problem will always be there. For the development stage of my software and the debugging that goes along with it I expect to have to tackle only 3rd party dependencies that are not related to Win10 SDKs, VC runtimes and what not. E.g. in my case such dependecies are OpenCV and libtorch. – rbaleksandar Oct 18 '22 at 11:25
  • Are you using binary downloads of the external libraries you use? Or did you build them from source? If you use binary packages, how were they built? By which compiler? By which *version* of the compiler? If the libraries are built with a newer version of MSVC then they might use other DLL's than your VS17 installation provides. – Some programmer dude Oct 18 '22 at 11:30

3 Answers3

2

TL;DR: Move to VS 2019 or VS 2022.

The current generation of the Microsoft Visual C/C++ Runtime has binary compatibility for VS 2015 Update 3, VS 2017, VS 2019, and VS 2022. This means that a newer version of the DLLs works with libraries and executables built with older versions of this tool series.

For retail distribution (i.e. just the Release versions) you have a few options as noted Microsoft Docs. You can obtain the latest REDIST packages from here: X64, X86, and ARM64.

The DEBUG versions of the CRT binaries are only available with Visual Studio, but you can use the VS 2019 or VS 2022 Build Tools Edition to obtain them. You cannot redistribute the DEBUG versions of the CRT with your application.

vcruntime140_1.dll was added in Visual Studio 2019 Update 2. It's there to support the new x64 exception handler. Note if you build with the /d2FH4-, this requirement is removed but I suspect the issue is that you are using a static C++ library built for VS 2019 with the VS 2017 toolset/linker. This is not a supported scenario per the binary compatibility rules.

All versions of VS 2017 are out of mainstream support as of April 2022.

Whether or not you can use the VS Community Edition depends on your situation. The feature set is identical to VS Professional, but you need to meet the requirements of the license agreement.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81
  • Note: The "Build Tools Edition" https://visualstudio.microsoft.com/downloads/ is an installer for Debug dll's and various products updates including VS 2019 community edition. – SHS Mar 21 '23 at 11:09
0

Installing VS2019 solved the issue. I am still using VS2017 but now the vcruntime140_1d.dll is found alongside the rest. Final product will of course not shipped with any debug version dependencies and only a release version will be given to the customer.

I will open a new question regarding licensing issues when combining debug library shipped with community edition with a professional edition project for commercial applications.

I am not sure as to why Microsoft has decided to make a debug version of the runtime for something that came out in 2015 available in a 2019 edition but not in the 2017 one.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • I know I cannot ship the debug versions (as stated in my comment above and in my question). As for mixing debug with release builds - yes, I am aware that there are differences but there are many cases that those difference can be neglected (and there are of course many, where those cannot). If you would like to, I have posted a question regarding the licensing dilemma I am facing here. :D – rbaleksandar Oct 18 '22 at 12:34
  • 1
    You probably received `vcruntime140_1d.dll` by coincidence, not intentionally. Microsoft's tools themselves are also built with Visual Studio, and due to the obvious chicken-and-egg problem some parts of newer Visual Studio versions have to be compiled with older versions. – MSalters Oct 18 '22 at 15:15
  • @MSalters What do you mean by coincidence? While it may be just sloppy work on MS' side, installing VS2019 does ship the required library, while VS2017 does not. For that I would not call it a coincidence. – rbaleksandar Oct 18 '22 at 16:36
  • In addition, vscruntime140 could be found on [Microsoft download site](https://www.microsoft.com/en-us/download/details.aspx?id=52685) . Please consider accepting it as an answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Minxin Yu - MSFT Oct 20 '22 at 07:37
  • This also worked for me. I *have* to use 2015 Pro SP 3. I got an error message running debug/x64 builds that says VCRUNTIME140_1D.dll is missing. I installed 2019 Community. Now when using 2015 I no longer get the error. – Humphrey Winnebago May 30 '23 at 23:43
  • Btw, `vcruntime140_1` didn't came in 2015. `vcruntime140` did. If I recall right the `_1` is some special verison which adressed memory management and it came out after VS2017 was finalized. It _is_ available in VS2017, but only as a NuGet package, as many other post-production tools ) – Swift - Friday Pie Aug 10 '23 at 07:37
0

It sounds like some library you're using is compiled using a newer SDK version.

It's not necessary to install whole VS2019 to get the SDk, there are several ways to do so. THe simplest is to have required components can be installed as NuGet package, a live Internet connection is required:

  1. You have to go to menu Tools|NuGet package Manager|Package Manager Console.

  2. While having your solution open, you have to enter command in appeared window:

    NuGet\Install-Package Microsoft.Windows.SDK.CPP.x64 -Version 10.0.22621.755

P.S. Never, EVER download and install .dlls from arbitrary websites offering such. That's how malware infiltrates software creaed by fair devolopers. In some cases you may become legally accountable for crafting software with such components in tow.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42