digitalmars.D - Calling D from C
- "Chris W." <wendlec cd.ie> Mar 12 2012
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> Mar 12 2012
- "Steven Schveighoffer" <schveiguy yahoo.com> Mar 12 2012
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> Mar 12 2012
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> Mar 12 2012
- Stewart Gordon <smjg_1998 yahoo.com> Mar 12 2012
- "Chris W." <wendlec tcd.ie> Mar 12 2012
- "Chris W." <wendlec tcd.ie> Mar 12 2012
- "Steven Schveighoffer" <schveiguy yahoo.com> Mar 12 2012
- "Steven Schveighoffer" <schveiguy yahoo.com> Mar 12 2012
- "Chris W." <wendlec tcd.ie> Mar 12 2012
- Sean Kelly <sean invisibleduck.org> Mar 12 2012
- "Steven Schveighoffer" <schveiguy yahoo.com> Mar 12 2012
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.7
Are 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.7
If 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: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.7
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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.
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 -- - Alex
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: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 = 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
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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.
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
More appropriate: http://dlang.org/phobos/core_runtime.html#initialize
But that's effectively an extern (D) function. That's why I linked to rt_init.And actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis(). -Steve
-- - Alex
Mar 12 2012
On 12/03/2012 15:38, Steven Schveighoffer wrote: <snip>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
Why can't this be dealt with using an extern (C) wrapper function in the D code? Stewart.
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: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
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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.
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: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 = 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
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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.
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
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.
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=
would be appreciated. I am using Mac OS X, 10.6.7
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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. B=
anything 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=
Documented here: =
https://github.com/D-Programming-Language/druntime/blob/master/src/cor=
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:=
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 hel=
would be appreciated. I am using Mac OS X, 10.6.7
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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. =
anything more sophisticated renders a Bus error. I am sure it is jus=
some little detail I have forgotten.
Don't forget to call this: =
https://github.com/D-Programming-Language/druntime/blob/master/src/co=
Documented here: =
https://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 Monday, 12 March 2012 at 15:39:15 UTC, Alex Rønne Petersen wrote: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: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 = 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
If C is running your application startup, you must initialize D's runtime from your C main routine. -Steve
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.
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
More appropriate: http://dlang.org/phobos/core_runtime.html#initialize
But that's effectively an extern (D) function. That's why I linked to rt_init.And actually, I think this should do everything necessary. No need to call gc_init and thread_attachThis(). -Steve
It's fine, no need to call gc_init() or thread_attachThis() once the runtime is initialized. Thanks guys.
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>http://dlang.org/phobos/core_runtime.html#initialize =20 And actually, I think this should do everything necessary. No need to ca=
thread_attachThis().
Hm... just realized you can't do this, since it's a D function :D
Why can't this be dealt with using an extern (C) wrapper function in the D=
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=
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>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
Why can't this be dealt with using an extern (C) wrapper function in the D code?
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. -Steve
Mar 12 2012









=?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> 