Zig build - can't compile cimgui example (but zig build-exe approach works)

I’m trying to get cimgui (which is a dear imgui C wrapper / bindings) example I have ported to Zig, to compile.

I was working gradually from cimgui’s provided C example, ported it to Zig and got it to build using a shell script that invokes zig c++ and zig. Script output and main.zig are in the comments.

But I can’t quite figure out why my build.zig fails. Here’s the output, build.zig proper is in the comment.

It does look like something is wrong with the way IMGUI_IMPL_API is defined in cpp_flags: "-DIMGUI_IMPL_API=\"extern \\\"C\\\" \"", but the way this key looks here after all the character escaping is done, -DIMGUI_IMPL_API="extern \"C\" ", does not ring any alarms. Frankly, I’m quite confused.

What am I missing?

cimgui only uses "-DIMGUI_IMPL_API=extern \"C\" " as the option (you have some extra quotes around the right side)
So maybe try it without the extra quotes:
"-DIMGUI_IMPL_API=extern \\\"C\\\" "

Nope. Getting similar, but slightly different IMGUI_IMPL_API-related errors.
Now with warning: missing terminating '"' character [-Winvalid-pp-token].

That is weird. Can you also share the build.zig for this one?

Posted the modified build.zig as a comment here.

Oh actually I think you don’t even need to escape the " twice:
"-DIMGUI_IMPL_API=extern \"C\" "

1 Like

It worked!

This is strange, as the command that zig build runs (see below) now includes this: -DIMGUI_IMPL_API=extern "C". Which makes me wonder how exactly zig understands that despite the space character between extern and "C", "C" is not a completely separate key.

$ zig build --verbose
/home/archie/apps/zig/zig build-exe /home/archie/projects/zig-playground/glfw-cimgui-00/src/main.zig -lglfw -lGL -lX11 -lXrandr -lXi -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/imgui.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/imgui_demo.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/imgui_draw.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/imgui_tables.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/imgui_widgets.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/backends/imgui_impl_glfw.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp -cflags -O2 -ffunction-sections -fdata-sections -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API=extern "C"  -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS -- /home/archie/projects/cimgui/cimgui.cpp -lc++ -lc --cache-dir /home/archie/projects/zig-playground/glfw-cimgui-00/zig-cache --global-cache-dir /home/archie/.cache/zig --name glfw-cimgui-00 -I /home/archie/projects/cimgui/imgui -I /home/archie/projects/cimgui/imgui/backends -I /home/archie/projects/cimgui/generator/output -I /home/archie/projects/cimgui --listen=-
1 Like