Skip to content

Tutorial: compile for Windows on Linux with MinGW (cross compile) #168

Closed

Description

@Topping1

First, install MinGW
sudo apt install g++-mingw-w64-x86-64-posix
sudo apt install gcc-mingw-w64-x86-64-posix

Then, clone the repository
git clone https://github.com/ggerganov/whisper.cpp

  • then, for compilation, we need to modify the pthread flag and static link libgcc and libstdc++ as follows:

x86_64-w64-mingw32-gcc-posix -I. -O3 -std=c11 -mfma -mf16c -mavx -mavx2 -c ggml.c -o ggml.o

x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -lwinpthread -c whisper.cpp -o whisper.o

x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -static-libgcc -static-libstdc++ -lwinpthread examples/main/main.cpp ggml.o whisper.o -o main

the compiled executable was tested on an Intel 4th gen CPU and it worked BUT accented characters do not show correctly (not a problem for english). A workaround (the problem might be with MinGW): before using main.exe on a command prompt window, execute the command chcp 65001 (UTF-8 encoding).

Tried to run it on a 3rd gen intel CPU but did not work.

Activity

  1. regstuff commented on Nov 22, 2022

    @regstuff

    Tested successfully on an i5-8300H. Failed on an old Pentium G4400.
    If this works well, I suppose it would be a good idea to setup a Github Workflow to auto-compile whenever this repo is updated.

  2. ggerganov commented on Nov 22, 2022

    @ggerganov
    Member

    @Topping1
    Very nice!
    Can you try cross-compiling using x86_64-w64-mingw32-gcc-posix + -lwinpthread without using mingw-std-threads?

    x86_64-w64-mingw32-gcc-posix -I. -O3 -std=c11 -mfma -mf16c -mavx -mavx2 -c ggml.c -o ggml.o
    
    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -lwinpthread -c whisper.cpp -o whisper.o
    
    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -static-libgcc -static-libstdc++ -lwinpthread examples/main/main.cpp ggml.o whisper.o -o main
    

    I want to avoid adding the extra source files.

    @regstuff
    Yes - having a Github action to build a Windows executable would be great.
    I think there are a lot of people that want to try Whisper on Windows without going through the hassle of compiling it.

  3. Topping1 commented on Nov 22, 2022

    @Topping1
    ContributorAuthor

    @Topping1 Very nice! Can you try cross-compiling using x86_64-w64-mingw32-gcc-posix + -lwinpthread without using mingw-std-threads?

    x86_64-w64-mingw32-gcc-posix -I. -O3 -std=c11 -mfma -mf16c -mavx -mavx2 -c ggml.c -o ggml.o
    
    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -lwinpthread -c whisper.cpp -o whisper.o
    
    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -static-libgcc -static-libstdc++ -lwinpthread examples/main/main.cpp ggml.o whisper.o -o main
    

    I want to avoid adding the extra source files.

    @regstuff Yes - having a Github action to build a Windows executable would be great. I think there are a lot of people that want to try Whisper on Windows without going through the hassle of compiling it.

    I will try and report back. By the way, when browsing https://github.com/ggerganov/whisper.cpp/actions I found the item
    ggml : fix Windows build #225 https://github.com/ggerganov/whisper.cpp/actions/runs/3509700272, and at the end of the page you can find that the "Artifacts Produced during runtime" section contains two automatically compiled windows executables (win32 and win64). I have tried the 64bit version and works ok on windows 10. I guess you already implemented the github action a couple of days ago.

  4. ggerganov commented on Nov 22, 2022

    @ggerganov
    Member

    Ah yes - I didn't even realise that the job already produces the executable. Not sure how to reference the latest one though - I would like to put a link the README.

    I think it would be still useful to be able to cross compile using mingw, so if you get the chance to try it - let us know.

  5. Topping1 commented on Nov 22, 2022

    @Topping1
    ContributorAuthor

    Ah yes - I didn't even realise that the job already produces the executable. Not sure how to reference the latest one though - I would like to put a link the README.

    I think it would be still useful to be able to cross compile using mingw, so if you get the chance to try it - let us know.

    Apparently, Github doesn´t have yet a way to reference the latest artifact from a build action (see https://stackoverflow.com/questions/60789862/url-of-the-last-artifact-of-a-github-action-build). In that stackoverflow question, a free service is mentioned that can generate a permanent link, for example https://nightly.link/ggerganov/whisper.cpp/actions/runs/3509700272/whisper-bin-x64.zip. Not sure if this link will work with new action runs, there is some documentation on the page that explains it in more detail.

  6. Topping1 commented on Nov 23, 2022

    @Topping1
    ContributorAuthor

    @ggerganov compiling with the POSIX version worked! In summary:

    sudo apt install g++-mingw-w64-x86-64-posix

    sudo apt install gcc-mingw-w64-x86-64-posix

    In ggml.c line 18
    change #include <Windows.h>
    to
    #include <windows.h>
    #include <errno.h>

    x86_64-w64-mingw32-gcc-posix -I. -O3 -std=c11 -mfma -mf16c -mavx -mavx2 -c ggml.c -o ggml.o

    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -lwinpthread -c whisper.cpp -o whisper.o

    x86_64-w64-mingw32-g++-posix -I. -I./examples -O3 -std=c++11 -static-libgcc -static-libstdc++ -lwinpthread examples/main/main.cpp ggml.o whisper.o -o main

    This version still does not show correctly accented characters. I have tested the automatic win64 build and also shows this problem.

  7. Topping1 commented on Nov 23, 2022

    @Topping1
    ContributorAuthor

    @ggerganov I found a workaround (the problem might be with MinGW): before using main.exe on a command prompt window, execute the command chcp 65001 (UTF-8 encoding).

  8. ggerganov commented on Nov 23, 2022

    @ggerganov
    Member

    @Topping1 Thank you for the efforts. I just pushed the windows.h fix on master and referenced this issue from the README for building with MinGW. If you want, you can update your original comment at the top of the thread with the latest instruction, so it is more visible when someone looks for the build steps.

  9. R4ZZ3 commented on Nov 25, 2022

    @R4ZZ3

    Hi,

    Any more detailed instructions coming? I have been able to get this working on WSL2.
    Now I tried with compiling with the instructions above using Ubunt22.04 in WSL2.
    First I got error that libwinpthread-1.dll is missing. Downloaded it from here https://www.dll-files.com/download/1e0d62c34ff2e649ebc5c372065732ee/libwinpthread-1.dll.html?c=OVhZYVRGRklHdFNGQWQ2NTZ3ZGVPdz09 and put it in the project root.

    Then I tried main.exe again but still running into error "application did not start correctly"

  10. Topping1 commented on Nov 27, 2022

    @Topping1
    ContributorAuthor

    Hi,

    Any more detailed instructions coming? I have been able to get this working on WSL2. Now I tried with compiling with the instructions above using Ubunt22.04 in WSL2. First I got error that libwinpthread-1.dll is missing. Downloaded it from here https://www.dll-files.com/download/1e0d62c34ff2e649ebc5c372065732ee/libwinpthread-1.dll.html?c=OVhZYVRGRklHdFNGQWQ2NTZ3ZGVPdz09 and put it in the project root.

    Then I tried main.exe again but still running into error "application did not start correctly"

    @R4ZZ3 I have tried your setup (WSL2 but with ubuntu 20.04) with the instructions of the first message of this thread and could compile the file with no errors about libwinpthread-1.dll missing. Not sure what else can be happening. Maybe try to compile in a fresh install of a linux VM?

  11. R4ZZ3 commented on Nov 27, 2022

    @R4ZZ3

    @Topping1
    sudo apt install g++-mingw-w64-x86-64-posix
    sudo apt install gcc-mingw-w64-x86-64-posix

    For me these run into error with Ubuntu 20.04
    image

  12. R4ZZ3 commented on Nov 27, 2022

    @R4ZZ3

    But I did not yet test with fresh install. Trying that now

  13. 11 remaining items

  14. fuzekun commented on Apr 30, 2024

    @fuzekun

    When I run main.exe,it gives me error which the system loss file named libgcc_s_seh-1.dll. And After I have put the file into exact path,there was a new problems which the application could't run (0x000007b).

    For those unfamiliar, it's essential not only to compile the project inside msys2 shell (ucrt64) ,but also to use the main executable there, probably also assuming the path delimiter correction. It looks like you tried to run it outside msys2. So if you have a file c:\myfiles\mywav.wav then the command line inside ucrt64 should be

    ./main -m models/ggml-base.en.bin -f c:/myfiles/mywav.wav

    Thank you, it worked. It was because I did not run it inside msys2. Since I can't run directly on Windows, I'll use Linux instead. Thank you again.

  15. thewh1teagle commented on Apr 30, 2024

    @thewh1teagle
    Contributor

    The disadvantages of using msys2, is that the binary won't run outside of that environment because it depended on msys2 DLLs.
    The simplest way to compile whisper.cpp on Windows, is using msvc compiler (of Microsoft)

    If you still need it, let me know and I'll provide instructions

  16. fuzekun commented on Apr 30, 2024

    @fuzekun

    No need to trouble yourself anymore, I have successfully compiled on Linux. Thank you very much for your kind assistance.

    The disadvantages of using msys2, is that the binary won't run outside of that environment because it depended on msys2 DLLs. The simplest way to compile whisper.cpp on Windows, is using msvc compiler (of Microsoft)

    If you still need it, let me know and I'll provide instructions

  17. Codereamp commented on Apr 30, 2024

    @Codereamp

    If you still need it, let me know and I'll provide instructions

    @thewh1teagle, It would be great in general, preferably with command-line options and information about versions compatibility. Sometimes one can have some (ancient) msvc installed for some purpose in the past and it would be nice if some bat file with minor correction would also work with it.

  18. Codereamp commented on May 7, 2024

    @Codereamp

    The disadvantages of using msys2, is that the binary won't run outside of that environment because it depended on msys2 DLLs.

    @thewh1teagle, I managed to make a static linking without much change to your routine, probably some minor questions along the way.

    Basically there's an half-baked option WHISPER_STATIC in CMakeLists.txt, that can be used if @ggerganov finds time to do this consistently across the compilers. It's half-baked imho because it is not declared as a cmake option and used in mostly GPU logic branches of CMakeLists.txt.

    Anyway, I added the following lines

    if (WHISPER_STATIC)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
    endif()
    
    

    and the following steps (assuming you already compiled the project once. If not just skip cleaning: steps)

    • cleaning: Delete build/CMakeCache.txt
    • cmake -B build -DWHISPER_STATIC=ON. This step is similar to the instruction, only the option provided
    • cleaning: cmake --build build --target clean . I had to do this otherwise ninja said there's nothing to do. I don't know whether this expected or not but in another cmake-based project deletion of CMakeCache.txt and configure was enough to trigger the project rebuild.
    • cmake --build build -j16 --config Release (this one's is the same from the instruction)

    The executable now starts in general Windows, without msys2 environment. It accepts windows-style paths (with backslash ""), but needs OEM encoding for non-latin parts of the paths.

    The only big (for me) question left for me is that while inferring non-lating, the outputted text in the console is garbage no matter what the encodings I chose for the msys2 shell, and I see the same garbage in the windows console. The file output is always ok (UTF-8)

  19. Codereamp commented on May 10, 2024

    @Codereamp
    > 
    > # WIP: With `Cuda`
    > 
    >     1. Setup `cuda`
    >  ....
    >     2. Build
    > 
    > 
    > NAME=cuda_12.3.2_546.12_windows
    > OPENBLAS_PATH=$MINGW_PREFIX cmake -B build -DWHISPER_CUBLAS=ON
    > cmake --build build -j16 --config Release
    
    

    @thewh1teagle, CUDA Build part probably no longer works for latest cmake (3.28.1). I already had a question to this part since the NAME variable is not mentioned anywhere in the following lines and OPENBLAS_PATH looks a little unrelated. What I managed to fix partly to make find_package(CUDAToolkit) happy is the second line replacement

    CUDAToolkit_ROOT=$MINGW_PREFIX/opt/$NAME/cuda_nvcc/nvcc/ cmake -B build -DWHISPER_CUBLAS=ON -DCUDAToolkit_INCLUDE_DIRECTORIES=$MINGW_PREFIX/opt/$NAME/cuda_cudart/cudart/include -DCUDA_CUDART=$MINGW_PREFIX/opt/$NAME/cuda_cudart/cudart
    

    now "cuBLAS found", but the script in the following lines can't detect CUDA architecture so I suspect I'm digging somewhere wrong (not to mentioning that my line is too long to look adequate).

    Any thoughts?

  20. thewh1teagle commented on May 12, 2024

    @thewh1teagle
    Contributor
    cmake -B build .
    cmake --build build -j16 --config Release
    

    I don't think it's possible to use CUDA when compiling inside msys2.

  21. thewh1teagle commented on May 12, 2024

    @thewh1teagle
    Contributor

    now "cuBLAS found", but the script in the following lines can't detect CUDA architecture so I suspect I'm digging somewhere wrong (not to mentioning that my line is too long to look adequate).

    Any thoughts?

    I don't think it's possible to use CUDA when compiling inside msys2.

  22. Codereamp commented on May 13, 2024

    @Codereamp

    I don't think it's possible to use CUDA when compiling inside msys2.

    I also met some short claims of impossibility, but now finally a notorious discussion about MinGW compatibility spanning 15 years, where the main fact is that nvcc.exe binary was and is hard-coded to cl.exe compiler (VC)

  23. BBC-Esq commented on May 30, 2024

    @BBC-Esq

    The disadvantages of using msys2, is that the binary won't run outside of that environment because it depended on msys2 DLLs. The simplest way to compile whisper.cpp on Windows, is using msvc compiler (of Microsoft)

    If you still need it, let me know and I'll provide instructions

    I would appreciate detailed instructions on how to compile on Windows with msvc please...

  24. jralduaveuthey commented on Jun 30, 2024

    @jralduaveuthey

    I am new to the party and probably this is a dumb question, but is there a .exe that I can just download and run in my windows machine?
    preferably without going through all the messages posted here in the last 2 years :)

  25. Kishlay-notabot commented on Aug 16, 2024

    @Kishlay-notabot

    @Topping1

    @Topping1 did you find a solution?

  26. BBC-Esq commented on Aug 16, 2024

    @BBC-Esq

    I did not

  27. Kishlay-notabot commented on Aug 25, 2024

    @Kishlay-notabot

    can someone help me to compile stream.cpp for windows? Thanks.

  28. Ghostown commented on Dec 7, 2024

    @Ghostown

    I can successfully build stream.cpp of both CPU and CUDA version on Windows11 with MSVC (using VScode only)

    whisper_init_from_file_with_params_no_state: loading model from 'models/ggml-base.en.bin'
    whisper_init_with_params_no_state: use gpu    = 1
    whisper_init_with_params_no_state: flash attn = 0
    whisper_init_with_params_no_state: gpu_device = 0
    whisper_init_with_params_no_state: dtw        = 0
    ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
    ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
    ggml_cuda_init: found 1 CUDA devices:
      Device 0: NVIDIA RTX A4000, compute capability 8.6, VMM: yes
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildBuild related issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions