Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 643 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing C and D code in the same program?

#1
Is it possible? i.e. compile .c with dmc and .d with dmd and then link them together, will this work? Will I be able to call D functions from C code, share globals etc? Thanks.
Reply

#2
Yes it is possible. In fact this is one of the main feature of dmd. To call a D function from C, just make that function `extern©`, e.g.

// .d
import std.c.stdio;
extern © {
shared int x; // Globals without 'shared' are thread-local in D2.
// You don't need shared in D1.
void increaseX() {
++ x;
printf("Called in D code\n"); // for some reason, writeln crashes on Mac OS X.
}
}


<!-- -->

// .c
#include <stdio.h>
extern int x;
void increaseX(void);

int main (void) {
printf("x = %d (should be 0)\n", x);
increaseX();
printf("x = %d (should be 1)\n", x);
return 0;
}

See [*Interfacing to C*](

[To see links please register here]

) for more info.
Reply

#3
The above answer is wrong as far as I know.
Because the D main routine has to be called before you use any D functions.
This is necessary to "initialize" D, f.e. its garbage collection.
To solve that, you simply can make the program be entered by a main routine in D or you can somehow call the D main routine from C. (But I dont know exactly how this one works)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through