You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下如何避免为X11编译OpenGL项目?

问题描述

在Windows系统中尝试用C++和OpenGL创建空白窗口,使用CMake命令cmake . -G "Unix Makefiles"编译项目时,出现X11依赖缺失的错误。

项目文件

main.cpp

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

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 450

int main ()
{
    GLFWwindow* window;

    if (!glfwInit())
    {
        std::cerr << "GLFW NOT INITIALIZED" << std::endl;
        return -1;
    }

    window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "OpenGL Window", 0, 0);

    if (!window)
    {
        std::cerr << "WINDOW WASN'T CREATED" << std::endl;
        return -1;
    }

    glfwMakeContextCurrent(window);

    while (1)
    {
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(window.exe)

add_subdirectory(glfw/)

add_executable(${PROJECT_NAME} src/main.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC glfw)
target_link_libraries(${PROJECT_NAME} PUBLIC glfw)

错误输出

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
CMake Error at /usr/share/cmake-3.23.2/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find X11 (missing: X11_X11_INCLUDE_PATH X11_X11_LIB)
Call Stack (most recent call first):
  /usr/share/cmake-3.23.2/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.23.2/Modules/FindX11.cmake:457 (find_package_handle_standard_args)
  glfw/src/CMakeLists.txt:187 (find_package)

已知X11是类Unix系统的依赖,询问能否在Windows上编译该项目,或如何在Windows运行X11应用。


解决方法

在MSYS2的mingw64 shell中执行以下操作:

  • 安装mingw-w64-x86_64-cmake包(注意:该包与普通cmake包并非同一包)
  • 执行CMake命令时指定MinGW专属生成器:
    cmake . -G "MinGW Makefiles"
    
  • 使用mingw-w64-x86_64-make命令完成编译,不要使用普通的make命令

内容的提问来源于stack exchange,提问作者NaniNoni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:20:48