【问题标题】:Error when trying to link GLFW from source to project on c++ via CLion on Ubuntu [duplicate]尝试通过 Ubuntu 上的 CLion 将 GLFW 从源代码链接到 C++ 项目时出错 [重复]
【发布时间】:2020-05-20 14:26:45
【问题描述】:

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

错误:

/usr/bin/ld: CMakeFiles/mancala_graphics.dir/main.cpp.o: 在函数'main'中:

/path/to/clion/mancala_graphics/main.cpp:47:未定义对“glClear”的引用

collect2: 错误:ld 返回 1 个退出状态

make[ 3 ]: *** [CMakeFiles/mancala_graphics.dir/build.make:88: mancala_graphics] 错误 1

make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/mancala_graphics.dir/all] 错误2

make[2]: *** [CMakeFiles/Makefile2:122: CMakeFiles/mancala_graphics.dir/rule] 错误2

make: *** [Makefile:164: mancala_graphics] 错误 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 中它可能是错误的来源,因为当我打开头文件时,我看到一条警告说如您所见,该文件不属于任何项目:

问题是:这个配置有什么问题,为什么我不能运行文档中的代码 sn-p?

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

IDE:CLion

操作系统: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 时,但感觉有些不对劲。

【问题讨论】:

    标签: c++ opengl cmake clion glfw


    【解决方案1】:

    来自GLFW documentation

    请注意,glfw 目标不依赖于 OpenGL,因为 GLFW 会在运行时加载它需要的任何 OpenGL、OpenGL ES 或 Vulkan 库。如果您的应用程序直接调用 OpenGL,而不是使用 modern extension loader library,请使用 OpenGL CMake 包。

    find_package(OpenGL REQUIRED)
    target_link_libraries(myapp OpenGL::GL)
    

    如果找到 OpenGL,则将 OpenGL::GL 目标添加到您的项目中,其中包含库和包含目录路径。像上面那样链接。

    【讨论】:

    • 我更新了 cmake,添加了 find_package 然后 target_link_libraries 并编辑了问题以添加新的 cmake。感觉有点不对劲,能查一下吗?
    • “感觉不对”是什么意思?您可以使用一个 target_link_libraries 语句来链接 glfw 和 OpenGL::GL,但除此之外.. 正如我的回答所说:您可以通过使用现代加载器之一(GLAD, GLEW,GL3W)。
    • 但是现在你的 CMake 文件还不错。它会根据需要增长和变化。首先关注您的应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2014-08-04
    • 2014-08-12
    相关资源
    最近更新 更多