且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Vala中导入其他文件?

更新时间:2022-11-22 13:08:13

您不直接这样做.如果运行valac file1.vala file2.vala,就好像您将它们编译到一个大文件中一样.

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.

如果要使其可重用,则可能需要共享库.在这种情况下,您可以编译生成一个C头文件和一个VAPI定义:

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

第二个可以吃掉这个:

valac --pkg file1 file2.vala

这假定已安装VAPI文件.如果不是这种情况,则需要传递--vapidirfile1.vapi所在的位置,可能是..同样,您还需要将file1.h-X -I/directory/containing一起存放的地方告知C编译器,可能又是-X -I..最后,您需要通过-X -L/directory/containing -X -lfile1告诉C链接器libfile1.so的位置.这是针对特定平台的,您可以使用AutoMake消除差异,尽管这涉及更多. Ragel是如何与Vala一起使用AutoMake的常用项目.

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.