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

运行Vulkan教程基础代码遇未定义标识符错误求助

问题排查与修复方案

1. GLFW宏未定义问题

你代码里的#define GLFW_INCLUDE_VULKAN放在了#include <GLFW/glfw3.h>之后,这个宏必须在包含GLFW头文件之前定义,否则GLFW不会加载对应的Vulkan兼容定义,也无法正确暴露GLFW_CLIENT_API、GLFW_NO_API这些宏。

修正后的头文件顺序:

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>

#include <iostream>
#include <stdexcept>
#include <cstdlib>

2. window标识符未定义问题

你的HelloTriangleApplication类中没有声明window成员变量,需要在类的private区域添加GLFW窗口的成员声明:

private:
    GLFWwindow* window; // 添加这行

    void initWindow() {
        // ... 原有代码
    }
    // ... 其他成员函数

完整修正后的代码

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>

#include <iostream>
#include <stdexcept>
#include <cstdlib>

const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;


class HelloTriangleApplication {
public:
    void run() {
        initWindow();
        initVulkan();
        mainLoop();
        cleanup();
    }

private:
    GLFWwindow* window;

    void initWindow() {
        glfwInit();

        glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

        window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
    }
    void initVulkan() {

    }

    void mainLoop() {
        while (!glfwWindowShouldClose(window)) {
            glfwPollEvents();
        }
    }

    void cleanup() {
        glfwDestroyWindow(window);

        glfwTerminate();
    }
};

int main() {
    HelloTriangleApplication app;

    try {
        app.run();
    }
    catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:35:24