DEV Community

DougAnderson444
DougAnderson444

Posted on

Rust Cargo Run exit code: 0xc0000135, STATUS_DLL_NOT_FOUND

This is probably happening in Powershell.

If you run this in bash MINGW64:

$ ldd target/debug/<app>.exe
Enter fullscreen mode Exit fullscreen mode

This should give you a list of .dll files. Those DLLs should be in your target folder, but they aren't.

ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffb1fd10000)
KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffb1fb70000)
KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffb1d5f0000)
apphelp.dll => /c/WINDOWS/SYSTEM32/apphelp.dll (0x7ffb1a980000)
bcrypt.dll => /c/WINDOWS/System32/bcrypt.dll (0x7ffb1d410000)
ucrtbase.dll => /c/WINDOWS/System32/ucrtbase.dll (0x7ffb1dab0000)
VCRUNTIME140.dll => /c/WINDOWS/SYSTEM32/VCRUNTIME140.dll (0x7ffb03f60000)
libssl-1_1-x64.dll => /mingw64/bin/libssl-1_1-x64.dll (0x7ffae26f0000)
msvcrt.dll => /c/WINDOWS/System32/msvcrt.dll (0x7ffb1fad0000)
libcrypto-1_1-x64.dll => /mingw64/bin/libcrypto-1_1-x64.dll (0x7ffaaf360000)
ADVAPI32.dll => /c/WINDOWS/System32/ADVAPI32.dll (0x7ffb1e950000)
sechost.dll => /c/WINDOWS/System32/sechost.dll (0x7ffb1fc30000)
RPCRT4.dll => /c/WINDOWS/System32/RPCRT4.dll (0x7ffb1f0c0000)
USER32.dll => /c/WINDOWS/System32/USER32.dll (0x7ffb1e7a0000)
win32u.dll => /c/WINDOWS/System32/win32u.dll (0x7ffb1da80000)
GDI32.dll => /c/WINDOWS/System32/GDI32.dll (0x7ffb1eaa0000)
gdi32full.dll => /c/WINDOWS/System32/gdi32full.dll (0x7ffb1dc50000)
msvcp_win.dll => /c/WINDOWS/System32/msvcp_win.dll (0x7ffb1dbb0000)
WS2_32.dll => /c/WINDOWS/System32/WS2_32.dll (0x7ffb1fa60000)

Enter fullscreen mode Exit fullscreen mode

See LDD for more details.

Turns out there are 2 ways to link libraries into your rust executable: dynamic (hence DLL: Dynamic Linked Library) and static.

To avoid any DLL issues on the target platform, we can set up our build target to use static linked libraries.

How do we do that for something like OpenSSL?

First we install that static version of OpenSSL for windows:

vcpkg install openssl:x64-windows-static

*Note that "static" word at the end.

Using vcpkg to install this gives us a directory where we can point our system env variables:

$env:OPENSSL_STATIC=1
$env:OPENSSL_NO_VENDOR=1
$env:OPENSSL_DIR="C:\...\<vcpkg>\installed\x64-windows-static"
$env:OPENSSL_INCLUDE_DIR="C:\Users\douga\Documents2\code\vcpkg-1\installed\x64-windows-static\include"
Enter fullscreen mode Exit fullscreen mode

In PowerShell we can set out environment variables to all say "static" and "here's our static installation from vcpkg"

Now when we cargo run our app is built using the static versions of OpenSSL based on out ENV VARS and the installation these point to, meaning our target environment doesn't need these DLL files available and our app will run without them.

Top comments (0)