www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using D-object with C-executable

reply "stas" <stas onlineua.net> writes:
Hi guys!

I'm trying to write D module for existing C/C++ project with no 
success :(

What I'm doing:

[code]
dmd -g -w -c test.d
gcc -g -Wall -Werror test.c test.o -ldruntime -lphobos2 -lrt 
-pthread -o test-app
[/code]

Executable is linked successfully, but fails with "Segmentation 
fault".

test.d:
[code]
import std.stdio;

extern(C) int this_is_test_d_function(const char* test_data)
{
         writeln("Hello from C-called D!");
         return false;
}

void main() {}
[/code]

test.c:
[code]
int this_is_test_d_function(const char* test_data);

int main(int argc, char**argv)
{
         this_is_test_d_function("test C line");

         return 0;
}
[/code]

Is it possible at all?
Mar 14 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
gotta init the d runtime first

extern (C)
         void startd() {
                 Runtime.initialize();
         }


adn then in C, before using any other D, call startd();
Mar 14 2013
parent "stas" <stas onlineua.net> writes:
Thanks Adam! You saved my day ;)
Mar 14 2013