Linking D and Obj-C code into a Cocoa app proper? (Mac)

Heywood Floyd soul8o8 at
Tue Oct 5 07:02:45 PDT 2010

Good Evenening Ladies and Gentlemen!

== Background ==
So I currently have a bare-bones Cocoa-app. It's just a window with an OpenGL-view. In the view's draw-function I make the gl-view the current OpenGL context and then call "extern (C) render()". Meanwhile, in a D-file I have the implementation for this function. Here I've copied all the function names from opengl.h and declared them in the D-file ("extern (C) glBlabla()" etc) so I can call glBegin etc from my render()-function in D. This is all in XCode and I use Michel Fortin's "D for XCode"-plugin btw.

This works great! The window paint triangles and what have you. Pretty! Happiness!

But, sometimes I get reeeaally weird bugs. I had one bug where if I added an empty function to a class in D I got EXC_BAD_ACCES (segfault). An empty function! Ie "void f(){}". Remove the function--it works. In the debugger, I got the impression maybe the stack has got messed up, but I don't know, the debugger just shows assembler code, and I don't have the proper skills.

This got really frustrating, needless to say, so I started playing around with the build settings. I switched from using LLVM 1.5 (for the obj-c code) to gcc 4.2. And now it magically seems to work!

Ok. But as you might understand, the frustration is not quite gone. Adding a function now feels like a spinning a wheel of fortune. It's utterly demoralizing. So I thought about this and realized, I probably should try to find out what it is I'm actually doing here, and hear with some real programmers if there's a "proper" way of doing it. So my question, dear D community:

== Question ==
How do you make D code and Obj-C code coexist? That is, I want to write a Cocoa-app that is mostly written in D, and with a little "glue"-code in Objective-C. How do you set that up? Is it even supposed to be possible?

(And what could the bug above be? I know LLVM does link-time optimizations, and even run-time optimizations. Could it be that it messes things up?)


More information about the Digitalmars-d-learn mailing list