Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips on wrapping a C library in Objective-C

I have a library written in C that I would like to use in an Objective-C app, either on the Mac or the iPhone.

Unfortunately, since this library is being written by individuals in the open source space, the documentation is quite sparse and incomplete. While I can figure out how to use the stuff in the library, I don't really have an overview of the entire code base.

What I would like to do is wrap the library up into some easily usable and transferrable classes in Objective-C.

  • Does anyone have any tips on how to approach this?
  • Any advice on the best way to get a visual hierarchy of how the library is structured?
  • How would I go about deciding how to best structure the wrapper for reusability and ease of use?

Any and all help will be greatly appreciated, thanks!

like image 469
Jasarien Avatar asked Aug 23 '09 23:08

Jasarien


1 Answers

I've done this a few times myself. This can be fun -- it's your chance to fix (or at least hide) bad code!

You can use Doxygen to get a visual hierarchy of the code (although I've only used it for C++ libraries, it also works with C), or any of the other free tools out there.

Don't structure your wrapper class like the underlying library if the library isn't designed or documented well. This is your chance to consider the point of view of the user and how they are going to be using the code. Write your test cases first to figure that out, and/or talk to some people who use the library already.

Two nice design patterns that match up with what you're doing are Adapter and Facade.

like image 128
Shaggy Frog Avatar answered Oct 04 '22 22:10

Shaggy Frog