# glfwInit returns 0, but when creating window, error callback returns GLFW\_NOT\_INITIALIZED

**URL:** <https://discourse.glfw.org/t/glfwinit-returns-0-but-when-creating-window-error-callback-returns-glfw-not-initialized/1821>\
**Category:** support\
**Created:** [April 9, 2021, 4:48am UTC](https://discourse.glfw.org/t/glfwinit-returns-0-but-when-creating-window-error-callback-returns-glfw-not-initialized/1821 "2021-04-09T04:48:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alegruz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/alegruz/32/494_2.png) [@Alegruz](https://discourse.glfw.org/u/Alegruz)\
**Post date:** [April 9, 2021, 4:48am UTC](https://discourse.glfw.org/t/glfwinit-returns-0-but-when-creating-window-error-callback-returns-glfw-not-initialized/1821/1 "2021-04-09T04:48:50Z")

</div>

# Summary

`glfwInit()` returns `0`, `GLFW_NO_ERROR`

but after hand,

when calling `glfwCreateWindow`, My error callback prints out “`GLFW_NOT_INITIALIZED`” error with error message “`The GLFW library is not initialized`”

- Engine
  - Graphics
    - Private
      - OpenGlTest.cpp

    - Public
      - OpenGlTest.h

- Samples
  - Main.cpp

```auto
// OpenGlTest.h
#pragma once
...
void GlfwErrorCallback(int error, const char* message);
void GlfwErrorInterpretor(int error);
int GlfwTestMain(int argc, char* argv[]);
GLuint LoadShaders(ShaderInfo* shaders);

```

```auto
// OpenGlTest.cpp
#include "OpenGlTest.h"

void GlfwErrorCallback(int error, const char* message)
{
	GlfwErrorInterpretor(error);
	LOGE(eLogChannel::GRAPHICS, std::cout, message);
}

void GlfwErrorInterpretor(int error)
{
	...
	LOGEF(eLogChannel::GRAPHICS, std::cout, "error code: %x, error message: %s", error, errorMessage);
	...
}

int GlfwTestMain(int argc, char* argv[])
{
	int errorCode = glfwInit();
	GlfwErrorInterpretor(errorCode);
	if (errorCode == GLFW_NOT_INITIALIZED)
	{
		return -1;
	}
	glfwSetErrorCallback(GlfwErrorCallback);
	GLFWwindow* window = glfwCreateWindow(640, 480, "MyTitle", nullptr, nullptr); // Error Occurs Here

	if (window == nullptr)
	{
		LOGE(eLogChannel::GRAPHICS, std::cout, "GLFWwindow creation failed");
		glfwTerminate();
		return -1;
	}
	...
	glfwDestroyWindow(window);
	glfwTerminate();

	return 0;
	}
...

```

```auto
// Main.cpp
...
int main(int argc, char* argv[])
{
	return GlfwTestMain(argc, argv);
}
...

```

```auto
# CMakeLists.txt in Graphics Folder
if (UNIX)
	set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
	set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
	set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

	find_package(glfw3 3.3 REQUIRED)
	
	set(OpenGL_GL_PREFERENCE "GLVND")
	find_package(OpenGL REQUIRED)
	target_link_libraries(Graphics PUBLIC OpenGL::GL)
	target_link_libraries(Graphics PUBLIC glfw)
	target_link_libraries(Graphics PUBLIC Gl3w)
	target_link_libraries(Graphics PUBLIC ${CMAKE_DL_LIBS})
endif (UNIX)

```

# Platform Information

OS: Ubuntu 20.04.2LTS  
VGA: Mesa Intel UHD Graphics 620 (WHL GT2)

---

<div class="post-metadata">

**Author:** ![mmozeiko](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mmozeiko/32/29_2.png) [@mmozeiko](https://discourse.glfw.org/u/mmozeiko)\
**Post date:** [April 9, 2021, 5:06am UTC](https://discourse.glfw.org/t/glfwinit-returns-0-but-when-creating-window-error-callback-returns-glfw-not-initialized/1821/2 "2021-04-09T05:06:08Z")

</div>

[glfwInit](https://www.glfw.org/docs/latest/group__init.html#ga317aac130a235ab08c6db0834907d85e) function returns boolean - GLFW\_TRUE or GLFW\_FALSE. Value 0 is GLFW\_FALSE. Which means function failed. To get why it failed, you call glfwGetError().

Basically:

```
int main()
{
    if (glfwInit() == GLFW_FALSE)
    {
        int error = glfwGetError();
        printf("GLFW error: %d\n", error);
        return -1;
    }

    // successfully called glfwInit, continue with rest of the code
    ....
}
```

---

<div class="post-metadata">

**Author:** ![Alegruz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/alegruz/32/494_2.png) [@Alegruz](https://discourse.glfw.org/u/Alegruz)\
**Post date:** [April 9, 2021, 7:47am UTC](https://discourse.glfw.org/t/glfwinit-returns-0-but-when-creating-window-error-callback-returns-glfw-not-initialized/1821/3 "2021-04-09T07:47:05Z")

</div>

Found the error!  
it was X11 error. the DISPLAY environment variable is missing

thank you!
