RtMidi library import fails at malloc.c: No such file or directory

hi there, i’m trying to use the RtMidi library in this zig project: GitHub - patchsoul/patchsoul: mono repo for game tools, but i’m getting an interesting error when i step through the program at rtmidi_in_create_default, which should succeed without any problems because it’s just creating a new pointer to a default RtMidiIn (no errors should throw there):

gdb zig-out/bin/patchsoul
...
(gdb) b rtmidi_in_create_default
Breakpoint 1 at 0x11836c8: file /home/lowagner/.cache/zig/p/122098d759a37725c9913a08f54112671f9f00a458a5c8102e2f5cdd9f9dac98ca07/rtmidi_c.cpp, line 171.
(gdb) run
Starting program: /home/lowagner/code/patchsoul/zig-out/bin/patchsoul 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, rtmidi_in_create_default () at /home/lowagner/.cache/zig/p/122098d759a37725c9913a08f54112671f9f00a458a5c8102e2f5cdd9f9dac98ca07/rtmidi_c.cpp:171
171	    RtMidiWrapper* wrp = new RtMidiWrapper;
(gdb) s
operator new (size=32) at /home/lowagner/zig/0.13.0/files/lib/libcxxabi/src/stdlib_new_delete.cpp:67
67	  void* p = operator_new_impl(size);
(gdb) s
operator_new_impl (size=32) at /home/lowagner/zig/0.13.0/files/lib/libcxxabi/src/stdlib_new_delete.cpp:51
51	  if (size == 0)
(gdb) s
54	  while ((p = std::malloc(size)) == nullptr) {
(gdb) s
__GI___libc_malloc (bytes=32) at ./malloc/malloc.c:3287
3287	./malloc/malloc.c: No such file or directory.

so i imagine i have the build.zig setup wrong, and admittedly it is hacked together based on internet sources and duct tape. but i am linking libC and libCpp in my build.zig file:

    const rtmidi_dep_c = b.dependency("rtmidi", .{
        .target = target,
        .optimize = optimize,
    });
    const rtmidi_zig = b.addStaticLibrary(.{
        .name = "rtmidi-zig",
        .root_source_file = b.path("rtmidi/rtmidi.zig"),
        .target = target,
        .optimize = optimize,
    });
    rtmidi_zig.linkLibC();
    rtmidi_zig.linkLibCpp();
    rtmidi_zig.addCSourceFiles(.{
        .root = rtmidi_dep_c.path(""),
        .files = &.{ "rtmidi_c.cpp", "RtMidi.cpp" },
    });
    rtmidi_zig.installHeadersDirectory(rtmidi_dep_c.path(""), "", .{
        .include_extensions = &.{ "rtmidi_c.h", "RtMidi.h" },
    });
    b.installArtifact(rtmidi_zig);
    const rtmidi = b.addModule("rtmidi", .{
        .root_source_file = b.path("rtmidi/rtmidi.zig"),
    });
    rtmidi.addIncludePath(rtmidi_dep_c.path(""));
    rtmidi.linkLibrary(rtmidi_zig);
    rtmidi.addImport("lib", lib);

how do i make sure that malloc is available to this rtmidi module? i don’t care too much about the internal structure of this C library wrapper, only that it is available via @import("rtmidi"), so if it can be simplified that’s even better.

i’m on zig 0.13.0 if it matters. thanks in advance for any help!

There is no fail in your program, it’s the debugger that fails to find the ./malloc/malloc.c file to dump the line of code you stepping into (__GI___libc_malloc call) as no sources for glibc is available.

1 Like

ah you’re right. when i break at other points inside RtMidi i see that i’m not getting any compiled APIs (rtmidi_num_compiled_apis == 0).

will need to debug more on why that is, but i imagine there might be some compile options i need to try for rtmidi.

totally different problem than what i thought i had, but yeah adding in an rtmidi backend fixed the issue: patchsoul/build.zig at f41c3614e8ead01f78312b6591fdd6f6455bcf55 · patchsoul/patchsoul · GitHub

now onto new problems :). thanks!