Search code examples
importvala

How can I import other files in Vala?


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


Solution

  • 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.