4

I want to make a custom vapi file, I have the basic stuff but I obviously miss something and I can't find anywhere how to do this properly. My main goal is to create a torrent app, using libtorrent, and create the GUI (the frontend?) with vala and gtk.

I have a c_func_head.h:

#ifndef WHATEVER_H_INCLUDED
#define WHATEVER_H_INCLUDED
int add(int a, int b);
#endif

c_functions.c:

#include <stdio.h>
#include <stdlib.h>
#include "c_func_head.h"

int add(int a, int b){
  printf("Adding numbers in c...\n");
  return a+b;
}

vala_p.vapi:

[CCode (cheader_filename = "c_func_head.h")]
namespace MyFunc {
    [CCode (cname = "add")]
    public int add (int a, int b);
}

and finally vala_program.vala:

//extern int add(int a, int b);
using MyFunc;

void main(){
    stdout.printf("Calling a c function...\n");
    //stdout.printf("The sum is: %d\n", add2number(2, 2));
    int sum = add(2, 2);
    stdout.printf("The sum is: %d\n", sum);
}

As you can see I used an extern too, it worked with it but I want to use vapi files.

I compiled with (everything is in the same folder):

valac vala_program.vala --vapidir=vala_p.vapi -o mustrun

and the error is:

The namespace name `MyFunc' could not be found using MyFunc;

One more thing. Is it possible to make bindings for libtorrent? It uses C++ and I guess I have to use C++ too.

1 Answer 1

5

You can't make Vala bindings of C++ code. Only C. There a a guide to writing legacy bindings and a binding for Transmission, which is C-based.

As for the specific error you have, you want to call valac vala_program.vala vala_p.vapi if the library (i.e., header files) are the the same directory or valac vala_program.vala --pkg vala_p --vapidir=/path/to/directory/containing/vapi.

2
  • When I compiled with the first I got undefined reference to 'add' function. The second method says that it cannot find the "c_func_head.h" file. (I created an .o file too but no luck)
    – Levi
    Commented Jan 18, 2014 at 10:35
  • In the first, you still have to provide the C code. You can do valac vala_program.vala vala_p.vapi c_functions.c if it is raw source that you want included. If not, use -X to give the right arguments to the C compiler to link against your code. For the second, the C compiler doesn't know where to search for headers; try passing -X -I. to include the current directory.
    – apmasell
    Commented Jan 18, 2014 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.