I was trying to get GLFW setup so I could make Windows in C++, but ran into an issue. My issue was that I was getting errors whenever I compiled. I am trying to get a window that opens and closes when I terminate it. I've tried multiple methods to fix this, none of which have actually worked properly. I can't really tell if I incorrectly compiled GLFW, if my CMake file is wrong, or the way I am trying to do it is dumb.
Here is the code:
#include <GLFW/glfw3.h>
int main(void)
{
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;
}
The error message I get when I try to run the code:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Documents/CPP-Stuff/OpenGL-Learning
[2/2] Linking CXX executable opengl-learning
FAILED: opengl-learning
: && /usr/bin/c++ -Wall -Werror -std=c++14 -g CMakeFiles/opengl-learning.dir/src/main.cpp.o -o opengl-learning -lglfw && :
/usr/bin/ld: CMakeFiles/opengl-learning.dir/src/main.cpp.o: in function `main':
/home/user/Documents/CPP-Stuff/OpenGL-Learning/src/main.cpp:26: undefined reference to `glClear'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The CMake file (CMakeLists.txt):
cmake_minimum_required (VERSION 3.5)
project (opengl-learning)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
set (GCC_COVERAGE_LINK_FLAGS "-lglfw3 -lGL -lX11 -lpthread -lXrandr -lXi -ldl")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
file (GLOB source_files "${source_dir}/*.cpp")
add_executable (opengl-learning ${source_files})
target_link_libraries(opengl-learning ${GLFW_LIBRARIES})
the build.sh file I am running to build the project:
#!/bin/sh
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug
ninja
and, finally, the files that are in /usr/include/ for GLFW:
(Oh, and also, the only dependency that I installed was xorg-dev
, if that matters. I'm not sure if there are other required dependencies.)
I have found that if I comment out `glClear', it compiles, but when I try to run it, nothing happens. The program just terminates without outputting anything to the terminal, or opening a window. If anyone knows how to solve this, please tell me.
You aren't including any OpenGL loader in your code. glClear
is an OpenGL function. Take a look at Glad.