我想使用Vala破解现有的基于GLib的C项目.
基本上我正在做的是,在我的构建过程开始时,使用valac从我的.vala文件生成.c和.h文件,然后按照我任何.c或.h文件的方式编译生成的文件.
这可能不是最好的方式,但似乎在大多数情况下工作正常.
我的问题是我很难从Vala代码访问现有的C代码.是否有捷径可寻?
我已经尝试编写自己的.vapi文件(我对vala附带的工具没有任何好运),但我找不到任何关于如何编写这些文件的体面文档.
有没有?我是否需要其中一个文件来调用现有的C代码?
我想制作一个自定义的 vapi 文件,我有基本的东西,但我显然错过了一些东西,我找不到任何地方如何正确地做到这一点。我的主要目标是使用libtorrent创建一个 torent 应用程序,并使用 vala 和 gtk 创建 GUI(前端?)。
我有一个 c_func_head.h:
#ifndef WHATEVER_H_INCLUDED
#define WHATEVER_H_INCLUDED
int add(int a, int b);
#endif
Run Code Online (Sandbox Code Playgroud)
c_functions.c:
#include <stdio.h>
#include <stdlib.h>
#include "c_func_head.h"
int add(int a, int b){
printf("Adding numbers in c...\n");
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
vala_p.vapi:
[CCode (cheader_filename = "c_func_head.h")]
namespace MyFunc {
[CCode (cname = "add")]
public int add (int a, int b);
}
Run Code Online (Sandbox Code Playgroud)
最后是 vala_program.vala:
//extern int add(int a, int b);
using MyFunc;
void main(){
stdout.printf("Calling a c function...\n");
//stdout.printf("The …Run Code Online (Sandbox Code Playgroud) 我有以下C代码使用libmodbus使用ModbusTCP读取单个设备寄存器:
modbus_t *ctx;
uint16_t tab_reg[16];
ctx = modbus_new_tcp("10.0.1.77", 502);
modbus_read_registers(ctx, 0x20, 2, tab_reg);
printf("reg = %d (0x%X)\n", tab_reg[0], tab_reg[0]);
printf("reg = %d (0x%X)\n", tab_reg[1], tab_reg[1]);
Run Code Online (Sandbox Code Playgroud)
现在尝试使用我生成的Vapi将其切换到Vala,new和read的内容是:
[CCode (cheader_filename = "modbus.h", cname = "modbus_new_tcp")]
public static unowned Modbus.modbus_t create_tcp (string ip_address, int port);
public static int read_registers (Modbus.modbus_t ctx, int addr, int nb, uint16 dest);
[CCode (cheader_filename = "modbus.h")]
Run Code Online (Sandbox Code Playgroud)
翻译的Vala程序是:
class ModbusReadTest : GLib.Object {
unowned Modbus.modbus_t ctx;
public void run () {
uint16 reg = 0x00;
ctx = create_tcp ("10.0.1.77", …Run Code Online (Sandbox Code Playgroud) 我想使用vega库来处理 dicom 文件。其网站的示例代码如下:
#include <string>
#include "vega/dictionary/dictionary.h"
#include "vega/dicom/file.h"
int main() {
// Set the dictionary file
vega::dictionary::Dictionary::set_dictionary("/path/to/dictionary/dictionary.txt");
// Read the DICOM file in
const std::string file_name = "/path/to/dicom/file/dicom.dcm";
vega::dicom::File file(file_name);
// Print a human-friendly representation of the file to std::cout
vega::Formatter formatter(std::cout);
file.data_set()->log(formatter);
}
Run Code Online (Sandbox Code Playgroud)
本页解释了包括 C 代码,但是 C++ 代码呢?
该官方页面指出“如果该库是用C++编写的,则无法将其绑定到Vala,除非有C++库的单独C绑定(例如,LLVM)。”。因此,在我看来,我不能使用 vega 库。我对么?
编辑:另外,valabind / valabind-cc与swig有帮助吗?