Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Xeikon

macrumors newbie
Original poster
Oct 12, 2011
2
0
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?
 
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).
 
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.
 
Put this line in the file that contains your command-line main() function.
Code:
#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.
 
Put this line in the file that contains your command-line main() function.
Code:
#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.

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.
 
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.