add working C code to Xcode cocoa project

Discussion in 'Mac Programming' started by Xeikon, Oct 12, 2011.

  1. Xeikon macrumors newbie

    Oct 12, 2011
    I have a working C program that reads a usb/serial converter to gather input from a serial device.

    I now want to create a cocoa project to include this code. How?

    I realise that as my working code has a 'main' then I will get compile errors but how do I get around this?
  2. Sydde macrumors 68020


    Aug 17, 2009
    Redesign your main() as a function that your program can call directly ( ( argc, argv ) might not be the ideal way for your project to use the function ) and rename it. If you have written your program the best way possible, the whole main() function could be omitted from the source for your project (the working part of the program would be in other .h/.c files that could be imported as-is).
  3. subsonix macrumors 68040

    Feb 2, 2008
    Is your C program a command line program? GUI code is event driven, if you have divided your functionality into separate functions then you will have an easier job moving these inside classes and method implementations. Having a main in itself should not create a compile error as even Cocoa programs has a main (legal C is legal Obj-c) it just does not make sense to center you program around main.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Put this line in the file that contains your command-line main() function.
    #define main cmd_main
    Now there won't be two main() functions, which is illegal in both C and Objective-C. And you can call the command-line main as cmd_main(), if you wish.
  5. subsonix macrumors 68040

    Feb 2, 2008
    Why not just rename main() in the old C code then? I did not suggest he should have two mian() functions btw, just to make that clear. There will most likely be more modifications required to get it working though, specifically to tie in different functionality from the C code to views and UI controllers and so on.
  6. Xeikon thread starter macrumors newbie

    Oct 12, 2011
    Thanks for the input guys.

    I have now moved my code into classes and copied IOKit.framework into the project. Just a few compile errors to get past now.

    Maybe a step back for me is required. Rather than try to make my project work in cocoa straight off, maybe I should get a little more familiar with Xcode :)

Share This Page