[ORIGINAL ANSWER]
Maybe I am coming too late, but I found a solution so the next guy falling on this topic will not waste time as much as I wasted mine.
First of all, you should generate the backends you need with the cimgui
generator:
$ cd deps/cimgui/generator # The cimgui generator works
# only in its directory.
$ ./generator.sh -c "glfw vulkan" # Please, pay attention to the
# doubles quotes.
Later you should delegate this job to your build.zig
.
Then add these lines to your @cImport
:
const c = @cImport({
// C Imgui
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", {});
// These lines:
@cDefine("CIMGUI_USE_GLFW", {});
@cDefine("CIMGUI_USE_VULKAN", {});
@cInclude("cimgui.h");
@cInclude("cimgui_impl.h");
// ! \\ WARNING: // ! \\
// Maybe you should move these following lines before cImgui
// @cIncludes and @cDefine, because Imgui depends on for
// its backends.
// GLFW and Vulkan:
@cDefine("GLFW_INCLUDE_NONE", {});
@cDefine("GLFW_INCLUDE_VULKAN", {});
@cInclude("GLFW/glfw3.h");
@cInclude("vulkan/vulkan.h");
});
Finally you should add these lines in your build.zig
:
const IMGUI_SOURCES = [_][]const u8 {
"deps/cimgui/cimgui.cpp",
"deps/cimgui/imgui/imgui_demo.cpp",
"deps/cimgui/imgui/imgui_draw.cpp",
"deps/cimgui/imgui/imgui_tables.cpp",
"deps/cimgui/imgui/imgui.cpp",
"deps/cimgui/imgui/imgui_widgets.cpp",
// These lines:
"deps/cimgui/imgui/backends/imgui_impl_glfw.cpp",
"deps/cimgui/imgui/backends/imgui_impl_vulkan.cpp",
};
[EDIT] - 23/07/2023
Unfortunately, with this changes, your program will go further but you will encounter a new error for each Imgui_Impl*
you are using that looks like this:
error: ld.lld: undefined symbol: ImGui_ImplVulkan_Init
note: referenced by your_file.zig:102 (/path/to/your/zig/file/your_file.zig:102)
note: /path/to/your/project/zig-cache/o/289b11a03ec527342c42c8f0bd4fd76e/your_project.o:(your.function.using.ImGui_ImplVulkan_Init)
note: did you mean to declare ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo*, VkRenderPass_T*) as extern "C"?
note: defined in: /path/to/your/project/zig-cache/o/34e4e97a5b4f3b2c1930a506bfa38f32/imgui_impl_vulkan.o
You have to make those new modifications to allow your program to compile:
- Reduce your
@cImport
as follow:
const c = @cImport({
// C Imgui
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", {})
@cInclude("cimgui.h");
@cDefine("GLFW_INCLUDE_NONE", {});
@cDefine("GLFW_INCLUDE_VULKAN", {});
@cInclude("GLFW/glfw3.h");
@cInclude("vulkan/vulkan.h");
});
- Keep the modifications I described in my original answer into your
build.zig
, but remove this line from your original file:
exe.addIncludePath("deps/cimgui/generator/output");
Into deps/cimgui/cimgui.cpp
:
- Add these includes at the end of the file:
#include "imgui/backends/imgui_impl_glfw.h"
#include "imgui/backends/imgui_impl_vulkan.h"
- After the includes you just added, for each
Imgui_Impl*
you are using and you can find into deps/cimgui/generator/output/cimgui_impl.h
(that you generate as I described in my original answer), add a new binding function. So for example for CIMGUI_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info,VkRenderPass render_pass)
, add this function at the end of the file (pay attention to the ig
I added before the name of the binding function. You can change it (or change the entire name) by whatever you want but do not use the same function name defined by the backends you want to use):
CIMGUI_API bool igImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info,VkRenderPass render_pass)
{
return ImGui_ImplVulkan_Init(info,render_pass);
}
Into deps/cimgui/cimgui.h
:
- copy the
deps/cimgui/generator/output/cimgui_impl.h
content before the very last #endif
,
- remove
#ifdef CIMGUI_USE_VULKAN
and #ifdef CIMGUI_USE_GLFW
and their matching #endif
that you recently pasted from deps/cimgui/generator/output/cimgui_impl.h
,
- remove the
struct
declarations from what you recently added. For example, keep this: typedef struct ImGui_ImplVulkanH_Frame ImGui_ImplVulkanH_FrameSemaphores;
or this: struct ImGui_ImplVulkanH_FrameSemaphores;
but remove this:
struct ImGui_ImplVulkanH_FrameSemaphores
{
VkSemaphore ImageAcquiredSemaphore;
VkSemaphore RenderCompleteSemaphore;
};
- add those extra macros before the
deps/cimgui/generator/output/cimgui_impl.h
you freshly pasted:
#define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>