Cross compile windows 64 bit .exe from linux
Asked Answered
S

3

11

I know that if I want to compile a 32 bit .exe for windows on Linux I can just install and use the mingw32 package (e.g. apt-get install mingw32) on linux. What if I want to compile a windows .exe that is 64 bit? Is there tools or a method to do this?

Sonar answered 22/4, 2010 at 10:3 Comment(3)
@Hannes de Jager : Can you please tell me how to compile after apt-get install mingw32 inorder to produce exe? Can you also please try to answer my questions #2733902 and #2731711 and #2710089Dnieper
i586-mingw32msvc-cc helloworld.c -o helloworld.exeSonar
and if you want to test it under linux: wine helloworld.exe (assuming you have wine installed)Sonar
S
6

It looks like my answer lies with the Mingw-w64 project which is available for host OSes Linux, Darwin & Windows

Sonar answered 22/4, 2010 at 16:0 Comment(0)
S
2

I know this question is very old and already has an accepted answer but I will post this answer the way I do it now. It is pretty simple and straightforward and I hope it helps anyone landing here:

To cross-compile windows applications from a Linux machine you just need to install mingw-w64 C and C++ compiler. On debian based system you just do this:

sudo apt install -y gcc-mingw-w64 g++-mingw-w64

That will install the gcc (the c compiler) and g++ (the c++ compiler) for both 64 and 32 bit cross-compilation

After that, if you need a 64-bit application just do:

x86_64-w64-mingw32-g++ hello.c -o hello.exe

And if you need a 32-bit application just do:

i686-w64-mingw32-g++ hello.c -o hello.exe

Simple as that!

Stillborn answered 27/1, 2023 at 16:19 Comment(0)
O
0

It's also possible to install MinGW from MSYS2. The main advantages are:

  • Usually up-to-date MinGW, regardless of what your Linux distribution ships.
  • A lot of prebuilt libraries.

Several MinGW flavors are provided: x32 and x64, with different C runtimes, etc.

You can't install MSYS2 on Linux directly, but it's possible with Quasi-MSYS2.

  1. Install Clang (and LLD):

    On Ubuntu:

    wget https://apt.llvm.org/llvm.sh
    chmod +x llvm.sh
    sudo ./llvm.sh
    rm llvm.sh
    

    Clang can cross-compile to Windows using MSYS2 libraries. Alternatively, you can run MSYS2 MinGW in Wine, but it's slower.

  2. Install dependencies:

    sudo apt install make wget tar zstd gpg wine
    

    Wine is optional.

  3. Install Quasi-MSYS2 and any desired MSYS2 packages:

    git clone https://github.com/HolyBlackCat/quasi-msys2
    cd quasi-msys2/
    # Optionally, choose MSYS2 flavor, see full list at: https://www.msys2.org/docs/environments/
    # echo MINGW64 >msystem.txt
    make install _gcc _gdb
    

    Then:

  4. env/shell.sh opens a shell with the correct environment variables set up.

  5. win-clang++ hello.cpp invokes Clang with flags for cross-compilation.

  6. ./a.exe runs the resulting app in Wine, if it's installed.


Full disclosure: I'm the developer of quasi-msys2.

Olympic answered 27/1, 2023 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.