www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D interface for C library problems

reply "Oleg" <gaolong i.ua> writes:
Hello everyone.

I creating interface for gpgme 
(http://www.gnupg.org/related_software/gpgme/) and have several 
problems:

1. Library have variable with D keyword name:
/* The version string of the installed engine.  */
   char *version;

How can I create this variable in D interface?

2. Have an error in static function:
gpgme_err_code_t gpgme_err_code_from_syserror (void);

static inline gpgme_error_t
gpgme_error_from_syserror (void)
{
   return gpgme_error (gpgme_err_code_from_syserror ());
}

When I write D code like this:

gpgme_err_code_t gpgme_err_code_from_syserror();

gpgme_error_t gpgme_error_from_syserror() {
    return gpgme_error(gpgme_err_code_from_syserror());
}

I got error:

In function `gpgme_error_from_syserror':
src/main.d:(.text.gpgme_error_from_syserror+0x5): undefined 
reference to `gpgme_err_code_from_syserror'
--- errorlevel 1
collect2: ld returned 1 exit status

Anyone knows how to fix this?

Thanks and sorry for my English.
Oct 15 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-15 13:37, Oleg wrote:

 2. Have an error in static function:
 gpgme_err_code_t gpgme_err_code_from_syserror (void);

 static inline gpgme_error_t
 gpgme_error_from_syserror (void)
 {
    return gpgme_error (gpgme_err_code_from_syserror ());
 }

 When I write D code like this:

 gpgme_err_code_t gpgme_err_code_from_syserror();

 gpgme_error_t gpgme_error_from_syserror() {
     return gpgme_error(gpgme_err_code_from_syserror());
 }

 I got error:

 In function `gpgme_error_from_syserror':
 src/main.d:(.text.gpgme_error_from_syserror+0x5): undefined reference to
 `gpgme_err_code_from_syserror'
 --- errorlevel 1
 collect2: ld returned 1 exit status

 Anyone knows how to fix this?

 Thanks and sorry for my English.

You need to declare all functions you want to use from C as: extern (C) gpgme_err_code_t gpgme_err_code_from_syserror(); Then you also need to link with the C library. http://dlang.org/interfaceToC.html -- /Jacob Carlborg
Oct 15 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-10-16 01:33, 1100110 wrote:

 Also the names of the particular fields do not have to match exactly.
 In a few C projects, you will find a field with the name 'version' which
 is a D keyword.

 It seems to work just fine if you simply change it to 'ver' on similar.

 Take a look at the deimos project to see a few examples in action.
 (Also please attempt to get it into deimos if that is what you are
 thinking about.)

For structs the size only need to be the same. Depending on how you operate on the struct the size of the individual fields, alignment and padding should match as well. -- /Jacob Carlborg
Oct 15 2012
prev sibling next sibling parent "Oleg" <gaolong i.ua> writes:
On Monday, 15 October 2012 at 11:42:41 UTC, Jacob Carlborg wrote:
 On 2012-10-15 13:37, Oleg wrote:


 You need to declare all functions you want to use from C as:

 extern (C) gpgme_err_code_t gpgme_err_code_from_syserror();

 Then you also need to link with the C library.

 http://dlang.org/interfaceToC.html

I have extern wrapper: extern (C) { // code here }
Oct 15 2012
prev sibling next sibling parent "Oleg" <gaolong i.ua> writes:
There is code
http://pastebin.com/KyhFdx36
Oct 15 2012
prev sibling next sibling parent "denizzzka" <4denizzz gmail.com> writes:
On Monday, 15 October 2012 at 11:37:22 UTC, Oleg wrote:

 I got error:

 In function `gpgme_error_from_syserror':
 src/main.d:(.text.gpgme_error_from_syserror+0x5): undefined 
 reference to `gpgme_err_code_from_syserror'
 --- errorlevel 1
 collect2: ld returned 1 exit status

 Anyone knows how to fix this?

 Thanks and sorry for my English.

I think you missed a library with err codes and functions. You should used -L compiler flag or pragma(lib, "nameoflib");
Oct 15 2012
prev sibling next sibling parent 1100110 <0b1100110 gmail.com> writes:
On Mon, 15 Oct 2012 06:46:40 -0500, Oleg <gaolong i.ua> wrote:

 There is code
 http://pastebin.com/KyhFdx36

Looks pretty good, haven't tried it or anything yet. One suggestion that I would make is that you have code like: enum _gpgme_attr_t { ... } alias _gpgme_attr_t gpgme_attr_t; It would probably be better to just use: enum gpgme_attr_t { ... } from the beginning. Also the names of the particular fields do not have to match exactly. In a few C projects, you will find a field with the name 'version' which is a D keyword. It seems to work just fine if you simply change it to 'ver' on similar. Take a look at the deimos project to see a few examples in action. (Also please attempt to get it into deimos if that is what you are thinking about.) -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Oct 15 2012
prev sibling parent 1100110 <0b1100110 gmail.com> writes:
On Tue, 16 Oct 2012 01:42:07 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2012-10-16 01:33, 1100110 wrote:

 Also the names of the particular fields do not have to match exactly.
 In a few C projects, you will find a field with the name 'version' which
 is a D keyword.

 It seems to work just fine if you simply change it to 'ver' on similar.

 Take a look at the deimos project to see a few examples in action.
 (Also please attempt to get it into deimos if that is what you are
 thinking about.)

For structs the size only need to be the same. Depending on how you operate on the struct the size of the individual fields, alignment and padding should match as well.

That's what I suspected, but It's nice to know that for a fact. Thank you. -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Oct 16 2012