Using translateC

I tried this and it kind of worked:

    const glad = init: {
        const translate_c = b.addTranslateC(
            .{
                .root_source_file = b.path("libs/glad/src/glad.c"),
                .link_libc = true,

                .optimize = optimize,
                .target = target,
            },
        );
        translate_c.addAfterIncludePath(b.path("libs/glad/include/"));

        break :init translate_c.createModule();
    };

This get’s added to the exe module

    exe_module.addImport("glad", glad);

And then I imported it in to main

    const glad = @import("glad");

The lsp does see the definitions if I try to go to the definition but when building I get a long line of:

error: undefined symbol: glad_glMultiDrawElementsIndirectCount
    note: referenced by main.o:.debug_info
error: undefined symbol: glad_glPolygonOffsetClamp
    note: referenced by main.o:.debug_info

What am I doing wrong or can translateC be used like this currently or ever, before I used @cImport() and that does work.

Probably a bug in translateC, but might just be missing some #define or flag that glad wants.

1 Like

Those are linker errors that occur when a dependency library is missing. So, presumably there is some additional library that contains the definitions of those functions that you need to also add to the executable.

As a troubleshooting step, it would help to know which source file contains the function definition glad_glMultiDrawElementsIndirectCount.

1 Like

I’m sorry, this seems a little out of depth for me but this is what I found:
Grepping I get this

glad/src/glad.c:PFNGLMULTIDRAWELEMENTSINDIRECTCOUNTPROC glad_glMultiDrawElementsIndirectCount = NULL;
glad/src/glad.c:        glad_glMultiDrawElementsIndirectCount = (PFNGLMULTIDRAWELEMENTSINDIRECTCOUNTPROC)load("glMultiDrawElementsIndirectCount");

Seem so be a global.

PFNGLMULTIDRAWELEMENTSINDIRECTCOUNTPROC glad_glMultiDrawElementsIndirectCount = NULL;

That gets set by:

static void load_GL_VERSION_4_6(GLADloadproc load) {
	if(!GLAD_GL_VERSION_4_6) return;
	glad_glSpecializeShader = (PFNGLSPECIALIZESHADERPROC)load("glSpecializeShader");
	glad_glMultiDrawArraysIndirectCount = (PFNGLMULTIDRAWARRAYSINDIRECTCOUNTPROC)load("glMultiDrawArraysIndirectCount");
	glad_glMultiDrawElementsIndirectCount = (PFNGLMULTIDRAWELEMENTSINDIRECTCOUNTPROC)load("glMultiDrawElementsIndirectCount");
	glad_glPolygonOffsetClamp = (PFNGLPOLYGONOFFSETCLAMPPROC)load("glPolygonOffsetClamp");
}

In the header it’s just:

GLAPI PFNGLMULTIDRAWELEMENTSINDIRECTCOUNTPROC glad_glMultiDrawElementsIndirectCount;
#define glMultiDrawElementsIndirectCount glad_glMultiDrawElementsIndirectCount

Side note, are you sure translate-c is the tool you want to be using here? Are you sure you don’t want to compile the C code directly into an object file?

Maybe? (Skill issue) I was mostly trying to get away from:
glad.zig:

pub const glad = @cImport({
   @cInclude("glad/glad.h");
});

build.zig:

    exe.addIncludePath(b.path("libs/glad/include/"));
    exe.addCSourceFile(.{
        .file = b.path("libs/glad/src/glad.c"),
    });

addTranslateC is only meant as a replacement for this part:

(i.e. it’s still only aimed at translating header files)

5 Likes

Thank you!
This might be incorrect but I just added:

exe_module.addIncludePath(b.path("libs/glad/include/"));
exe_module.addCSourceFile(.{
    .file = b.path("libs/glad/src/glad.c"),
});

Which brought down the errors from 1438 to just 2.

   └─ compile exe Project Debug native 2 errors
error: duplicate symbol definition: gladLoadGL
    note: defined by main.o
    note: defined by .zig-cache/o/e9a541dde28b1a4f1bc2964e9d918d06/glad.o
error: duplicate symbol definition: gladLoadGLLoader
    note: defined by main.o
    note: defined by .zig-cache/o/e9a541dde28b1a4f1bc2964e9d918d06/glad.o

I’m still doing something wrong but I should be able figure that out I hope.

I had mistakenly typed:

.root_source_file = b.path("libs/glad/src/glad.c"),

And not:

.root_source_file = b.path("libs/glad/include/glad/glad.h"),

Whole build.zig now looks like:

    const glad = init: {
        const translate_c = b.addTranslateC(
            .{
                .root_source_file = b.path("libs/glad/include/glad/glad.h"),
                .link_libc = true,

                .optimize = optimize,
                .target = target,
            },
        );
        translate_c.addAfterIncludePath(b.path("libs/glad/include/"));
        break :init translate_c.createModule();
    };
    
    // etc...
    exe_module.addAfterIncludePath(b.path("libs/glad/include/"));
    exe_module.addCSourceFile(.{ .file = b.path("libs/glad/src/glad.c") });

Thanks everybody for the help!

3 Likes