digitalmars.D - How to make D libraries callable from C?
- %u (8/8) Dec 18 2006 Hello,
- Gregor Richards (8/17) Dec 18 2006 All you need is to use extern (C) on those functions you want to be
- Brad Roberts (19/42) Dec 18 2006 There's more to it than that.
- Alain (16/34) Dec 19 2006 part
- Tomas Lindquist Olsen (5/40) Dec 19 2006 The Win32 DLL in D article at digitalmars.com sums it up pretty well I
Hello, I am a D newbie and my company makes heavy use of C. I would like to demonstrate that the D language can be beneficial much more than Java. My question is: is it possible to make a library, that is callable from C? I guess i would have to write the corresponding .h file for inclusion, with possibly a C-wrapper around the D classes. Thank you for helping. Alain
Dec 18 2006
%u wrote:Hello, I am a D newbie and my company makes heavy use of C. I would like to demonstrate that the D language can be beneficial much more than Java. My question is: is it possible to make a library, that is callable from C? I guess i would have to write the corresponding .h file for inclusion, with possibly a C-wrapper around the D classes. Thank you for helping. AlainAll you need is to use extern (C) on those functions you want to be callable from C. D: extern (C) void doStuff(int withThis) { ... } C header: void doStuff(int); - Gregor Richards
Dec 18 2006
Gregor Richards wrote:%u wrote:There's more to it than that. The D runtime will need to be initialized unless special care is taken to avoid anything that's going to touch the GC or anything else that requires initialization. (it's been a while since I looked at that part of phobos/ares. You'll have to be careful to keep the interfaces to mutually compatible data types. D doesn't prevent you from writing something like: extern (C) char[] someFunc(SomeClass a, SomeAlias b, SomeDelegate c) Essentially all the extern C part does is drop the name mangling. Don't let exceptions leak out of this D/C barrier.. C doesn't know anything about exceptions. If instead of C you really meant C++, the same still applies except with dmd on windows. Every other combination of language/compiler/os is currently incompatible, though I'd love to see this fixed. There might be something else that needs to be handled as well, but those are the things that come to mind quickly. Later, BradHello, I am a D newbie and my company makes heavy use of C. I would like to demonstrate that the D language can be beneficial much more than Java. My question is: is it possible to make a library, that is callable from C? I guess i would have to write the corresponding .h file for inclusion, with possibly a C-wrapper around the D classes. Thank you for helping. AlainAll you need is to use extern (C) on those functions you want to be callable from C. D: extern (C) void doStuff(int withThis) { ... } C header: void doStuff(int); - Gregor Richards
Dec 18 2006
== Quote from Brad Roberts (braddr puremagic.com)'s articleThere's more to it than that. The D runtime will need to be initialized unless special care istakento avoid anything that's going to touch the GC or anything else that requires initialization. (it's been a while since I looked at thatpartof phobos/ares. You'll have to be careful to keep the interfaces to mutuallycompatibledata types. D doesn't prevent you from writing something like: extern (C) char[] someFunc(SomeClass a, SomeAlias b,SomeDelegate c)Essentially all the extern C part does is drop the name mangling. Don't let exceptions leak out of this D/C barrier.. C doesn't know anything about exceptions. If instead of C you really meant C++,thesame still applies except with dmd on windows. Every othercombinationof language/compiler/os is currently incompatible, though I'd lovetosee this fixed. There might be something else that needs to be handled as well, but those are the things that come to mind quickly. Later, BradAll you warn me against seems reasonable. D being a higher-level language than C, you have to down-grade it by writing a wrapper around the D library in order to make it understandable by the C application. What i miss however is what you call the 'D runtime' initialisation. Could you give a concrete example of what needs to be done. Thanks in advance Alain
Dec 19 2006
Alain wrote:== Quote from Brad Roberts (braddr puremagic.com)'s articleThe Win32 DLL in D article at digitalmars.com sums it up pretty well I think: http://www.digitalmars.com/d/dll.html --There's more to it than that. The D runtime will need to be initialized unless special care istakento avoid anything that's going to touch the GC or anything else that requires initialization. (it's been a while since I looked at thatpartof phobos/ares. You'll have to be careful to keep the interfaces to mutuallycompatibledata types. D doesn't prevent you from writing something like: extern (C) char[] someFunc(SomeClass a, SomeAlias b,SomeDelegate c)Essentially all the extern C part does is drop the name mangling. Don't let exceptions leak out of this D/C barrier.. C doesn't know anything about exceptions. If instead of C you really meant C++,thesame still applies except with dmd on windows. Every othercombinationof language/compiler/os is currently incompatible, though I'd lovetosee this fixed. There might be something else that needs to be handled as well, but those are the things that come to mind quickly. Later, BradAll you warn me against seems reasonable. D being a higher-level language than C, you have to down-grade it by writing a wrapper around the D library in order to make it understandable by the C application. What i miss however is what you call the 'D runtime' initialisation. Could you give a concrete example of what needs to be done. Thanks in advance Alain
Dec 19 2006