如何在 OpenGL 4.1 和 GLFW 中使用 Nuklear

How to use Nuklear with OpenGL 4.1 and GLFW

提问人:Jordan 提问时间:11/11/2023 最后编辑:Jordan 更新时间:11/15/2023 访问量:77

问:

尝试将Nuklear集成到新的C++项目中,并得到了很多链接器错误。

我尝试根据 Nuklear 存储库设置一个简单的带有标头和 CPP 文件的简单 UI 类,如下所示:

#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_STANDARD_BOOL
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_UINT_DRAW_INDEX
#define NK_GLFW_GL4_IMPLEMENTATION
#define NK_IMPLEMENTATION
#include <nuklear.h>
#include <nuklear_glfw_gl3.h>

class UI
{
public:
    UI(GLFWwindow *window);
    ~UI();
    struct nk_context *ctx;
    void RenderWindow();
};
#include "UI.h"

UI::UI(GLFWwindow *window)
{
    ctx = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
}

UI::~UI()
{
    nk_glfw3_shutdown();
}

void UI::RenderWindow()
{
    nk_glfw3_new_frame();
    if (nk_begin(ctx, "In-Game Window", nk_rect(10, 10, 200, 200),
                 NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
                     NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE))
    {
        // Nuklear widgets go here

        // Example widget: a button
        if (nk_button_label(ctx, "Click Me!"))
        {
            // Handle button click
        }
    }
    nk_end(ctx);
}

我尝试从我的 CMakeLists.txt 中以各种方式包含 Nuklear;当我将整个 Nuklear 存储库克隆为子模块并添加以下内容以将其包含在项目中时,它似乎效果最好:

file(GLOB_RECURSE SOURCES "external/Nuklear/src/*.c")

但是此设置给了我以下错误:

<projpath>/external/Nuklear/src/nuklear_buffer.c:113:9: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        NK_MEMCPY(temp, b->memory.ptr, buffer_size);
        ^
<projpath>/external/Nuklear/src/nuklear_buffer.c:128:9: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        NK_MEMCPY(dst, src, back_size);
        ^
<projpath>/external/Nuklear/src/nuklear_buffer.c:191:5: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    NK_MEMCPY(mem, memory, size);
    ^
3 errors generated.

如果我不直接包含.c文件(这似乎没问题,因为Nuklear可以仅包含标头),则会出现如下错误,表明未包含GL函数:

<projpath>/external/Nuklear/demo/glfw_opengl4/nuklear_glfw_gl4.h:202:9: error: use of undeclared identifier 'glCreateVertexArrays'

我正在将 GLAD 用于 OpenGL 函数,但我正在尝试使用 OpenGL 4.1,所以我认为错误来自尝试使用较新的 OpenGL 函数。如何告诉 Nuklear 使用旧版本的 OpenGL?还是我缺少其他配置?

我有 GLFW 窗口等的正常样板代码。如何修复这些链接器错误?

C++ OpenGL Nuklear

评论

1赞 drescherjm 11/11/2023
看起来你错过了一些陈述。#include <>
0赞 Jordan 11/11/2023
知道我需要包括什么吗?我没有因缺少符号而收到任何编译错误,并且我已经包含了非官方的 GLFW 集成标头(从演示中捕获),所以我不明白缺少什么

答: 暂无答案