How can I import other files in Vala?

限于喜欢 提交于 2019-12-08 21:14:42

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/20457985/how-can-i-import-other-files-in-vala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!