Tips on wrapping a C library in Objective-C - 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!

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.

First, remember: a C library is an Objective-C library. You don't actually need to do any wrapping at all, although you may want to if the library interface is especially cumbersome.
Second, if you decide that you want to write a library wrapper, keep it simple. Identify the core functions of the library that you actually plan to use, and think about how best to provide an interface to those functions and those functions only, with your intended usage in mind. Design an interface that you want to work with, then implement it over the library.

Since ARC (Automatic Reference Counting) was added to the Apple compilers and libraries, Objective-C and C are no longer so freely interchangeable. (Here's a list of ARC documentation and tutorials.) You need to consider the memory allocation issues much more thoroughly, and you might just want to "bridge" the libraries. See this SO question and some of the links from there, about how Apple bridges between Obj-C and C libraries.

Related

Is it practical to use #ifdef's to compile a library without certain features?

So I'm currently working with a proprietary programming language that is C-like. So while this question wasn't directly inspired by a C program, I think those of you who are familiar with C may be able to offer some good insight.
I'm currently working on a library. This library encompasses some basic features as well as features that require other libraries. I'm running into the problem of 'where do I draw the line for how many dependencies are included in this library?'.
So this seems like it could be a fairly common problem. What methods exist for addressing this issue?
Something I've had in mind. Implement #defines and #ifdefs that allow users to compile the library with only the features they want. So essentially, all of the library functions that require additional libraries would be wrapped in #ifdef guards. The user would be responsible for #define'ing the features they want. Essentially, this method would allow a user to still use parts of the library without needing to have other dependent libraries.
Your thoughts? Again, this is for a C-like language. Thus tools like CMake, etc. aren't available.
Yes. If you take a look at the Linux kernel for instance, it's done the exact same way.
Yes you can and You Should.
I use them whenever i feel the necessity. Also, i have been working on freeRTOS and that's how things are being done over there.

.NET 4.5 Portable class libraries : Is it possible to determine the platform at runtime?

I've got a set of libraries that target Silverlight & Windows Phone 7.5, In my libraries I have a navigation solution that has a common interface but different implementations - depending on the platform. (SL & WP7 Navigation are very similar, there are very minor differences, unfortunately they can't be avoided)
If I were to write a portable class library that would contain my INavigationModel interface + both implementations, is there any way to tell which platform is calling the portable class library? This information would help me choose the correct implementation of my solution at runtime.
Cheers
edit
Any alternative solution is welcome too, I'm still trying to piece any information about the portable class libraries to see what they're capable of
Realistically, that may not work so well. Even if you could reliably determine the environment you're running under (some kind of hack with Environment.OperatingSystem perhaps), if your implementation is of any complexity, it's unlikely you could stick completely to the lowest-common-denominator of defined types/methods, etc.
The whole point of portable assemblies is to define truly shared bits (utility methods, interfaces, etc)- probably a better solution might be to define base classes/interfaces with shared functionality in the portable assembly, then extend in your platform-specific assemblies to implement the non-portable bits.
There's not a good way to do what you want to do. Portable libraries do not remove the need to have platform-specific projects, it just helps facilitate platform-specific projects to talk in a platform-agnostic way.
I would tend to recommend that the concrete implementation of the INavigationService live in the platform specific projects themselves, whereas the common abstraction live in the portable project. Then use an IoC container or ServiceLocator-like framework to hook them up (I'd recommend Autofac). I've done similar in an article (in fact, I did this for a navigation service) I wrote for MSDN: Create a Continuous Client Using Portable Class Libraries. Be sure to download the source code to see a concrete example.

Template based C / C++ code generation

Any suggestion for template base code generator for C / C++ specifically to generate repetitive code generation? (Not UML / MATLAB model based or other advanced stuff). For a newbie in this field any good generic tutorial (not tool based)?
I came across GNU Autogen looks good but looks like it needs a steep learning curve. I would prefer some plug-in for eclipse like IDE, easy to use and most importantly good tutorials.
The basic concept of code generation is simple enough - and people's needs are varied enough - that there are quite a few options out there.
Boost.Preprocessor is a library of functions built on top of the standard C / C++ preprocessor that makes it much easier to use the preprocessor to do code generation. It's not as flexible as other options, and figuring out preprocessor errors can be tricky, but the fact that it uses only standard language features greatly simplifies using it and integrating it into your builds.
If you know Python, there's Cog.
Some of Google's projects use Pump.
There are many general-purpose templating solutions (Python's Genshi, eRuby, etc.). These are often designed for generating HTML and XML but also work for code.
It's also easy enough to hack something together in the scripting language of your choice.
Without knowing more about what your needs are and what tools you're comfortable with, I can't give a more specific recommendation.
I'm not familiar with anything that provides an Eclipse plugin.
If you know Python, then Cog could be considered as light-weight solution: http://www.python.org/about/success/cog/
Look at my answer for a similar question for Java classes using M2T-JET, an eclipse based, lightweight templating generator. JET is language agnostic and you can see from the example that it's fairly easy to use.
I appreciate using Lua for this task, with something like Templet or one of another myriad of Lua-based preprocessors. The benefit of using Lua over something like Python is that you can, if necessary, include the source code to your template processor and a basic Lua installation along with whatever it is you are shipping. You may then add the compilation of Lua and subsequent template files to the build process as usual.
I would advise not using Python-based solutions for one reason: juggling various pythons to satisfy every developer's use of a completely different yet incompatible version is annoying. If you choose to use a language which you can't embed in your trees, you'll want to make sure pre-computed versions are available.
Late to the party but I would recommend Codeworker Its the only tool I found that does everything the above tools do and more. It has the Python Cog like functionality for embedded generation, it has the template based generation like Templet or Pump. And it has the rather useful feature of protected areas so you can customise your code as needed and re-generate.
I have used it for generating all the boiler plate c++ code as well as configuration for projects like SQL, config, javascript etc.

pure c support/util library

I am searching a library in C/pragma/.. for basic programming tasks.
Something for handling and creating Lists and hasmaps and arrays and souch stuff.
So i dont have to reinvent the wheel again and again and write the same structures again and again.
But has to be pure C library.
Thanks for any help.
You might want to retag your question and remove the C++ tag, as your question is a little strange with the tag.
If you need something implemented in C, then look at Glib, which is part of GTK+, and it implements data structures like linked lists and trees.
Alternatively, the Apache Portable Runtime is a project from Apache which is also written in C, and is used in the Apache web server.
In plain c projects I tend to use the APR.
Maybe it covers all the things you need. And it provides a nice abstraction of the OS too.
Glib
In addition to the other answers, you might take a look at the source code accompanying the book C Interfaces and Implentations.

Bootstrapping a language on LLVM

I'm bootstrapping a programming language compiler on top of LLVM. Currently I'm mostly done writing a compiler for a subset of C which is self-compiling. When I'm finished with that, I'll bootstrap my language away from C, maintaining self-compilation as I go.
Since the compiler is self-compiling, any features of C that I use I will have to implement. So it's a constant balance: if I use too many features I will have to implement more than I want to, but if I don't implement enough features it will be difficult to write code.
One such feature is the LLVM bindings. Generating LLVM intermediate representation without the LLVM C bindings is difficult. However, if I us the LLVM bindings, I have to implement them again when I branch away from C.
I'm having some difficulty here, so I a looking for alternative solutions. Any ideas?
You could use the LLVM C bindings, but that requires your language understand enough C to do that.
Another alternative is to write out LLVM assembly language (a text file) and use llvm-as to turn that into bitcode.
Edit:
I re-read you question, I think you already understand the llvm-as vs. binding stuff.
Your language will probably want to be able to bind to C anyway for support libraries, etc. Use the C bindings for now and write your own bindings when you get further along.
A Strategy for using ANTLR + StringTemplate + LLVM
HTH
At some point, you're probably going to want to provide an API for wrapping C libraries as extension modules. LLVM may already support this (I know the parrot vm does). Why not use whatever extension system you use to wrap LLVM's own API? They may already support that, too. :)

Resources