我尝试使用libgcrypt的.vapi在Vala中编译代码。 我在系统中安装了库,然后在c中运行测试。用C编写的代码编译没有错误: 我输入以下命令来编译C代码:
gcc -o main main.c -l gcrypt
代码运行完美。
但是,在Vala中编写的以下代码不能使用以下命令进行编译:
valac --pkg gcrypt --Xcc=-lgcrypt main.vala
main.vala是
using GCrypt;
public static int main(string[] args)
{
Hash h;
Hash.open(out h, Hash.Algorithm.MD5, Hash.Flag.SECURE);
return 0;
}
我不知道我做错了什么。
EDITED: 很抱歉没有发布错误,但有:
Loaded package /usr/share/vala-0.30/vapi/glib-2.0.vapi'
Loaded package /usr/share/vala-0.30/vapi/gobject-2.0.vapi'
Loaded package /usr/share/vala-0.30/vapi/gcrypt.vapi'
cc -o '/home/kyul/Documents/Codes/Vala/test_gcrypt/main' '/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c' -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lgobject-2.0 -lglib-2.0 '-lgcrypt'
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c: In function ‘_vala_main’:
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c:22:38: error: ‘GCRY_MD_FLAGSECURE’ undeclared (first use in this function)
gcry_md_open (&_tmp0_, GCRY_MD_MD5, GCRY_MD_FLAGSECURE);
^
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c:22:38: note: each undeclared identifier is reported only once for each function it appears in
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)
答案 0 :(得分:0)
很可能您错过了.vapi
文件。
$ wget https://raw.githubusercontent.com/nemequ/vala-extra-vapis/master/gcrypt.vapi
$ patch < gcrypt.patch
$ valac --vapidir=. --pkg gcrypt --Xcc=-lgcrypt main.vala
$ ./main
$ echo $?
0
我之前从未使用过来自vala的gcrypt api,并且必须修补gcrypt.vapi
以使您的示例正常工作,但补丁很简单:
--- gcrypt.vapi
+++ gcrypt.vapi
@@ -619,7 +619,7 @@
public Error get_oid (uchar[] buffer);
}
- [CCode (cname = "enum gcry_md_flags", cprefix = "GCRY_MD_FLAG")]
+ [CCode (cname = "enum gcry_md_flags", cprefix = "GCRY_MD_FLAG_")]
public enum Flag {
SECURE,
HMAC
关于您的密码错误的 更新,显然Cipher
类缺少free_function
映射:
--- a/gcrypt.vapi 2016-02-22 21:40:34.458977385 +0200
+++ b/gcrypt.vapi 2016-02-22 21:39:59.662976449 +0200
@@ -560,7 +560,7 @@
CBC_MAC /* Enable CBC message auth. code (MAC). */
}
[Compact]
- [CCode (cname = "gcry_cipher_hd_t", lower_case_cprefix = "gcry_cipher_")]
+ [CCode (cname = "struct gcry_cipher_handle", lower_case_cprefix = "gcry_cipher_", free_function = "gcry_cipher_close")]
public class Cipher {
public static Error open (out Cipher cipher, Algorithm algo, Mode mode, Flag flags);
public void close ();