Re: [Vala] Calling into C libs



Magnus Therning wrote:
I'm assume this is what .vapi files allow you to do.  However, I only
really want to call a couple of functions, so are there any pointers on
how to manually write a .vapi file and get it hooked up properly for
compilation?

If you only need a couple of functions you can use the 'extern' keyword without
writing a separate vapi file:

-----------------------------------------------------------------------
[CCode (cname = "SampleMethod")]
static extern int sample_method (int x);

static void main () {
    stdout.printf ("sample_method () returns %d\n", sample_method (4));
}
-----------------------------------------------------------------------

Pass the library name to the compiler with -X -l... :

$ valac demo.vala -X -lfoo


If you want to write a local vapi file (e.g. foo.vapi) you can specify the
directory of your vapi file with '--vapidir' and use it with '--pkg'

$ valac demo.vala --vapidir . --pkg foo -X -lfoo

If the used library has a pkg-config file (*.pc) in /usr/lib/pkgconfig/ with
the same base name as the vapi file Vala will use its information and you can
omit the '-X -l...' part:

$ valac demo.vala --vapidir . --pkg foo
(will use 'foo.vapi' and 'foo.pc')


Best Regards,

Frederik



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]