Skip to content

pokedpeter/vala-manual-vapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vala-manual-vapi

Using a manually created VAPI for a C library with Vala.

An very basic example C library that provides a method to sum two numbers is below.

The C file source: lib.c

#include <stdio.h>
#include "lib.h"

int add(int a, int b)
{
  printf("Adding numbers %d and %d\n", a, b);
  return a + b;
}

The header for the C file source: lib.h

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

The hand-crafted VAPI file: lib.vapi

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

Lastly, the vala file that will call the C library: test.vala

using MyMaths;

void main()
{
    int sum = add(5, 5);
    stdout.printf("Total: %d\n", sum);
}

Compile everything to generate a ./test executable:

valac test.vala lib.vapi lib.c

Run it, and you'll see the output below.

Adding numbers 5 and 5
Total: 10

About

Manually created VAPI for C library

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published