在 Linux 上编译和 运行 GLFW

Compile and run GLFW on Linux

我试图设置 GLFW,这样我就可以在 C++ 中制作 Windows,但 运行 成为一个问题。我的问题是每次编译都会出错。我正在尝试获得一个 window 在我终止它时打开和关闭它。我已经尝试了多种方法来解决这个问题,其中 none 确实有效。我真的不知道我是否错误地编译了 GLFW,我的 CMake 文件是否错误,或者我尝试这样做的方式很愚蠢。

代码如下:

#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;
}

我尝试 运行 代码时收到的错误消息:

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

CMake 文件(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})

build.sh文件我运行构建项目:

#!/bin/sh

cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug
ninja

最后,/usr/include/ 中的 GLFW 文件:

(哦,还有,我安装的唯一依赖项是 xorg-dev,如果这很重要的话。我不确定是否还有其他必需的依赖项。)

我发现如果我注释掉 `glClear',它会编译,但是当我尝试 运行 它时,什么也没有发生。该程序只是终止而没有向终端输出任何内容,也没有打开 window。如果有人知道如何解决这个问题,请告诉我。

您没有在代码中包含任何 OpenGL 加载程序。 glClear 是一个 OpenGL 函数。看看Glad.