Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import other files in Vala?

Tags:

import

vala

The question pretty much says it all- how could I import file2.vala to file1.vala?

like image 406
hkk Avatar asked Dec 08 '13 19:12

hkk


2 Answers

You don't do it directly. If you run valac file1.vala file2.vala, it is as if you compiled them in one big file.

If you want to make them reusable, then you probably want a shared library. In which case, you compile one to produce a C header file and a VAPI definition:

valac --vapi file1.vapi -H file1.h --library libfile1.so file1.vala

The second one can then consume this:

valac --pkg file1 file2.vala

This assume that the VAPI file has been installed. If this is not the case, you'll need to pass --vapidir and the location where file1.vapi exists, probably .. Similarly, you'll need to inform the C compiler about where file1.h lives with -X -I/directory/containing, again, probably -X -I.. Finally, you'll need to tell the C linker where libfile1.so is via -X -L/directory/containing -X -lfile1. This is a little platform specific, and you can smooth the difference out using AutoMake, though this is a bit more involved. Ragel is the usual go-to project for how to use AutoMake with Vala.

like image 143
apmasell Avatar answered Nov 01 '22 14:11

apmasell


just to supply apmasell:

you can use multiple files by using classes and public variables:

main.vala:

extern void cfunction(string text);

void main ()
{
    first f = new first ();
    f.say_something(f.mytext);
    cfunction("c text\n");
}

class.vala:

public class first {

    public string mytext = "yolo\n";
    public first ()
    {
        stdout.printf("text from constructer in first\n");
    }

    public void say_something(string text)
    {
        stdout.printf("%s\n", text);
    }
}

text.c:

#include <stdio.h>

void cfunction(const char *s)
{
    puts("This is C code");
    printf("%s\n", s);
}

compiles with: valac class.vala main.vala text.c

as you can see, you can even use C code

like image 38
user69969 Avatar answered Nov 01 '22 14:11

user69969