c++linuxubuntucompiler-errorscross-compiling

x86_64-w64-mingw32-g++ doesn't link GLFW on Ubuntu 22.04


I'm doing some stuff with OpenGL and I wanted to compile an exe for Windows with x86_64-w64-mingw32-g++ but it doesn't link GLFW. When trying to compile it says:

main.cpp:4:10: fatal error: GLFW/glfw3.h: No such file or directory
    4 | #include <GLFW/glfw3.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

I used x86_64-w64-mingw32-g++ -m64 ~myproject/glad/*.c main.cpp -o test.exe -lglfw -lGL in terminal.

Compiling with g++ -m64 ~myproject/glad/*.c main.cpp -o test -lglfw -lGL has no problems.


Solution

  • You need to compile GLFW with MinGW, or find a precompiled version. MinGW can't reuse the Linux version of GLFW you already have installed.

    I made a tool called quasi-msys2 to automatically install libraries for MinGW, from MSYS2's vast repositories of prebuilt libraries.

    Install dependencies: (this assumes Ubuntu 22.04, adjust for your Linux distribution)

    sudo apt install make wget tar zstd gpg wine # Wine is optional but recommended.
    # Install Clang and LLD (recommended)
    bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
    

    Install quasi-msys2:

    git clone https://github.com/holyblackcat/quasi-msys2
    cd quasi-msys2
    make install _gcc _gdb _glfw
    # We install GCC only for the libraries it provides, we'll be compiling with Clang instead.
    

    Open its shell:

    env/shell.sh
    

    Compile:

    win-clang++ 1.cpp -o program.exe `pkg-config --libs --cflags glfw3`
    

    Quasi-msys2 doesn't need an external MinGW installation, only Clang (a regular native version) to cross-compile with. Though it might be possible to use an external MinGW GCC with libraries downloaded from it.