Skip to content

Add glfw3impl.h, a single header implementation of GLFW for C/C++ (rebased) - #2890

Draft
M374LX wants to merge 13 commits into
glfw:masterfrom
M374LX:unity
Draft

M374LX wants to merge 13 commits into
glfw:masterfrom
M374LX:unity

Conversation

@M374LX

@M374LX M374LX commented Aug 20, 2026

Copy link
Copy Markdown

This is the same as #2462, but rebased into the 3.5.1 release (WIP at the moment).

Some newly introduced functions might be yet to be renamed to the _glfw<function-name><platform>() pattern.

It has been tested only briefly on Linux for now. It builds successfully with both Wayland and X11 using CMake and also as a unity build. It also builds successfully as a Windows executable from a unity build and a Linux host.

At the moment, when building as C++ with Wayland support, warnings about the "visibility" attribute are shown. Additionally, there is an error related to the types used with the wl_array_for_each() macro. Apparently, this macro is not usable in C++ and the only alternative is to iterate the array manually.

Maybe we can set aside the plans to make the code a valid subset of C++ for now and open a separate issue about this.

These are the steps to test a unity build on Linux supporting both Wayland and X11:

  1. Write a simple C program (named test.c in this example) that includes glfw3impl.h:
#include "include/GLFW/glfw3impl.h"

int main()
{
	return 0;
}
  1. Generate Wayland headers:
wl_generate() {
	wayland-scanner client-header deps/wayland/$1.xml wayland-headers/$1-client-protocol.h
	wayland-scanner private-code deps/wayland/$1.xml wayland-headers/$1-client-protocol-code.h
}

rm -rf wayland-headers
mkdir wayland-headers

wl_generate wayland
wl_generate xdg-shell
wl_generate xdg-decoration-unstable-v1
wl_generate viewporter
wl_generate relative-pointer-unstable-v1
wl_generate pointer-constraints-unstable-v1
wl_generate fractional-scale-v1
wl_generate xdg-activation-v1
wl_generate idle-inhibit-unstable-v1
  1. Build it as C using GCC:
gcc -o test.elf -lm -D_GLFW_X11 -D_GLFW_WAYLAND -I./wayland-headers test.c
  1. To check if is also valid as a subset of C++, build it using G++:
gcc -o test.elf -lm -D_GLFW_X11 -D_GLFW_WAYLAND -I./wayland-headers test.c

If MinGW is installed, it can also be built as a Windows executable from a Linux host:

x86_64-w64-mingw32-gcc test.c -o test.exe -lopengl32 -lgdi32 -lwinmm
x86_64-w64-mingw32-g++ test.c -o test.exe -lopengl32 -lgdi32 -lwinmm

- assigned _glfw-qualified private names to all static symbols to avoid naming conflicts when including glfw3impl.h

- added casts as needed to compile glfw3impl.h as C++

- replaced sprintf with snprintf to avoid warnings from clang -Wall, (except on win32, where this provokes an appveyor failure `'snprintf' undefined`)

- Added a brief remark on `glfw3impl.h` to the `Compiling GLFW` section in `README.md`

- Added `Garett Bass` to `CONTRIBUTORS.md`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants