link GLFW 在 Ubuntu 上通过 CLion 在 c++ 上尝试从源代码到项目时出错

Error when trying to link GLFW from source to project on c++ via CLion on Ubuntu

我正在尝试将 GLFW 从源代码添加到我的项目中。我按照文档中的说明进行操作,但出现错误。

错误:

/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

源代码如下,来自 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 在下面,这是来自 documentation 也:

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)

下图显示了项目的文件层次结构:

如图所示gl.h中似乎定义了glClear

当我转到 gl.h 时,它不在我下载并保存的源文件中 Dependencies/ 但在 /usr/include/GL/gl.h 中它可能是错误的来源,因为当我打开头文件我看到一条警告说该文件不属于任何项目,如您所见:

问题是:此配置有什么问题以及为什么我不能 运行 文档中的代码片段?

另外一个问题是:如何将 GLFW 从源代码添加到我的项目中?

IDE: CLion

OS: Linux / Ubuntu

编辑

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)

当我像上面的代码一样编写 cmake 时,但感觉有些不对劲。

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