Accessing D globals in C
Thad via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Oct 28 08:24:02 PDT 2014
Hello,
I am mixing some of my existing C code with D via static linking.
I can access C globals from D using __gshared but I cannot seem
to be able to access a D global from within the C code. Keep in
mind I am a novice programmer. Everything is built with gcc and
gdc under Linux.
e.g.
//D code
import std.stdio;
extern (C) void print_global();
__gshared int global = 5;
void main(){
writeln("The global value is: ", global);
print_global(); //Call our C code
}
//C code
#include <stdio.h>
int global;
void print_global(){
printf("Global value: %d\n", global);
return;
}
If I compile and link the above two object files, the
print_global function prints "Global value: 0". But of course,
the writeln in the D code prints 5. If I put "extern int global;"
or remove "int global" in the C file, gdc exits with:
staticdC.o: In function `print_global':
staticdC.c:(.text+0xa5): undefined reference to `global'
Am I missing something? Or is accessing D globals from C not
possible?
More information about the Digitalmars-d-learn
mailing list