Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 15, 2005 cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Hi all, I just recently downloaded the D linux compiler dmd and started playing around with it. D programming language is completely new to me but my general impression is that it is great! It allows me to write some routines I've previously implemented in C much faster and without memory leakages. I should say that I'm a PhD student in Germany doing its research in a big company. In this company it is quite usual to use C programming language but, since I'm doing research, I'm free to use the tools I want to achieve my purpose. When integrating developed routines (from me or from other colleges for example) into bigger projects, normally two approaches are normally taken: creating a linux library (put in /usr/lib for example), or an object file (.o) to be included and used directly for compilation, if the proper header files (.h) are present. My question is the following: If I use D to develop software and later on I or someone else needs to integrate the routines into a larger project, how can this be done? I've read something about using C routines in D but, is it possible to do it the other way around? What happens with the memory allocation routines and the garbage collector? If this is possible to be done, can someone please provide me with a simple example such that I can start toying around? Thanks, Tiago Gasiba |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tiago Gasiba | Yes, it's possible, in short you specify C linkage for the exported D functions.
Yes, the GC causes problems, for example if you pass some memory to the C code, but no longer have a reference in the D code, the GC may collect it. The solution is probably to keep a list of memory in the D code, and explicitly free it from the C code, or to use malloc to obtain the memory, or to avoid doing this alltogether.
Have a quick look at the " Calling D code from C" thread in this NG. Also the "Calling D functions from C" thread in the digitalmars.D NG.
Regan
On Wed, 15 Jun 2005 03:31:09 +0200, Tiago Gasiba <tiago.gasiba@gmail.com> wrote:
> Hi all,
>
> I just recently downloaded the D linux compiler dmd and started playing
> around with it. D programming language is completely new to me but my
> general impression is that it is great! It allows me to write some routines
> I've previously implemented in C much faster and without memory leakages.
> I should say that I'm a PhD student in Germany doing its research in a big
> company. In this company it is quite usual to use C programming language
> but, since I'm doing research, I'm free to use the tools I want to achieve
> my purpose.
> When integrating developed routines (from me or from other colleges for
> example) into bigger projects, normally two approaches are normally taken:
> creating a linux library (put in /usr/lib for example), or an object file
> (.o) to be included and used directly for compilation, if the proper header
> files (.h) are present.
>
> My question is the following: If I use D to develop software and later on
> I or someone else needs to integrate the routines into a larger project,
> how can this be done? I've read something about using C routines in D but,
> is it possible to do it the other way around? What happens with the memory
> allocation routines and the garbage collector?
> If this is possible to be done, can someone please provide me with a
> simple example such that I can start toying around?
>
> Thanks,
> Tiago Gasiba
>
|
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > Yes, it's possible, in short you specify C linkage for the exported D functions. > > Yes, the GC causes problems, for example if you pass some memory to the C code, but no longer have a reference in the D code, the GC may collect it. The solution is probably to keep a list of memory in the D code, and explicitly free it from the C code, or to use malloc to obtain the memory, or to avoid doing this alltogether. <snip> Yes, keeping a pointer on the D side works at the moment, but it might not work once a copying GC is out. http://www.digitalmars.com/drn-bin/wwwnews?D/26273 Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Hi all, Where can I get the dstub.d and cstub.c in order to compile D libraries in C? Googling around I could find nothing, and the links given in the thread: www.digitalmars.com/d/archives/ digitalmars/D/learn/630.html do not exist anymore... Thanks! |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Hi all, What am I doing wrong in the following code? The idea is to try to pass some values (through a variable) from a function in D into a function in C. The code compiles w/o problems, but does not execute properly - its gets a Segmentation fault. ---------------- (SOURCE) ---------------- -------- Makefile -------- C = gcc D = dmd LIB_OPTS_D = -c -w -g LIB_OPTS_C = -c -Wall -g OPTS_C = -Wall -g all: teste teste: teste.o libd.o $(C) $(OPTS_C) -o teste teste.o libd.o -lphobos -lpthread -lm teste.o: teste.c $(C) $(LIB_OPTS_C) teste.c libd.o: libd.d $(D) $(LIB_OPTS_D) libd.d clean: $(RM) teste $(RM) *.o -------- teste.c -------- #include <stdio.h> #include <stdlib.h> int func2( int * ); int main( int argc, char **argv ){ int *X, i, n; n = func(X); for( i=0; i<n; i++ ) printf("%d\n",X[i]); return 0; } -------- libd.d -------- module libd; const int LEN = 2; extern (C) int func( int *X ){ printf("func - 1\n"); int [] Y; printf("func - 2\n"); Y.length=LEN; printf("func - 3\n"); Y[] = 4; printf("func - 4\n"); X = Y; printf("func - 5\n"); return LEN; } int main( char [][]argv ){ return 0; } ---------------- (END) ---------------- Tiago |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tiago Gasiba | You have both a main() in the C program, and a main() in the D program. This will prevent the D startup code from being executed - see the code in phobos/internal/dmain2.d. |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tiago Gasiba | Hi *, Sorry, small mistake with copy-paste - the prototype of the D function (in the file teste.c) is obviously: int func( int * ); and not func2 as in my previous post. Still, the question remains, where is the bug? :) Thanks, Tiago |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > You have both a main() in the C program, and a main() in the D program. > This will prevent the D startup code from being executed - see the code in > phobos/internal/dmain2.d. Great! I have done some changes (shown later in the email), and now the code compiles and does not give any segmentation fault. The solution I have found is pretty ugly, but it works. However, the C function does not get the correct values from D! I get the following result, by executing the program: gasiba@linux:~/D/D in C> ./teste Entering D... func - 1 func - 2 func - 3 func - 4 func - 5 4 4 Leaving D... 134566464 134566644 ---------------- (CHANGED FILES) ---------------- -------- teste.c -------- #include <stdio.h> #include <stdlib.h> int func( int * ); int main2( void ){ int *X, i, n; n = func(X); for( i=0; i<n; i++ ) printf("%d\n",X[i]); return 0; } -------- libd.d -------- module libd; const int LEN = 2; extern (C) int func( int *X ){ printf("Entering D...\n"); printf("func - 1\n"); int [] Y; printf("func - 2\n"); Y.length=LEN; printf("func - 3\n"); Y[] = 4; printf("func - 4\n"); X = Y; printf("func - 5\n"); for( int i=0; i<Y.length; i++ ) printf("%d\n",X[i]); printf("Leaving D...\n"); return LEN; } extern (C) int main2(); int main( char [][]argv ){ return main2(); } ---------------- (END) ---------------- Tiago |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tiago Gasiba | Tiago Gasiba wrote: > Great! > I have done some changes (shown later in the email), and now the code > compiles and does not give any segmentation fault. > The solution I have found is pretty ugly, but it works. > However, the C function does not get the correct values from D! > extern (C) int func( int *X ){ > printf("Entering D...\n"); > printf("func - 1\n"); > int [] Y; > printf("func - 2\n"); > Y.length=LEN; > printf("func - 3\n"); > Y[] = 4; > printf("func - 4\n"); > X = Y; You're modifying a local copy of the pointer, not the one that you have in your C function. -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/ |
June 15, 2005 Re: cooperation between D and C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom S | On Thu, 16 Jun 2005 00:15:34 +0200, Tom S <h3r3tic@remove.mat.uni.torun.pl> wrote: > Tiago Gasiba wrote: >> Great! >> I have done some changes (shown later in the email), and now the code >> compiles and does not give any segmentation fault. >> The solution I have found is pretty ugly, but it works. >> However, the C function does not get the correct values from D! > >> extern (C) int func( int *X ){ >> printf("Entering D...\n"); >> printf("func - 1\n"); >> int [] Y; >> printf("func - 2\n"); >> Y.length=LEN; >> printf("func - 3\n"); >> Y[] = 4; >> printf("func - 4\n"); >> X = Y; > > > You're modifying a local copy of the pointer, not the one that you have in your C function. I think it's even worse than that. The variable 'Y' is on the stack of the function 'func' and can thus be collected/destroyed after the function has returned. Try: -------- teste.c -------- #include <stdio.h> #include <stdlib.h> int func( int ** ); int main2( void ){ int *X, i, n; n = func(&X); for( i=0; i<n; i++ ) printf("%d\n",X[i]); return 0; } -------- libd.d -------- module libd; const int LEN = 2; int[LEN] Y; extern(C) int func(int **X) { Y[] = 4; *X = Y.ptr; } extern (C) int main2(); int main( char [][]argv ){ return main2(); } Regan |
Copyright © 1999-2021 by the D Language Foundation