为 Vala 编译 gcrypt

Compile gcrypt for Vala

我正在尝试使用 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;
    }

我不知道我做错了什么。

已编辑: 很抱歉没有 post 错误,但是有:

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)

很可能您缺少 .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 class 丢失了 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 ();