glfwInit returns 0, but when creating window, error callback returns GLFW_NOT_INITIALIZED

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
// 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);
// 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;
	}
...
// Main.cpp
...
int main(int argc, char* argv[])
{
	return GlfwTestMain(argc, argv);
}
...
# 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)

glfwInit 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
    ....
}

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

thank you!