Writing .di files
1100110
10equals2 at gmail.com
Fri Jun 22 23:42:04 PDT 2012
On Fri, 22 Jun 2012 13:13:52 -0500, Minas Mina
<minas_mina1990 at hotmail.co.uk> wrote:
> I'm sorry, what I meant was "how to interface to C code". Sorry for
> writing it in a bad way.
>
> Do I just declare the C code as extern and then link together with the C
> .lib/.o/.so file? (I'm in Ubuntu)
>
> What about the stuff that is in header files?
Take a look at the Deimos Repos on Github.
https://github.com/D-Programming-Deimos
Take a look at ncurses there. There are two folders, C and deimos.
It needs to be cleaned up, but you'll notice that the .d files are really
nothing more than a direct
translation of the C header files.
Include the translated header and link with the C library.
If you have a specific library that you want to interface to, it's pretty
easy once you realize what needs to be done.
Global variables in C are truly global, but in D they are Thread Local.
So certain variables will need to be immutable, shared, __gshared,
whatever.
Everything will need to be wrapped with 'extern (C)'
The biggest issues your likely to face are the Thread Local Storage issues
and
the preprocessor directives.
dmd -vtls will print out the problem vars...
You can translate partial header files, just make sure you get all the
relevant bits.
If you wanna play around, then add this to a file and compile it.
import std.string: toStringz;
extern (C) int system(in char* command);
auto newTerm(S:string)(S command)
{ return system(command.toStringz); }
Yeah, I hate dealing with toStringz directly....
Unlike the functions in std.process, this one will allow interactive
programs to be run from inside a D file.
try newTerm("vim ~/.vimrc");
Yeah i just killed my terminal making sure std.process wouldn't work with
that.
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d-learn
mailing list