Skip to content

Instantly share code, notes, and snippets.

@soda92
Created November 28, 2024 09:42
Show Gist options
  • Save soda92/9d056425b3c74a1028acdb54b84c80c0 to your computer and use it in GitHub Desktop.
Save soda92/9d056425b3c74a1028acdb54b84c80c0 to your computer and use it in GitHub Desktop.
package C++ application

You can use ldd to view dependencies, then copy the related dll: https://web.archive.org/web/20240620214721/https://janw.name/posts/3-msys2-dlls/

Here is an example makefile (please use the code in solution https://stackoverflow.com/a/60906375/12291425, or use this gist):

it can save a hassle; create a file named GNUmakefile or Makefile and paste the content, then run make dist:

targets := prog.exe my_library.dll my_plugin.dll

CXX := g++
CXXFLAGS :=  -c   -g -O0  -MMD -pedantic -Wall -Wextra -Wconversion -Wno-unused -Wno-unused-parameter -Werror -Wfatal-errors -UNDEBUG  -std=c++17 -Wno-missing-braces -Wno-sign-conversion

all: $(targets)

.PHONY: all

%.dll: %.o
        $(CXX) -shared -g -O0 -o $@ $<

# by implicit rule, my_plugin.o will be compiled from my_plugin.cpp

# by implicit rule, my_library.o will be compiled from my_library.cpp

prog.exe: prog.o my_library.dll
        g++ -o prog.exe prog.o -g -O0 -L. -lmy_library

files := $(wildcard *.dll *.o *.d *.exe)
distfiles := $(wildcard dist/*.dll dist/*.exe)
clean:
        rm $(files) 2>/dev/null || true
        rm $(distfiles) 2>/dev/null || true

.PHONY: clean

dist: all
        rm -r dist 2>/dev/null || true
        mkdir dist 2>/dev/null || true
        cp *.dll *.exe dist
        @# extra $ is needed in awk in makefile
        cd dist; ldd $(targets) | grep /ucrt64 | awk '{print $$3}' | xargs -i cp {} .
        @# if you make sure you are running UCRT, you can remove the following line
        cd dist; ldd $(targets) | grep /mingw64 | awk '{print $$3}' | xargs -i cp {} .
        cd dist; explorer . || true

.PHONY: dist

To clean up, run make clean.

Result:

files

The resulting program can be run in PowerShell/cmd. You can also double click it if it's a GUI application (note that Qt have more related files).

You can also further package your application using Inno Setup or NSIS.

Learn more about makefile here: https://makefiletutorial.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment