尝试通过Ubuntu上的CLion将GLFW从源链接到c ++上的项目时出错

问题描述 投票:0回答:1

我正在尝试将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)

下图显示了项目的文件层次结构:file hierarchy of the project

您在图片glClear defined in gl.h中看到的好像在gl.h中定义了glClear

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

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

另外一个问题是:如何从源代码向项目添加GLFW?

IDE:CLion

操作系统:Linux / Ubuntu

c++ opengl cmake clion glfw
1个回答
0
投票

GLFW documentation

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.