• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Integrating a C command line tool into Objective-C application
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Integrating a C command line tool into Objective-C application


  • Subject: Re: Integrating a C command line tool into Objective-C application
  • From: Uli Kusterer <email@hidden>
  • Date: Sat, 8 Sep 2007 11:15:35 +0200

On 08.09.2007, at 09:08, Jason Wiggins wrote:
Now my question. I've read through the documentation and also done google searches but I am yet to find *exactly* what I'm looking for to help me. A colleague wrote a command line tool in C a while back and now I want to integrate that tool into my obj-c code. I'm not sure how to go about this. I started coding on obj-c about a year ago on and off and haven't coded in C before, but looking through some books I have a general idea in that area. The problem is the integration, ie the Main function etc. Do I make the C code a new class and strip out the main function? I tried using that code verbatim (minus Main) and tried to turn it all into a method but the compiler said I can't have nested functions. If anybody can help in this area, I would be most appreciative.

I hope I'm not explaining too basic things to you, but I'm just trying to cover all places where you could be hung up:


A function in C is essentially the same as a function in ObjC, e.g. NSLog() is a function. Some C compilers let you put one function *definition* (1) inside another, which is what is called "nested functions". This is a nonstandard extension that e.g. GCC 3.3 would let you do it. GCC 4.x doesn't let you do nested functions anymore (2).

Methods are very similar to functions. Essentially, they are functions associated with a class of object. If you try to put a function inside a method, you'll probably get the same error message as when you try to put one function inside the other.

Something like this must be going on in the code you created based on your colleague's code. Either you copy-pasted a group of functions into one of your methods, or your colleague used nested functions.

There are two approaches I usually take when converting other people's C code to ObjC:

1) Take the whole C code and just write a function that effectively contains the code from the "main" function, but with nicer parameters, and then call that C function wherever I need.

2) If I want to replace some code, or the code calls back to my code (in C this is usually done by passing a pointer to one function to another), I just turn the whole package into one class, and make every function a method. The callback functions get turned into methods that simply call through to a delegate I add to instances of this class.

Of course, if I have to keep the original C code and my Objective C code synched, I usually take a hybrid approach, where I generally just do #1, but create an ObjC class that calls the function that was based on the "main" function, which installs callback functions that just call through to a delegate.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de

1) A function definition is essentially the code that makes up the actual function. This is different from a function call, which is what most function definitions use to actually do their work.

2) The way GCC implemented nested functions was taking advantage of a security gap in the OS, which Apple intends to plug, so they removed this feature so people can prepare. Nested functions are an extension, not part of ANSI standard C or ISO C, so the complaints weren't too loud.



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Integrating a C command line tool into Objective-C application
      • From: Jason Wiggins <email@hidden>
References: 
 >Integrating a C command line tool into Objective-C application (From: Jason Wiggins <email@hidden>)

  • Prev by Date: Re: Integrating a C command line tool into Objective-C application
  • Next by Date: Re: RS: NSObject members class and isMemberOfClass
  • Previous by thread: Re: Integrating a C command line tool into Objective-C application
  • Next by thread: Re: Integrating a C command line tool into Objective-C application
  • Index(es):
    • Date
    • Thread