Interfacing to C. extern (C) variables

Alex_Dovhal alex_dovhal at yahoo.com
Sun Jun 26 12:54:57 PDT 2011


Hi. I try to interface D module with C one. See:

* file test.c:
#include <stdio.h>
int c_var;
int func()
{
    c_var = 1234;
    printf("In C\n");
    printf("c_var = %d\n", c_var);
    printf("&c_var = %x\n", &c_var);
    return 0;
}

* file unit.d:
module unit;
import std.stdio;
extern(C){
    int c_var;
    int func();
}

void main()
{
    func();
    writeln("In D");
    writefln("c_var = %d", c_var);
    writefln("&c_var = %x", &c_var);
}

And compile:
    dmc -c test.c
    dmd unit.d test.obj

I'd like to call C functions and use C variables in D module.
D calls C functions just fine, but how can I use C variables?

This program outputs:
In C
c_var = 1234
&c_var = 45d004
In D
c_var = 0
&c_var = 14298c




More information about the Digitalmars-d-learn mailing list