Empowering you to understand your world

“undefined reference to glfwInit”: How To Link GLFW In CMakeLists (via CLion)

If you get any of the following errors even after including the GLFW header files in your C++ project, it may be because you need to link the library.

undefined reference to `glfwInit'
undefined reference to `glfwCreateWindow'
undefined reference to `glfwSetKeyCallback'
undefined reference to `glfwWindowShouldClose'
undefined reference to `glfwDestroyWindow'

How it is linked of course depends on the compiler you are using. However, if you are using an IDE such as CLion which uses CMake build tools, you can try adding the following (last two) lines to your CMakeLists.txt file. ‘test’ is just the name of your app.

cmake_minimum_required(VERSION 3.15)
project(test)

set(CMAKE_CXX_STANDARD 14)

add_executable(test main.cpp)

find_package(glfw3 3.3 REQUIRED)
target_link_libraries(test glfw)
Subscribe to our newsletter
Get notified when new content is published