PDA

View Full Version : How to use objective C objects in C files




arunan
Feb 17, 2009, 05:04 AM
I have most of the code in C, C++ in my application . I would want to build the GUI and render the video using the QTkit .
Is it possible to build a objective C dynamic library in cocoa using xcode and call them from the C files ? I am using make files to compile the C code .
Please provide me the details as to how to do it if it is possible.A simple example would be of great help as I am new to MAC .
:confused:



gnasher729
Feb 17, 2009, 07:20 AM
I have most of the code in C, C++ in my application . I would want to build the GUI and render the video using the QTkit .
Is it possible to build a objective C dynamic library in cocoa using xcode and call them from the C files ? I am using make files to compile the C code .
Please provide me the details as to how to do it if it is possible.A simple example would be of great help as I am new to MAC .
:confused:

You can add any C functions you like to a .m file and compile them, then call them as you would call any other C functions.

arunan
Feb 18, 2009, 09:17 PM
May be I have n't put my problem clearly !!
I want to build a dynamic library in cocoa that implements the video capture using QTKit and initiate the capture from C code . Would finally compile the application C code using make files .

Catfish_Man
Feb 19, 2009, 05:13 AM
Rename a few of your C files to .m and put Objective-C code there to talk to QTKit. You can use makefiles with ObjC just fine.

kpua
Feb 19, 2009, 10:59 AM
You can export C functions from Objective-C files just like you normally would. Here's an example:

MyFunc.m:

void doSomething() {
[someObjCObject someMethod];
}


and then in your C file, you use it like this:

extern void doSomething();

void anotherFunction() {
doSomething();
}



Both files are compiled separately into object files, after which, the linker doesn't care what language they were written in. The extern'd doSomething() declaration in the C file will be linked to the doSomething() definition in the .m file.

kainjow
Feb 19, 2009, 11:09 AM
Don't forget to setup an NSAutoreleasePool also.

arunan
Feb 22, 2009, 11:47 PM
Thanks a lot for all your valuable inputs . I could succesfully compile creating a C wrapper function in a .m file and having the objective code in that.