Segmentation fault when compiling GLFW with optimization flags


I tried to enable o3 optimization for my cpp program which uses GLFW
(I use the extra/glfw-x11 package on endeavour-os)
When setting “set(CMAKE_CXX_FLAGS “-O3”)” in my CmakeLists.txt I get the error:
program terminated by signal SIGSEGV (Address boundary error).
Otherwise the program runs fine if I don’t add this option.

This seems to happen in the glfwInit() function, at beginning of my programm,
so basically the first GLFW function I call without any settings.

Running this in the debugger gives me:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79b2112 in __printf_buffer_init_end (mode=__printf_buffer_mode_asprintf, end=0x7fffffffd818 “”, base=0x7fffffffd750 “”, buf=0x7fffffffd728)
at …/include/printf_buffer.h:124
124 buf->write_base = base;

Any idea what this error could relate to?

Thank you

Hi @jneoj,

Welcome to the GLFW forum.

I would first try running the GLFW examples or test code with the same optimization level set to narrow down the issue. If it’s still happening in these then the full call stack would be helpful.

If you don’t have the example or test source code this can be got from

Hi, I am able to run the code from GitHub - juliettef/GLFW-CMake-starter: Use CMake to create a project with GLFW - Multi-platform Windows, Linux and MacOS.

However, for my setup as soon as I add O3 it crashes.
My project is also using CUDA, could this be related to this ?

Also I’m now building GLFW locally in my repo as a submodule (and removed the system libraries) but that doesn’t seem to change anything :frowning:


Were you able to run the GLFW-CMake-starter with the additional “-O3” flags?

You can set these by altering the CMakeLists.txt file or the command line -DCMAKE_CXX_FLAGS "-O3" (neither GLFW nor the GLFW-CMake-starter set the CXX_FLAGS).

If this works then it likely is something in your project, but I do not have enough information to know what. I would either debug the problem to find the root cause or cut the project down to the minimum working solution and build it back up to see what causes the problem.