首页 > 解决方案 > 将 GLFW 静态链接到 CMake 到可执行文件

问题描述

在我的项目中,我想将 GLFW 静态链接到我的主可执行文件,但是当我尝试这样做时,我的 CMakeLists 没有任何错误,而我的主可执行文件的错误只是说 GLFW/glfw3.h 文件不是一个有效的文件。

这是我的 CMakeLists.txt 代码:

cmake_minimum_required(VERSION 3.0.0)
project(openGL VERSION 1.0.0)


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(L:\glfw-3.3.2)

add_executable(main main.cpp)

target_link_libraries(main glfw)

find_package(OpenGL REQUIRED)

target_link_libraries(main OpenGL::GL)

我的主要可执行文件:

#include <iostream>

#include <GLFW/glfw3.h>

using namespace std;


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

确切的错误代码:

cannot open source file "GLFW/glfw3.h/"

标签: c++openglcmakeglfw

解决方案


推荐阅读