Re: [Vala] Using a C library



Justin,

I believe that you have to use [CCode(cheader_filename="libspotify/api.h")] instead of cheader.

-Andrew

On 04/28/2015 06:29 PM, Justin Brown wrote:
Hi Al,

Thanks for the continued help. I've been making some progress on the vapi,
but I've been putting off the compilation issue for a while now. Your reply
had me go back to compiling the vapi properly instead of as a vala program.
I think I'm still doing something slightly wrong. Structure:

src/
├── fandingo.c
├── fandingo.vala
└── vapi/
     └── libspotify.vapi
ibspotify --vapidir vapi/ fa
The libspotify-devel RPM from rpmfusion comes with a spotify.pc. It's in a
strange location, but that's fixed by exporting PKG_CONFIG_PATH. It's also
named libspotify, so I renamed my vapi. Trying to compile:

valac -v --pkg libspotify --vapidir vapi/ fandingo.vala


The test program just tries to create an error enum, print the message, and
print the spotify api version. I get a few errors:

/home/justin/code/fandingo/src/fandingo.vala.c:48:2: error: unknown type
name ‘sp_error’

   sp_error err = 0;

   ^

/home/justin/code/fandingo/src/fandingo.vala.c:55:46: error:
‘SPOTIFY_API_VERSION’ undeclared (first use in this function)


Seems like libspotify/api.h is not being linked correctly, which is
strange.ibspotify --vapidir vapi/ fa

vapi/libspotify.vapi begins with:

[CCode (cheader = "libspotify/api.h")]

namespace Spotify {

That seems fine. However, if I generated C code with --ccode, the #include
is missing:

/* fandingo.c generated by valac 0.26.1, the Vala compiler
  * generated from fandingo.vala, do not modify */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

If I add #include <libspotify/api.h>, and compile the generated C with the
compilation command that valac provided earlier (cc -o
'/home/justin/code/fandingo/src/fandingo'
'/home/justin/code/fandingo/src/fandingo.c' -I/usr//include
-I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -L/usr//lib
-lgobject-2.0 -lglib-2.0 -lspotify).

Any idea why valac isn't adding "#include <libspotify/api.h>?"

After the manual compilation, I think everything is resolved with the enum
values. Errors 39-41 still produce "invalid error code" when calling
sp_error_message, but I think that might mean that libspotify doesn't have
error strings for those errors. The "jumps" (like 3 to 5 in my last email)
are working properly. Hooray.

Thanks,
Justin

On Tue, Apr 28, 2015 at 5:32 AM, Al Thomas <astavale yahoo co uk> wrote:

From: Justin Brown <justin brown fandingo org>
Cc: vala-list gnome org
Sent: Wednesday, 22 April 2015, 7:05
Subject: Re: [Vala] Using a C library
I did have a question about the C sp_error enum. There are 36 values
[0-35] in the C library, but there are >some gaps such that the last value
(SYSTEM_FAILURE) is 41, not 35. Every once in a while in the C enum a
manual value is specified that jumps the integer a few places -- for
example:
SP_ERROR_TRACK_NOT_PLAYABLE        = 3,
SP_ERROR_BAD_APPLICATION_KEY       = 5,

I've tried a variety of solutions. If I don't specify any integer values
in Vala, then the codes don't >respect the "jumps," meaning that
SP_ERROR_BAD_APPLICATION_KEY == 4. If I specify the "jumps" or all the
integers, then libspotify sp_error_message() complains about an invalid
type in a string (not a segfault or >compilation error). Why isn't Vala
using the C values properly when not specified? Also, what's the possible
invalid type problem when specifying the integers? I don't feel like I'm
understanding whether it's a >int-length (unsigned/long/etc.) issue or
something else. Any idea why the Vala enum isn't pulling the values
specified in libspotfiy/api.h?

This is because you are not using a VAPI for your binding, but a Vala
program.

As I understand it now, after some experimentation, a VAPI can be seen as a
translation file. It is used by the Vala compiler when it parses a Vala
program
to validate the identifier exists, in this case the Vala enum you have
created.

The second thing the Vala compiler uses the VAPI file for is to generate
the C code
from the Vala program using the binding. So the cname CCode attribute will
translate the Vala program name for the enum to the C code name. The
cprefix CCode
attribute will prepend SP_ERROR_ to all the enum values in the C code.
What the
Vala compiler doesn't do is generate a C code translation of the enum in
the VAPI
itself. This is because the C code is already there in the C header file
of your
library. By placing the Vala enum in a VAPI you stop two versions of the
enum
being generated in C code.

Hope that helps,

Al
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list



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