Jump to page: 1 2 3
Thread overview
Interfacing to C global variables ?
Apr 25, 2004
Matthew
Apr 25, 2004
Matthew
Apr 25, 2004
Matthew
Apr 25, 2004
Matthew
Apr 25, 2004
Ilya Minkov
Apr 25, 2004
J Anderson
Apr 25, 2004
Walter
Apr 26, 2004
Walter
Apr 26, 2004
Walter
Apr 26, 2004
John Reimer
Apr 26, 2004
Walter
Apr 26, 2004
John Reimer
Apr 26, 2004
Ilya Minkov
April 25, 2004
I've made a DLL with VC++ which contains two symbols:

DLL_API int value = 10;

DLL_API int *get_value_addr()
{
    return &value;
}

Then I used implib.exe to make a dll.lib for using with D.

Then I made the following D program to test access to DLL symbols:

import std.c.stdio;

extern(C) int value;
extern(C) int* get_value_addr();

int main(char[][] args)
{
     printf("value=%i\n", value);
     printf("addrof(value)=%p\n", &value);
     printf("get_value_addr=%p\n", get_value_addr());
     printf("*get_value_addr=%i\n", *get_value_addr());
     return 0;
}

The program output is:

value=0
addrof(value)=004130E0
get_value_addr=10006030
*get_value_addr=10

Which means that I can't access a C global variable from D; the function is accessed ok, but the global variable is not.

Is this a bug ? is there some option that I should pass at the compiler ?

I am trying to port the Allegro multimedia library to D. Allegro functions work ok, but Allegro global variables can't be accessed.



April 25, 2004
"Achilleas Margaritis" <axilmar@in.gr> wrote in message news:c6gejo$1r7v$1@digitaldaemon.com...
> I've made a DLL with VC++ which contains two symbols:
>
> DLL_API int value = 10;
>
> DLL_API int *get_value_addr()
> {
>     return &value;
> }
>
> Then I used implib.exe to make a dll.lib for using with D.
>
> Then I made the following D program to test access to DLL symbols:
>
> import std.c.stdio;
>
> extern(C) int value;
> extern(C) int* get_value_addr();
>
> int main(char[][] args)
> {
>      printf("value=%i\n", value);
>      printf("addrof(value)=%p\n", &value);
>      printf("get_value_addr=%p\n", get_value_addr());
>      printf("*get_value_addr=%i\n", *get_value_addr());
>      return 0;
> }
>
> The program output is:
>
> value=0
> addrof(value)=004130E0
> get_value_addr=10006030
> *get_value_addr=10
>
> Which means that I can't access a C global variable from D; the function is accessed ok, but the global variable is not.

I've *never* once seen a good reason to export data, rather than functions providing access to data, from a DLL. I recommend you change the design.

(btw, for anyone who wants to tell me about exporting static class variables from DLLs, I acknowledge the technical feasibility of such things, but suggest that it's notionally a very bad idea still. basic_string::npos? No thanks!)



April 25, 2004
Achilleas Margaritis schrieb:

> Which means that I can't access a C global variable from D; the function is
> accessed ok, but the global variable is not.
> 
> Is this a bug ? is there some option that I should pass at the compiler ?

This is a principal limitation of DLLs. Variables don't get exported.

> I am trying to port the Allegro multimedia library to D. Allegro functions
> work ok, but Allegro global variables can't be accessed.

Can you compile Allegro with DMC?

If i remember correctly, Allegro is not designed to be linked dynamically.

BTW, why Allegro and not libSDL, which was already ported by someone?

-eye
April 25, 2004
> I've *never* once seen a good reason to export data, rather than functions providing access to data, from a DLL. I recommend you change the design.
>

I agree, but Allegro is not mine... It is a big library, which you can find it here:

www.allegro.cc

Do you know if exported data can be accessed from D ? if not, I would have to make an additional DLL with the functions which provide access to Allegro's global data.



April 25, 2004
> If i remember correctly, Allegro is not designed to be linked dynamically.

Oh yes, it does. It comes in 2 flavors: dynamic, in form of DLL, and static.

>
> BTW, why Allegro and not libSDL, which was already ported by someone?

SDL is not the same with Allegro: All SDL does is provide access to the framebuffer and mouse/keyboard input, whereas Allegro can do this and sound, 3d, gui, drawing routines, filesystem, compression, sprites, and lots of other things SDL does not do.




April 25, 2004
Why not link in with a C static lib, for which you write a D layer. (Look at recls for an example, albeit it's far larger than what you may need here, but it'll give you the general idea of mapping from another language.)

"Achilleas Margaritis" <axilmar@in.gr> wrote in message news:c6gjms$233m$1@digitaldaemon.com...
>
> > I've *never* once seen a good reason to export data, rather than functions providing access to data, from a DLL. I recommend you change the design.
> >
>
> I agree, but Allegro is not mine... It is a big library, which you can find it here:
>
> www.allegro.cc
>
> Do you know if exported data can be accessed from D ? if not, I would have to make an additional DLL with the functions which provide access to Allegro's global data.
>
>
>


April 25, 2004
Achilleas Margaritis wrote:

>>If i remember correctly, Allegro is not designed to be linked dynamically.
>>    
>>
>
>Oh yes, it does. It comes in 2 flavors: dynamic, in form of DLL, and static.
>
>  
>
>>BTW, why Allegro and not libSDL, which was already ported by someone?
>>    
>>
>
>SDL is not the same with Allegro: All SDL does is provide access to the
>framebuffer and mouse/keyboard input, whereas Allegro can do this and sound,
>3d, gui, drawing routines, filesystem, compression, sprites, and lots of
>other things SDL does not do.
>  
>
SDL can do sound and sprites and can link into openGL (3d) but not some of those other things.

-- 
-Anderson: http://badmama.com.au/~anderson/
April 25, 2004
"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:c6glbu$25m6$1@digitaldaemon.com...
> Why not link in with a C static lib, for which you write a D layer. (Look
at
> recls for an example, albeit it's far larger than what you may need here,
but
> it'll give you the general idea of mapping from another language.)
>

But D is supposed to interface directly with C.

Anyway, a layer is not really needed: allegro is usable from D, except for global variables. I will have to write a short DLL with getters/setters of the global variables.



April 25, 2004
"Achilleas Margaritis" <axilmar@in.gr> wrote in message news:c6gejo$1r7v$1@digitaldaemon.com...
> Which means that I can't access a C global variable from D; the function
is
> accessed ok, but the global variable is not.
>
> Is this a bug ? is there some option that I should pass at the compiler ?
>
> I am trying to port the Allegro multimedia library to D. Allegro functions work ok, but Allegro global variables can't be accessed.

The problem is that, in D, a declaration is a definition. So

    int x;

will declare and define x. The trick to referencing a C global variable is to make a module, let's say foo.d:
-------------------
extern (C) int x;
-------------------

And then in your code:
-----------------------
import foo;

... and then refer to foo.x ...
-----------------------

but do NOT link in foo.obj. You'll find this trick is used in std.c.stdio to refer to _iob[]. stdio.obj is NOT added to phobos.lib, and so the linker will look elsewhere for a definition of _iob[].


April 25, 2004
"Achilleas Margaritis" <axilmar@in.gr> wrote in message news:c6h151$2oao$1@digitaldaemon.com...
>
> "Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:c6glbu$25m6$1@digitaldaemon.com...
> > Why not link in with a C static lib, for which you write a D layer. (Look
> at
> > recls for an example, albeit it's far larger than what you may need here,
> but
> > it'll give you the general idea of mapping from another language.)
> >
>
> But D is supposed to interface directly with C.
>
> Anyway, a layer is not really needed: allegro is usable from D, except for global variables. I will have to write a short DLL with getters/setters of the global variables.

I'm saying you don't need to have it as a DLL, just a bit of linked-in C.


« First   ‹ Prev
1 2 3