digitalmars.D - Calling D from C
- Chris W. (12/12) Mar 12 2012 I have a problem when calling D functions from C. While I can
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (6/18) Mar 12 2012 Are you remembering to initialize the runtime, attach your thread,
- Steven Schveighoffer (4/16) Mar 12 2012 If C is running your application startup, you must initialize D's runtim...
- Chris W. (8/30) Mar 12 2012 Yes, I am using extern (C) and in my C main function I call
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (7/34) Mar 12 2012 Don't forget to call this:
- Chris W. (5/52) Mar 12 2012 Thanks a million, calling rt_init(); in my C code did the trick.
- Steven Schveighoffer (10/45) Mar 12 2012 ut
- Steven Schveighoffer (12/55) Mar 12 2012 :
- Stewart Gordon (4/9) Mar 12 2012 Why can't this be dealt with using an extern (C) wrapper function in the...
- Sean Kelly (6/16) Mar 12 2012 code?
- Steven Schveighoffer (6/17) Mar 12 2012 All it does is call the extern(C) rt_init. It was my bad, if you are in...
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (5/51) Mar 12 2012 But that's effectively an extern (D) function. That's why I linked to
- Chris W. (4/74) Mar 12 2012 It's fine, no need to call gc_init() or thread_attachThis() once
I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7
Mar 12 2012
On 12-03-2012 15:53, Chris W. wrote:I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7Are you remembering to initialize the runtime, attach your thread, etc... Also, you can't call D functions directly from C code. You have to go through an extern (C) wrapper that then calls the D function. -- - Alex
Mar 12 2012
On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
Mar 12 2012
On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:Yes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. But anything more sophisticated renders a Bus error. I am sure it is just some little detail I have forgotten.I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
Mar 12 2012
On 12-03-2012 16:09, Chris W. wrote:On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:Don't forget to call this: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33 Documented here: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101 -- - AlexOn Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:Yes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. But anything more sophisticated renders a Bus error. I am sure it is just some little detail I have forgotten.I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
Mar 12 2012
On Monday, 12 March 2012 at 15:17:32 UTC, Alex Rønne Petersen wrote:On 12-03-2012 16:09, Chris W. wrote:Thanks a million, calling rt_init(); in my C code did the trick. Now I can perform string operations etc. I knew it was just a tiny little detail.On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:Don't forget to call this: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33 Documented here: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:Yes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. But anything more sophisticated renders a Bus error. I am sure it is just some little detail I have forgotten.I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
Mar 12 2012
On Mon, 12 Mar 2012 11:17:31 -0400, Alex R=C3=B8nne Petersen = <xtzgzorex gmail.com> wrote:On 12-03-2012 16:09, Chris W. wrote:On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:=On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 =3D s.dup; (s is a char* passed from C). Any hint or help=utYes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. B=would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Steveanything more sophisticated renders a Bus error. I am sure it is just=some little detail I have forgotten.Don't forget to call this: =https://github.com/D-Programming-Language/druntime/blob/master/src/cor=e/runtime.d#L33Documented here: =https://github.com/D-Programming-Language/druntime/blob/master/src/cor=e/runtime.d#L101 More appropriate: http://dlang.org/phobos/core_runtime.html#initialize And actually, I think this should do everything necessary. No need to = call gc_init and thread_attachThis(). -Steve
Mar 12 2012
On Mon, 12 Mar 2012 11:36:45 -0400, Steven Schveighoffer = <schveiguy yahoo.com> wrote:On Mon, 12 Mar 2012 11:17:31 -0400, Alex R=C3=B8nne Petersen =<xtzgzorex gmail.com> wrote::On 12-03-2012 16:09, Chris W. wrote:On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote=On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:=pI have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 =3D s.dup; (s is a char* passed from C). Any hint or hel=ButYes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. =would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -Stevetanything more sophisticated renders a Bus error. I am sure it is jus=some little detail I have forgotten.Don't forget to call this: =re/runtime.d#L33https://github.com/D-Programming-Language/druntime/blob/master/src/co=Documented here: =re/runtime.d#L101https://github.com/D-Programming-Language/druntime/blob/master/src/co=More appropriate: http://dlang.org/phobos/core_runtime.html#initialize And actually, I think this should do everything necessary. No need to==call gc_init and thread_attachThis().Hm... just realized you can't do this, since it's a D function :D But yeah, all it does is call rt_init, so you should be good. -Steve
Mar 12 2012
On 12/03/2012 15:38, Steven Schveighoffer wrote: <snip>Why can't this be dealt with using an extern (C) wrapper function in the D code? Stewart.http://dlang.org/phobos/core_runtime.html#initialize And actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis().Hm... just realized you can't do this, since it's a D function :D
Mar 12 2012
On Mar 12, 2012, at 9:54 AM, Stewart Gordon <smjg_1998 yahoo.com> wrote:On 12/03/2012 15:38, Steven Schveighoffer wrote: <snip>ll gc_init andhttp://dlang.org/phobos/core_runtime.html#initialize =20 And actually, I think this should do everything necessary. No need to ca=code? D function names are mangled based on the name of the module they're defined= in. For the runtime code, it's easier to wrap extern C calls with D functio= ns.=20==20 Why can't this be dealt with using an extern (C) wrapper function in the D=thread_attachThis().=20 Hm... just realized you can't do this, since it's a D function :D
Mar 12 2012
On Mon, 12 Mar 2012 12:54:25 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:On 12/03/2012 15:38, Steven Schveighoffer wrote: <snip>All it does is call the extern(C) rt_init. It was my bad, if you are in D-land, it's definitely better to use Runtime.initialize, but from C, the best bet is rt_init. -SteveWhy can't this be dealt with using an extern (C) wrapper function in the D code?http://dlang.org/phobos/core_runtime.html#initialize And actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis().Hm... just realized you can't do this, since it's a D function :D
Mar 12 2012
On 12-03-2012 16:36, Steven Schveighoffer wrote:On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen <xtzgzorex gmail.com> wrote:But that's effectively an extern (D) function. That's why I linked to rt_init.On 12-03-2012 16:09, Chris W. wrote:More appropriate: http://dlang.org/phobos/core_runtime.html#initializeOn Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:Don't forget to call this: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33 Documented here: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:Yes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. But anything more sophisticated renders a Bus error. I am sure it is just some little detail I have forgotten.I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -SteveAnd actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis(). -Steve-- - Alex
Mar 12 2012
On Monday, 12 March 2012 at 15:39:15 UTC, Alex Rønne Petersen wrote:On 12-03-2012 16:36, Steven Schveighoffer wrote:It's fine, no need to call gc_init() or thread_attachThis() once the runtime is initialized. Thanks guys.On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen <xtzgzorex gmail.com> wrote:But that's effectively an extern (D) function. That's why I linked to rt_init.On 12-03-2012 16:09, Chris W. wrote:More appropriate: http://dlang.org/phobos/core_runtime.html#initializeOn Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:Don't forget to call this: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33 Documented here: https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec cd.ie> wrote:Yes, I am using extern (C) and in my C main function I call gc_init(); thread_attachThis(); This works fine for primitive types such as int + int calculations. But anything more sophisticated renders a Bus error. I am sure it is just some little detail I have forgotten.I have a problem when calling D functions from C. While I can perform simple arithmetic operations (i.e. the calculation is performed in D and returned to C), I experience problems when trying to perform string/char operations or call functions from the D standard library (e.g. writefln()). The usual error message I get is either "Bus error" or "Segmentation fault". I haven't been able to find the reason for this. The programs compile and link, however, when run, they terminate with "Bus error" whenever a D function is performed within the D code, e.g. something like char[] s2 = s.dup; (s is a char* passed from C). Any hint or help would be appreciated. I am using Mac OS X, 10.6.7If C is running your application startup, you must initialize D's runtime from your C main routine. -SteveAnd actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis(). -Steve
Mar 12 2012