# Segmentation fault when compiling GLFW with optimization flags

**URL:** <https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535>\
**Category:** support\
**Created:** [January 16, 2024, 7:04am UTC](https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535 "2024-01-16T07:04:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jneoj](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/jneoj/32/861_2.png) [@jneoj](https://discourse.glfw.org/u/jneoj)\
**Post date:** [January 16, 2024, 7:04am UTC](https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535/1 "2024-01-16T07:04:09Z")

</div>

Hi,

I tried to enable o3 optimization for my cpp program which uses GLFW 3.3.9.1  
(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

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [January 16, 2024, 9:31am UTC](https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535/2 "2024-01-16T09:31:51Z")

</div>

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 [https://www.glfw.org/](https://www.glfw.org/)

---

<div class="post-metadata">

**Author:** ![jneoj](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/jneoj/32/861_2.png) [@jneoj](https://discourse.glfw.org/u/jneoj)\
**Post date:** [January 16, 2024, 9:31pm UTC](https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535/3 "2024-01-16T21:31:44Z")

</div>

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.](https://github.com/juliettef/GLFW-CMake-starter)

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 ☹

TY

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [January 17, 2024, 10:05am UTC](https://discourse.glfw.org/t/segmentation-fault-when-compiling-glfw-with-optimization-flags/2535/4 "2024-01-17T10:05:51Z")

</div>

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.
