Skip to main content

Finding and copying DLLs from MinGW-W64 directories to bin directory

Sidebar

After cross-compiling your project for Windows, you find that it crashes due to missing DLLs. I will show how to identify any required DLLs using objdump, and copy them to your build directory.

After cross-compiling your project for Windows, you find that it crashes due to missing DLLs. I will show how to identify any required DLLs using objdump, and copy them to your build directory.

mingw32-objdump can be used to analysis an executable or object. Among other things, it lists the .DLLs which are required by the .EXE. Call it by passing the path to the executable and the -p flag:

x86_64-w64-mingw32-objdump -p project/bin/project.exe

This will output a ton of content, so you’ll want to pass the result through grep and sed to get just the DLL names:

x86_64-w64-mingw32-objdump -p project/bin/project.exe | grep 'DLL Name:' | sed -e "s/\t*DLL Name: //g"

The output will look a bit like this:

libgcc_s_seh-1.dll
KERNEL32.dll
msvcrt.dll
libwinpthread-1.dll
libstdc++-6.dll
WSOCK32.dll
sfgui.dll
sfml-graphics-2.dll
sfml-network-2.dll
sfml-system-2.dll
sfml-window-2.dll
libthor.dll

Now we want to iterate over the result of this, and try and find the DLL in a number of search paths. Here’s the full script to do that:

#!/bin/bash

BINDIR="project/bin"
EXE="$BINDIR/project.exe"
PREFIX="x86_64-w64-mingw32"

paths=("/usr/local/mingw64/bin"
    "/usr/local/mingw64/bin/x64"
     "/usr/$PREFIX/bin"
    "/usr/lib/gcc/$PREFIX/7.3-posix"
    "/usr/$PREFIX/lib")

function findAndCopyDLL() {
    for i in "${paths[@]}"
    do
        FILE="$i/$1"
        if [ -f $FILE ]; then
           cp $FILE $BINDIR
           echo "Found $1 in $i"
           copyForOBJ $FILE
           return 0
        fi
    done

    return 1
}

function copyForOBJ() {
    dlls=`$PREFIX-objdump -p $1 | grep 'DLL Name:' | sed -e "s/\t*DLL Name: //g"`
    while read -r filename; do
        findAndCopyDLL $filename || echo "Unable to find $filename"
    done <<< "$dlls"
}

copyForOBJ $EXE

The output will look like this:

Found libgcc_s_seh-1.dll in /usr/x86_64-w64-mingw32/bin
Unable to find KERNEL32.dll
Unable to find msvcrt.dll
Found libwinpthread-1.dll in /usr/x86_64-w64-mingw32/bin
Found libstdc++-6.dll in /usr/x86_64-w64-mingw32/bin
Unable to find WSOCK32.dll
Found sfgui.dll in /usr/local/mingw64/bin
Found sfml-graphics-2.dll in /usr/local/mingw64/bin
Found sfml-network-2.dll in /usr/local/mingw64/bin
Found sfml-system-2.dll in /usr/local/mingw64/bin
Found sfml-window-2.dll in /usr/local/mingw64/bin
Found libthor.dll in /usr/local/mingw64/bin

Some DLLs won’t be found as they’re provided as part of Windows.

rubenwardy's profile picture, the letter R

Hi, I'm Andrew Ward. I'm a software developer, an open source maintainer, and a graduate from the University of Bristol. I’m a core developer for Luanti, an open source voxel game engine.

Comments

Leave comment

Shown publicly next to your comment. Leave blank to show as "Anonymous".
Optional, to notify you if rubenwardy replies. Not shown publicly.
Max 1800 characters. You may use plain text, HTML, or Markdown.