ibinshoid
ibinshoid

Reputation: 31

Problem on creating vala bindings (vapi) for libui

I'm trying to write a VAPI file to use libui (https://github.com/andlabs/libui) in Vala. This is my first try:

[CCode (cheader_filename = "ui.h")]
namespace LibUi {

[CCode (cname = "uiInitOptions", cprefix = "ui", has_type_id = false)]
public struct uiInitOptions {
    size_t Size;
}

[CCode (cname = "uiInit", has_type_id = false)]
public char uiInit(uiInitOptions options);
[CCode (cname = "uiMain", has_type_id = false)]
public void uiMain();
[CCode (cname = "uiQuit", has_type_id = false)]
public void uiQuit();
}

And this is a test code in vala:

using LibUi;

public static int main (string[] args) {
uiInitOptions o = uiInitOptions();
uiInit(o);
return 0;
}

Compiling with "valac --vapidir . --pkg libui main.vala"
brings this error:
main.vala.c:(.text+0x3c): undefined reference to `uiInit'

In the example C code of libui is this:

int main(void)
{
uiInitOptions o;
const char *err;
memset(&o, 0, sizeof (uiInitOptions));
err = uiInit(&o);
...
}

How do i make this memset stuff in vala or build it into the vapi file?

Upvotes: 1

Views: 154

Answers (1)

Jens Mühlenhoff
Jens Mühlenhoff

Reputation: 14873

For your first question on the undefined references:

libui does not provide a libui.pc file for pkg-config as far as I can see. You therefore have to tell the vala compiler to link the libui.so library like this:

valac --vapidir . --pkg libui -X -lui main.vala

Now for the second question regarding memset:

You don't have to zero the memory, valac does this automatically anyway. You can see that in the generated code that you can get with:

valac --vapidir . --pkg libui -C main.vala

There is a memset there:

gint _vala_main (gchar** args, int args_length1) {
    gint result = 0;
    uiInitOptions o = {0};
    uiInitOptions _tmp0_;
    memset (&o, 0, sizeof (uiInitOptions));
    _tmp0_ = o;
    uiInit (&_tmp0_);
    result = 0;
    return result;
}

PS: You might want to get rid of the ui* prefixes in your vapi file and use a namespace instead.

Upvotes: 1

Related Questions