How to use objective C objects in C files

Discussion in 'Mac Programming' started by arunan, Feb 17, 2009.

  1. macrumors newbie

    Feb 17, 2009
    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 .
  2. macrumors G5


    Nov 25, 2005
    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.
  3. thread starter macrumors newbie

    Feb 17, 2009
    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 .
  4. macrumors 68030


    Sep 13, 2001
    Portland, OR
    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.
  5. macrumors 6502

    Jul 25, 2006
    You can export C functions from Objective-C files just like you normally would. Here's an example:

    void doSomething() {
        [someObjCObject someMethod];
    and then in your C file, you use it like this:
    extern void doSomething();
    void anotherFunction() {

    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.
  6. Moderator emeritus


    Jun 15, 2000
    Don't forget to setup an NSAutoreleasePool also.
  7. thread starter macrumors newbie

    Feb 17, 2009
    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.

Share This Page