1

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?

ibinshoid
  • 31
  • 2
  • Welcome to stackoverflow. You are asking a question for at least two problems at the same time here. Please ask only one question for every problem in the future. It helps to keep the site better organized. – Jens Mühlenhoff Jan 09 '19 at 15:08

1 Answers1

1

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.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113