Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save eladkarako/83ad9b59ec601c831c430866f3dc11b7 to your computer and use it in GitHub Desktop.

Select an option

Save eladkarako/83ad9b59ec601c831c430866f3dc11b7 to your computer and use it in GitHub Desktop.
cross compile toolkit for linux (wsl too). `sudo chmod u+x` both, execute by `source ./set_export.sh` to make them effect current shell. you can start with `unset_export.sh` first
  1. sudo apt update

  2. sudo apt install --yes curl

  3. cd ~

  4. curl --insecure --anyauth --ssl-no-revoke --location-trusted --remote-time --url "https://gist.github.com/eladkarako/83ad9b59ec601c831c430866f3dc11b7/raw/afa9da76793c768841aaebd99c2ca601f29263e5/unset_export.sh" --output "unset_export.sh"

  5. curl --insecure --anyauth --ssl-no-revoke --location-trusted --remote-time --url "https://gist.github.com/eladkarako/83ad9b59ec601c831c430866f3dc11b7/raw/afa9da76793c768841aaebd99c2ca601f29263e5/set_export.sh" --output "set_export.sh"

  6. sudo chmod u+x "unset_export.sh" "set_export.sh"

(optional source ./unset_export.sh )

  1. source ./set_export.sh

sample compilation (for simple cases):

  1. autoreconf -fi

  2. make clean || true

  3. ./configure mingw64 --host="${HOST}" --target="${HOST}" --prefix="${HOME}/.local/${HOST}" --cross-compile-prefix="${HOST}" --libdir="${HOME}/.local/${HOST}/lib" --disable-shared --enable-static LIBS="-lws2_32"

(here is another variation: ./configure --host="${HOST}" --target="${HOST}" --prefix="${HOME}/.local/${HOST}" --build=x86_64-pc-linux-gnu

note: LIBS="-lws2_32" is just for programs you need to link to Winsocket2 in Windows.

you can link more than just Winsocket2
LIBS += -lssl -lcrypto -lws2_32 -lgdi32 -ladvapi32 -lcrypt32 -luser32 WS2_32.LIB, GDI32.LIB, ADVAPI32.LIB, CRYPT32.LIB and USER32.LIB (Those developing non-interactive service applications might feel concerned about linking with GDI32.LIB and USER32.LIB, as they are justly associated with interactive desktop, which is not available to service processes. The toolkit is designed to detect in which context it's currently executed, GUI, console app or service, and act accordingly)

to find out if there is a need to, change director to source top most directory and run:
grep -R --line-number -E "winsock|winsock2|WSAStartup|WSACleanup|getaddrinfo|socket|recvfrom|sendto"
that's a pretty good indication if you need to add LIBS="-lws2_32". note it will not matter if you don't need it and it is linked.
you will just see it under dependency walker.

for openssl you'll do something like this instead:
perl Configure mingw no-shared --prefix="${INSTALL_PREFIX}" LIBS="-lws2_32"

  1. make -j$(nproc)

  2. (optional) make install

#---------------------------------------------------------------------------------------------#
# make sure this file is LINUX EOL (\n) and not WINDOWS EOL (\r\n) if you edit it in Windows. #
#---------------------------------------------------------------------------------------------#
set -o nounset #treat unset variables as an error (ON set -u OFF set +u).
set -o errexit #exit on any command failure (set -e).
set -o pipefail #make a pipeline fail if any component fails.
#set -o xtrace #print each command before executing it (set -x).
set -o verbose #print shell input lines as read (set -v).
#set -o noexec #read commands but do not execute (syntax check) (set -n).
set -o noglob #disable filename globbing (wildcards) (set -f).
set -o notify #notify of job completion immediately (set -b).
set -o noclobber #prevent > from overwriting existing files (set -C).
#-------------------------------------------------- Target host (cross-compile triplet)
#export HOST="i686-w64-mingw32"
export HOST="x86_64-w64-mingw32"
#-------------------------------------------------- Cross-tool prefix and installation prefix for staged sysroot
export CROSS_PREFIX="${HOST}-"
export INSTALL_PREFIX="${HOME}/.local/${HOST}" #I prefer full path. keep it, used in many scripts.
export SYSROOT="${HOME}/.local/${HOST}" #same. I prefer full path. keep it, used in many scripts.
#-------------------------------------------------- Toolchain binaries (use cross-prefixed tools)
export ADDR2LINE="${CROSS_PREFIX}addr2line" # convert addresses to source locations
export AR="${CROSS_PREFIX}ar" # archive librarian
export CC="${CROSS_PREFIX}gcc" # C compiler
export CPP="${CROSS_PREFIX}cpp" # C preprocessor
export CXX_POSIX="${CROSS_PREFIX}c++-posix" # posix variant of C++ compiler, if available
export CXX="${CROSS_PREFIX}g++" # C++ compiler
export DLLTOOL="${CROSS_PREFIX}dlltool" # create import libraries
export DLLWRAP="${CROSS_PREFIX}dllwrap" # wrap DLLs (optional)
export ELFEDIT="${CROSS_PREFIX}elfedit" # edit ELF files
export GCC_AR="${CROSS_PREFIX}gcc-ar" # gcc wrapper for ar (for LTO)
export GCC_NM="${CROSS_PREFIX}nm" # name lister (gcc-aware)
export GCOV_TOOL="${CROSS_PREFIX}gcov-tool" # gcov tool for coverage
export GCOV="${CROSS_PREFIX}gcov" # coverage analyzer
export GPROF="${CROSS_PREFIX}gprof" # profiler
export GXX="${CROSS_PREFIX}g++" # alt C++ compiler name
export LD="${CROSS_PREFIX}ld" # linker
export LTO_DUMP="${CROSS_PREFIX}lto-dump-posix" # LTO dump tool (posix)
export NM="${CROSS_PREFIX}nm" # symbol table dumper
export OBJCOPY="${CROSS_PREFIX}objcopy" # copy and translate object files
export RANLIB="${CROSS_PREFIX}ranlib" # index archives
export READELF="${CROSS_PREFIX}readelf" # display ELF info
export SIZE="${CROSS_PREFIX}size" # list section sizes
export STRINGS="${CROSS_PREFIX}strings" # print strings in binaries
export STRIP="${CROSS_PREFIX}strip" # strip symbols
export WIDL="${CROSS_PREFIX}widl" # IDL compiler (Wine)
export WINDMC="${CROSS_PREFIX}windmc" # message compiler (Windows)
export WINDRES="${CROSS_PREFIX}windres" # resource compiler
export PKG_CONFIG="${CROSS_PREFIX}pkg-config" # pkg-config wrapper for cross prefix
#-------------------------------------------------- search path for pkg-config .pc files
if [ "${PKG_CONFIG_PATH+defined}" = defined ] && [ -n "${PKG_CONFIG_PATH}" ]; then
# defined and not empty
export PKG_CONFIG_PATH="${HOME}/.local/${HOST}/lib/pkgconfig:${PKG_CONFIG_PATH}"
else
# either undefined or empty — set to INSTALL_PREFIX only
export PKG_CONFIG_PATH="${HOME}/.local/${HOST}/lib/pkgconfig"
fi
#-------------------------------------------------- Preprocessor and include paths.
export CPPFLAGS="-I. -I${HOME}/.local/${HOST}/include -I${HOME}/.local/${HOST}/usr/include -I/usr/${HOST}/include" # add current dir and staged include dir
#-------------------------------------------------- Compiler flags: sysroot, staged includes, tuning and optimization
export CFLAGS="--sysroot=${HOME}/.local/${HOST} -I. -I${HOME}/.local/${HOST}/include -I${HOME}/.local/${HOST}/usr/include -I/usr/${HOST}/include -mtune=generic -O2 -g0"
#-------------------------------------------------- Linker flags: set sysroot and staged lib dir
export LDFLAGS="--sysroot=${HOME}/.local/${HOST} -L. -L${HOME}/.local/${HOST}/lib -L${HOME}/.local/${HOST}/usr/lib -L/usr/${HOST}/lib"
#-------------------------------------------------- Native compilers used to build host tools (not the target)
export CC_FOR_BUILD="gcc"
export CXX_FOR_BUILD="g++"
#-------------------------------------------------- Configure flags for distcheck
export DISTCHECK_CONFIGURE_FLAGS="--disable-check"
#-------------------------------------------------- Optional: create symlink for windres in staged bin (uncomment to enable)
####ln -s "/usr/bin/${WINDRES}" "${HOME}/.local/${HOST}/bin/windres"
#-------------------------------------------------- Ensure core system and staged bin come before user PATH entries
#export PATH="/usr/bin:${HOME}/.local/${HOST}/bin:${PATH}"
#-------------------------------------------------- optional. minimal PATH. since WSL also carry the entire PATH of Windows, there is a chance of breaking stuff while building due to lanching by mistake tools at same name from Windows (Visual Studio). this is minimal set of PATH folders needed. you can comment it and uncomment the above line which will just put stuff before older PATH entries.
unset PATH
export PATH="/usr/bin:${HOME}/.local/${HOST}/bin:${HOME}/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib/snap/bin"
#--- have to turn it off when using source to this file, otherwise exit codes other than zero, will exit your wsl or shell ;)
set +o nounset #treat unset variables as an error (ON set -u OFF set +u).
set +o errexit #exit on any command failure (set -e).
set +o pipefail #make a pipeline fail if any component fails.
#set +o xtrace #print each command before executing it (set -x).
set +o verbose #print shell input lines as read (set -v).
#set +o noexec #read commands but do not execute (syntax check) (set -n).
set +o noglob #disable filename globbing (wildcards) (set -f).
set +o notify #notify of job completion immediately (set -b).
set +o noclobber #prevent > from overwriting existing files (set -C).
#
set -o nounset #treat unset variables as an error (ON set -u OFF set +u).
set -o errexit #exit on any command failure (set -e).
set -o pipefail #make a pipeline fail if any component fails.
#set -o xtrace #print each command before executing it (set -x).
set -o verbose #print shell input lines as read (set -v).
#set -o noexec #read commands but do not execute (syntax check) (set -n).
set -o noglob #disable filename globbing (wildcards) (set -f).
set -o notify #notify of job completion immediately (set -b).
set -o noclobber #prevent > from overwriting existing files (set -C).
unset CC_FOR_BUILD
unset CXX_FOR_BUILD
unset CFLAGS
unset HOST
unset CROSS_PREFIX
unset INSTALL_PREFIX
unset ADDR2LINE
unset AR
unset CC
unset CPP
unset CXX_POSIX
unset CXX
unset DLLTOOL
unset DLLWRAP
unset ELFEDIT
unset GCC_AR
unset GCC_NM
unset GCOV_TOOL
unset GCOV
unset GPROF
unset GXX
unset LD
unset LTO_DUMP
unset NM
unset OBJCOPY
unset RANLIB
unset READELF
unset SIZE
unset STRINGS
unset STRIP
unset WIDL
unset WINDMC
unset WINDRES
unset PKG_CONFIG
unset PKG_CONFIG_PATH
unset CPPFLAGS
unset LDFLAGS
####unset PATH
#--- have to turn it off when using source to this file, otherwise exit codes other than zero, will exit your wsl or shell ;)
set +o nounset #treat unset variables as an error (ON set -u OFF set +u).
set +o errexit #exit on any command failure (set -e).
set +o pipefail #make a pipeline fail if any component fails.
#set +o xtrace #print each command before executing it (set -x).
set +o verbose #print shell input lines as read (set -v).
#set +o noexec #read commands but do not execute (syntax check) (set -n).
set +o noglob #disable filename globbing (wildcards) (set -f).
set +o notify #notify of job completion immediately (set -b).
set +o noclobber #prevent > from overwriting existing files (set -C).
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment