I'm trying to add GLFW from source to my project. I did as stated in the documents, but I get an error.
Error:
/usr/bin/ld: CMakeFiles/mancala_graphics.dir/main.cpp.o: in function `main':
/path/to/clion/mancala_graphics/main.cpp:47: undefined reference to 'glClear'
collect2: error: ld returned 1 exit status
make[ 3 ]: *** [CMakeFiles/mancala_graphics.dir/build.make:88: mancala_graphics] Error 1
make[ 1 ]: *** [CMakeFiles/Makefile2:115: CMakeFiles/mancala_graphics.dir/all] Error 2
make[ 2 ]: *** [CMakeFiles/Makefile2:122: CMakeFiles/mancala_graphics.dir/rule] Error 2
make: *** [Makefile:164: mancala_graphics] Error 2
Source Code is below and its from documentation:
#include <GLFW/glfw3.h>
int main() {
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
CMake is below and this is from documentation also:
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics glfw)
Below image shows file hierarchy of the project:
It seems like glClear defined in gl.h as you see in picture
When I go to gl.h it is not in source files which I downloaded and save Dependencies/
but in /usr/include/GL/gl.h
it may be the source of error, because when I open the header file I saw a warning says the file doesn't belong to any project as you see:
Question is: What is wrong with this configuration and why I can't run the code snippet from documentation?
Also question is: How can I add GLFW from source to my project?
IDE: CLion
OS: Linux / Ubuntu
EDIT
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
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(OpenGL REQUIRED)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics OpenGL::GL)
target_link_libraries(mancala_graphics glfw)
When I write cmake like above code working but it feels something wrong.
From the GLFW documentation:
Note that the glfw target does not depend on OpenGL, as GLFW loads any OpenGL, OpenGL ES or Vulkan libraries it needs at runtime. If your application calls OpenGL directly, instead of using a modern extension loader library, use the OpenGL CMake package.
find_package(OpenGL REQUIRED)
target_link_libraries(myapp OpenGL::GL)
If OpenGL is found, the OpenGL::GL target is added to your project, containing library and include directory paths. Link against this like above.