GLFW-CMake-starter 项目使用教程

GLFW-CMake-starter 项目使用教程

GLFW-CMake-starterUse CMake to create a project with GLFW - Multi-platform Windows, Linux and MacOS.项目地址:https://gitcode.com/gh_mirrors/gl/GLFW-CMake-starter

1. 项目的目录结构及介绍

GLFW-CMake-starter/
├── CMakeLists.txt
├── README.md
├── licence.txt
├── main.cpp
└── glfw/
    ├── include/
    ├── src/
    └── CMakeLists.txt
  • CMakeLists.txt: 顶层CMake配置文件,用于配置整个项目。
  • README.md: 项目说明文档。
  • licence.txt: 项目许可证文件。
  • main.cpp: 项目的主程序文件。
  • glfw/: 包含GLFW库的文件夹,包括头文件、源文件和GLFW的CMake配置文件。

2. 项目的启动文件介绍

main.cpp:

#include <GLFW/glfw3.h>
#include <iostream>

int main() {
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", nullptr, nullptr);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}
  • main.cpp 是项目的入口文件,负责初始化GLFW、创建窗口并进入主循环。

3. 项目的配置文件介绍

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(GLFW-CMake-starter)

set(GLFW_SOURCE_DIR ${CMAKE_SOURCE_DIR}/glfw)

add_subdirectory(${GLFW_SOURCE_DIR})

find_package(OpenGL REQUIRED)

add_executable(GLFW-CMake-starter main.cpp)

target_link_libraries(GLFW-CMake-starter ${OPENGL_LIBRARIES} glfw)

target_include_directories(GLFW-CMake-starter PRIVATE ${GLFW_SOURCE_DIR}/deps)

if(MSVC)
    if(${CMAKE_VERSION} VERSION_LESS "3.6.0")
        message(
            "\n\t[ WARNING ]\n\n\tCMake version lower than 3.6 \n\n\t - Please update CMake and rerun OR\n\t - Manually set 'GLFW-CMake-starter' as StartUp Project in Visual Studio \n"
        )
    else()
        set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT GLFW-CMake-starter)
    endif()
endif()
  • CMakeLists.txt 是项目的CMake配置文件,定义了项目的基本信息、依赖项和编译选项。
  • 它指定了GLFW库的路径,并链接了OpenGL库。
  • 还包含了针对Visual Studio的特定配置,确保项目能够正确启动。

GLFW-CMake-starterUse CMake to create a project with GLFW - Multi-platform Windows, Linux and MacOS.项目地址:https://gitcode.com/gh_mirrors/gl/GLFW-CMake-starter

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

管翔渊Lacey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值