digitalmars.D.learn - Accessing D globals in C
- Thad (30/30) Oct 28 2014 Hello,
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (9/39) Oct 28 2014 There are only two small things you need to change:
- Thad (2/49) Oct 28 2014 Ah! That solved it. Thank you for the clarification.
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?
Oct 28 2014
On Tuesday, 28 October 2014 at 15:24:03 UTC, Thad wrote:
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?
There are only two small things you need to change:
// D code
// this is necessary to get the name mangling right
extern(C) __gshared int global = 5;
// C code
// `extern` to declare that it should not reserve space
// for the variable in this compilation unit
extern int global;
Oct 28 2014
On Tuesday, 28 October 2014 at 16:42:20 UTC, Marc Schütz wrote:On Tuesday, 28 October 2014 at 15:24:03 UTC, Thad wrote:Ah! That solved it. Thank you for the clarification.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?There are only two small things you need to change: // D code // this is necessary to get the name mangling right extern(C) __gshared int global = 5; // C code // `extern` to declare that it should not reserve space // for the variable in this compilation unit extern int global;
Oct 28 2014








"Thad" <tswoskowiak gmail.com>