Libcsnd6.dylib does not expose C API symbols

Hey there.
I’m trying to compile a very simple zig program that uses the C API.

const std = @import("std");
const cs = @cImport({
    @cInclude("csound/csound.h");
});

pub fn main() !void {
    const score =
        \\ <CsoundSynthesizer>
        \\ <CsOptions> -odac </CsOptions>
        \\ <CsInstruments>
        \\ instr 1
        \\  out(linen(oscili(p4,p5),0.1,p3,0.1))
        \\ endin
        \\ </CsInstruments>
        \\ <CsScore>
        \\ i1 0 5 1000 440
        \\ </CsScore>
        \\ </CsoundSynthesizer>
    ;

    const csound = cs.csoundCreate(null);
    defer {
        _ = cs.csoundCleanup(csound);
        cs.csoundReset(csound);
        cs.csoundDestroy(csound);
    }

    if (cs.csoundCompileCsdText(csound, score) == 0) {
        _ = cs.csoundStart(csound);

        while (cs.csoundPerformKsmps(csound) == 0) {}
    }
}

Roughly equivalent to the C example here: Csound API: Main Page

I’m linking in against libcsnd6 but it doesn’t find any symbol and when I’m listing libcsnd6.dylib content I don’t see them even though the csound.h header does define them

λ giann [~/git/synth] → zig run -I/opt/homebrew/Cellar/csound/6.18.1_9/include -L/opt/homebrew/Cellar/csound/6.18.1_9/lib -lcsnd6 src/main.zig
error: undefined symbol: _csoundCleanup
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundCompileCsdText
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundCreate
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundDestroy
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundPerformKsmps
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundReset
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
error: undefined symbol: _csoundStart
    note: referenced by /Users/giann/.cache/zig/o/2f4f63526e79e71f102d7a5ee85e46c6/main.o:_main.main
λ giann [~/git/synth] → nm -gU /opt/homebrew/Cellar/csound/6.18.1_9/lib/libcsnd6.6.0.dylib | grep _csoundCreate
# nothing...

Am I linking against the wrong library?
I have installed csound with brew install csound

Try this (uses csound 7, so no brew install for you, but you can probably adapt it to csound 6)


const std = @import("std");
const cs = @cImport({

    @cInclude("csound/csound.h");

});


pub fn main() !void {
    const orc = 
        \\ sr = 48000
        \\ ksmps = 64
        \\ 0dbfs = 1
        \\ nchnls = 2        
        \\ instr 1
        \\  outch 1, oscili:a(p4, p5)
        \\ endin
    ;

    const csound = cs.csoundCreate(null, null);
    _ = cs.csoundSetOption(csound, "-odac");
    _ = cs.csoundCompileOrc(csound, orc, 0);
    var pfields = [_]f64{ 1.0, 0.0, 10.0, 0.1, 1000.0 };
    _ = cs.csoundEvent(csound, 0, &pfields, 5, 1);
    _ = cs.csoundStart(csound);
	
    while (cs.csoundPerformKsmps(csound) == 0) {}

}

Run it with zig run -I /usr/local/include -L /usr/local/lib -lc -lcsound64 test.zig (adapted to your system)

I installed csound with the installer rather than brew and I’m almost there I think. It now complains about FFT functions of the macOS sdk but I don’t know which library I should link against to get them:

λ giann [~/git/synth] → zig run -I /Library/Frameworks/CsoundLib64.framework/Versions/Current/Headers -L /Library/Frameworks/CsoundLib64.framework -I /opt/homebrew/include -L /opt/homebrew/lib -I /usr/local/include -L /usr/local/lib -lCsoundLib64 -lc -lsndfile -lm -lcurl src/main.zig

error: undefined symbol: _vDSP_create_fftsetupD
    note: referenced by /Library/Frameworks/CsoundLib64.framework/libCsoundLib64.a(fftlib.c.o):_csoundRealFFT2Setup
error: undefined symbol: _vDSP_destroy_fftsetupD
    note: referenced by /Library/Frameworks/CsoundLib64.framework/libCsoundLib64.a(fftlib.c.o):_setupDispose
error: undefined symbol: _vDSP_fft_zripD
    note: referenced by /Library/Frameworks/CsoundLib64.framework/libCsoundLib64.a(fftlib.c.o):_csoundRealFFT2
    note: referenced by /Library/Frameworks/CsoundLib64.framework/libCsoundLib64.a(fftlib.c.o):_vDSP_DCT_execute
    note: referenced by /Library/Frameworks/CsoundLib64.framework/libCsoundLib64.a(fftlib.c.o):_vDSP_DCT_execute

Looks like you’re static linking to libCsoundLib64.a, so you’ll need to link to Accelerate for vDSP symbols

zig run \
-I /Library/Frameworks/CsoundLib64.framework/Versions/Current/Headers \
-L /Library/Frameworks/CsoundLib64.framework \
-I /opt/homebrew/include \
-L /opt/homebrew/lib \
-I /usr/local/include \
-L /usr/local/lib \
-lCsoundLib64 -lc -lsndfile -lm -lcurl -framework Accelerate \
src/main.zig

worked :+1: