www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - External array pointer

reply Jaap Geurts <Jaap_member pathlink.com> writes:
I've been converting a program to D and been successful so far. 
Interfacing
with C is not a big issue but, 
I've been trying to make a D module for the
ncurses library which works well 
except for the acs_map array. 
 
I realize
that the ncurses library is not the most modular one and it's 
stuffed with
macros but I have to use it. 
 
The general problem is as follows: The ncurses
library header file defines an 
external array pointer as follows: 
extern
chtype *acs_map; 
 
That array is filled by the library on run-time and contains
the terminal 
character conversion map. 
For now D seems unable to use because
it doesn't connect the D-symbol with the 
library at link time, and instead
defines a new pointer type. Of course 
accessing that results in a seg-fault.
To get around the problem I rename the acs_map pointer to sym_map and wrote a
small C function: 
 
#include <ncurses.h> 
extern chtype *sym_map; 
int
initLibrary() 
{ 
  sym_map = acs_map; 
} 
 
I compile it with C and link
everything together. 
This seems to work, but I don't like the solution.
Question: How do I define an external pointer int D that is resolved at link
time? 
 
Thanks 
Jaap 
 
Jun 23 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Jaap Geurts" <Jaap_member pathlink.com> wrote in message
news:cbdmv0$2pk2$1 digitaldaemon.com...
 Question: How do I define an external pointer int D that is resolved at

 time?

Easy. Create a module, call it foo.d: module foo; extern (C) char* sym_map; Import foo.d and use sym_map. But don't link in foo.obj! This technique is used in phobos' std.c.stdio for the _iob[] array.
Jul 01 2004
parent Jaap Geurts <Jaap_member pathlink.com> writes:
In article <cc0hlv$9gv$3 digitaldaemon.com>, Walter says... 
 
"Jaap Geurts"

news:cbdmv0$2pk2$1 digitaldaemon.com... 
 Question: How do I define an external pointer int D that is resolved at 

 time? 

Easy. Create a module, call it foo.d: module foo; extern (C) char* sym_map; Import foo.d and use sym_map. But don't link in foo.obj! This technique is used in phobos' std.c.stdio for the _iob[] array.

Thanks, Walter, Yes, that works. After some extensive debuggin I also figured out that this works: extern (C) { extern char sym_map[256]; } Even if I link this code in it still works. If I remove the extern keyword it doesn't. It that correct? Forgot to mention: D is EXCELLENT. I've been waiting for the simplicity of Java but be able to interface with and generate real executables. Jaap
Jul 01 2004