Libssh2 issue

I’m getting this issue when trying to use a function from libssh2. Not too sure why?

    const ssh_channel = ssh.libssh2_channel_open_session(ssh_session);

compiler error:
/home/wizard/projects/zig/sshz/.zig-cache/o/26c0322c148abb5a740835e0b76b8de5/cimport.zig:2027:42: error: unable to translate C
expr: unexpected token ‘a string literal’
pub const libssh2_channel_open_session = @compileError(“unable to translate C expr: unexpected token ‘a string literal’”);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

translate-c definition:

pub const libssh2_channel_open_session = @compileError("unable to translate C expr: unexpected token 'a string literal'");

I’m linking against my system library I’m too sure if that is the issue. Any help would be much appreciated!

There’s something in the file that the Zig C translation code can’t handle. What does libssh2_channel_open_session look like in the .h file?

To work around whatever the Zig limitation might be, you may have to manually write wrappers that you can use from Zig.

Hey thanks for a quick response. Seems like it is a macro. I never wrote a wrapper before. Does anyone have good examples for Zig?

#define libssh2_channel_open_session(session) \
    libssh2_channel_open_ex((session), "session", sizeof("session") - 1, \
                            LIBSSH2_CHANNEL_WINDOW_DEFAULT, \
                            LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0)

Every time a C programmer writes a preprocessor macro, a kitten dies.

Try writing your own translation of that particular macro, like:

fn libssh2_channel_open_session(session: *c.LIBSSH2_SESSION) ?*c.LIBSSH2_SESSION {
    const channel_type = "session";
    return c.libssh2_channel_open_ex(
        session,
        channel_type.ptr,
        channel_type.len - 1,
        c.LIBSSH2_CHANNEL_WINDOW_DEFAULT,
        c.LIBSSH2_CHANNEL_PACKET_DEFAULT,
        null,
        0,
    );
}

You could always create a new C file with an exported function that wraps the macro, and use the function from your Zig code, leaving the macro to be compiled by the C compiler.