Using a .c file in objective c code

Apprentice
Posts: 7
Joined: 2009.01
Post: #1
All,

I'm working on some basic samples to get myself familiar with game programming, and I'm having problems using a '.c' file with objective-c code.

In order to start to make my game more reusable, I want to ideally take all of the opengl code and put it into C files. This way, the core openGL code for my game can be taken and recompilied on other systems with few dependencies to Mac OS X or Cocoa.

But when I try to do this, I run in to a lot of problems. My first simple attempt worked: I created a test.c file, put it into the 'Classes' directory, added a simple c method, added the #import in the AppController.m objective-c class, and was able to compile and run.

Then, I added a more complex c method with OpenGL calls, and suddenly I was getting a "no file test.c" compiler error from AppController.m. But all I had done was add some additional code to test.c!!

So then I removed the code I had added (making the files look exactly as they had when the test actually worked) and I still got the error! So I tried deleting everything and starting over, and now I can't get the test to work no matter what I do!

Has anyone done what I'm attempting before? Is there something special I need to do to use ".c" files with Objective-C?
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
If I'm reading your post correctly, it sounds like you're trying to #import a .c file. While there's nothing stopping you from doing this, it's generally inadvisable. The usual way is to compile each implementation file as an isolated unit, producing an object (.o) file for each, then letting ld stitch them together when you want to build your executable. This means that you only have to recompile the ones that changed since the last compilation, and with good header management, inter-file dependencies are easier.

With all that said, I can't think of any obvious reason you'd be getting a file not found error when trying to import test.c, assuming it worked previously and you didn't move files around or change include path settings in the compiler. Check the command line Xcode (or whatever you're building with) is using to invoke gcc, and make sure your files are in the right places and/or the correct -I flags are passed so that they can find each other.

To answer your other question, there isn't generally anything special you need to do to get the two languages to work with each other. Objective-C is a proper superset of C, so any calls from Objective-C to C will work with no additional effort. Calling from C to Objective-C will require a bit of abstraction so that you can use plain function calls rather than Objective-C messages, of course.
Quote this message in a reply
Post Reply