Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn
On Sunday, 3 January 2016 at 21:20:35 UTC, anonymous wrote: On 03.01.2016 21:32, TheDGuy wrote: If i type: gcc -c -otest.c.o the 'test.c.o' file is generated but if i type: dmd main.d test.c.o i get: 'Error: unrecognized file extension o'? You're probably on Windows then? dmd doesn't

Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn
Use an import. import std.string; import std.conv; void main(string[] args) { auto value = toStringz("Hello World"); auto result = write(value); auto s = to!(string)(result); writeln(s); } Also all string literals in D are zero terminated so you could write the call

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 19:24:46 UTC, TheDGuy wrote: On Sunday, 3 January 2016 at 13:25:04 UTC, Gary Willoughby wrote: On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote: I think I've noticed one problem with the code above. You are using `text.ptr`. You shouldn't do that

Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn
On 03.01.2016 21:32, TheDGuy wrote: If i type: gcc -c -otest.c.o the 'test.c.o' file is generated but if i type: dmd main.d test.c.o i get: 'Error: unrecognized file extension o'? You're probably on Windows then? dmd doesn't recognize the .o extension on Windows, use .obj instead.

Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn
On 03.01.2016 22:37, TheDGuy wrote: If i rename "test.o" to "test.obj" i get: 'Error: Module or Dictionary corrupt' My guess is, that means that dmd can't handle the object file format that gcc produces. Disclaimer: I don't know much about object formats, gcc, and Windows. I may be

Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn
On 03.01.2016 14:12, anonymous wrote: You shouldn't get a segfault, though. You should get some compile/link error. Are you compiling the right files? Can the segfault be from something other than your program? Oh, I see what's probably happening: You compile the D program, but you don't

Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn
I get an access violation with this code: extern(C) char* write(char* text); void main(string[] args){ char[] text = "Hello World".dup; //.dup converts string to char[] text ~= '\0'; //append char* result = write(text.ptr); //you need .ptr const(char)[] s =

Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn
Works for me after adding the needed imports and removing the wrong include from the C file. Is this really the actual code you're running? Doesn't your C compiler reject that include? gcc does. Okay, i think this C code should work (checked with cpp.sh): #import char* write(char* text){

Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn
On 03.01.2016 14:01, TheDGuy wrote: Okay, i think this C code should work (checked with cpp.sh): #import char* write(char* text){ return text; } int main(void){ return 0; } Uh, no. 1) In C it's include, not import. 2) Now you have two main functions, that can't work. You

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 12:30:34 UTC, TheDGuy wrote: I get an access violation with this code: ... There are a few things you can do to improve your code to make it easier to debug. 1. When converting a D string to a char pointer for use with C, use `std.string.toStringz`:

Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn
On 03.01.2016 13:30, TheDGuy wrote: I get an access violation with this code: extern(C) char* write(char* text); void main(string[] args){ char[] text = "Hello World".dup; //.dup converts string to char[] text ~= '\0'; //append char* result = write(text.ptr); //you need .ptr

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote: I think I've noticed one problem with the code above. You are using `text.ptr`. You shouldn't do that because you are passing a pointer not an array. Just use `text`. Forget this line, my mistake. Use `toStringz` and pass a

Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn
On Sunday, 3 January 2016 at 13:25:04 UTC, Gary Willoughby wrote: On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote: I think I've noticed one problem with the code above. You are using `text.ptr`. You shouldn't do that because you are passing a pointer not an array. Just use