How to use objective C objects in C files

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

  1. macrumors newbie

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

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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

    Joined:
    Feb 17, 2009
    #3
    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

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    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

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

    MyFunc.m:
    Code:
    void doSomething() {
        [someObjCObject someMethod];
    }
    
    and then in your C file, you use it like this:
    Code:
    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.
     
  6. Moderator emeritus

    kainjow

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

    Joined:
    Feb 17, 2009
    #7
    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