cgccvalalibgcrypt

Compile gcrypt for Vala


I'm try to compile a code write in Vala using a .vapi of libgcrypt. I installed the library in the system and i run a test in c. The code write in C compiles without errors: I typed the following command to compile the C code:

gcc -o main main.c -l gcrypt

The code runs perfectly.

But, the following code write in Vala don't compile using the command:

valac --pkg gcrypt --Xcc=-lgcrypt main.vala

The main.vala is

using GCrypt;
    public static int main(string[] args)
    {
        Hash h;
        Hash.open(out h, Hash.Algorithm.MD5, Hash.Flag.SECURE);
        return 0;
    }

I don't know what i doing wrong.

EDITED: Sorry for not post the error, but there is:

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)


Solution

  • Most likely You are missing the .vapi file.

    $ 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
    

    I've never used gcrypt api from vala before and had to patch gcrypt.vapi for Your example to work, but the patch is trivial:

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

    Update: concerning Your cipher errors, obviously the Cipher class is missing free_function mapping:

    --- 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 ();