| Thread overview | |||||
|---|---|---|---|---|---|
|
October 28, 2014 Accessing D globals in C | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
October 28, 2014 Re: Accessing D globals in C | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Thad | 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;
| |||
October 28, 2014 Re: Accessing D globals in C | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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:
>> 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;
Ah! That solved it. Thank you for the clarification.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply