How can I compile GLFW for Windows on Arm?

I am developing on one of the new snapdragon x elite laptops that ships with windows on arm and would like to compile a static library for native arm64.

I can’t seem to find instructions on how to do so, both the cmake configurations and build guides provide details with the assumption that windows is x86. Do I need to avoid cmake and manually compile or did I just fumble the cmake configuration?

My toolchain of choice is llvm-mingw with UCRT (GitHub - mstorsjo/llvm-mingw: An LLVM/Clang/LLD based mingw-w64 toolchain) and would be very happy if I can avoid visual studio.

I don’t think there is anything special you need to do to target arm64 Windows than for x86 Windows.

You just get the llvm-build that runs on your host (x86_64 if x86, aarch64 if on arm64), and then run cmake as usual.

Here’s me running it on x86 windows, but targeting arm64 and using Ninja for build:

C:\test>set PATH=c:\test\llvm-mingw-20240619-ucrt-x86_64\bin;%PATH%                                                                                   

C:\test>cmake -S glfw -B glfw.build -G Ninja -DCMAKE_C_COMPILER=aarch64-w64-mingw32-gcc.exe -DCMAKE_AR=aarch64-w64-mingw32-ar.exe
-- The C compiler identification is Clang 18.1.8
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/test/llvm-mingw-20240619-ucrt-x86_64/bin/aarch64-w64-mingw32-gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Including Win32 support
-- Looking for dinput.h
-- Looking for dinput.h - found
-- Looking for xinput.h
-- Looking for xinput.h - found
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Documentation generation requires Doxygen 1.9.8 or later
-- Configuring done (2.2s)
-- Generating done (0.1s)
-- Build files have been written to: C:/test/glfw.build

C:\test>ninja -C glfw.build triangle-opengl
ninja: Entering directory `glfw.build'
[25/25] Linking C executable examples\triangle-opengl.exe

C:\test>llvm-objdump --file-headers glfw.build\examples\triangle-opengl.exe | findstr architecture
architecture: aarch64

It has created examples\triangle-opengl.exe that is native arm64 binary. And it has built src\libglfw.a static library too.

1 Like

Thank you! I will try this right now

Worked like a charm! I think I couldn’t get it working earlier because I didn’t specify the archiver maybe? I had attempted this in visual studio and in msys2 clangarm64 so many times, I wonder what I was doing wrong when using those build tools.

With Visual Studio it is even simpler - you just need extra -A argument. Something like this should work:

cmake -G "Visual Studio 17 2022" -A arm64 -S glfw -B glfw.build
cmake --build glfw.build --target triangle-opengl
1 Like