Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile C library as iPhone framework?

Each C/C++ library has some amount of headers that should be used with that library. And if you're use more than 1-2 libraries, custom header paths is kind of headache.

So I thought: is there a way to compile C libraries as frameworks. Static library + headers + versioning.

I found in XCode template for Cocoa framework but nothing about iPhone framework building. This simple step could allow developers to build and share between each other frameworks with some interesting code.

Also it would be great to use libpng, libjpeg and other libraries packaged as frameworks.

I won't use dynamic libraries in those frameworks. Only static version will be present.

like image 651
Evgen Bodunov Avatar asked Mar 17 '11 15:03

Evgen Bodunov


People also ask

Can you run C code on iOS?

XCode is compatible with C, C++ and Objective C as well as Swift. Objective C is based on C. You can execute any C program in XCode as long as it does not have any platform specific dependencies that would prevent it from running on an Apple device / computer.

How can I make my own iOS app framework?

Create your iOS FrameworkFramework under Framework & Library section. Give a name to your framework (select Include Unit Tests — optional), click Next and you should set your workspace as you add to and group options. Then click Create. Framework's creation.

Can Swift compile C?

Swift cannot compile C code because the syntax is not compatible. Swift can interoperate wit C code (see Interacting with C APIs article for more information). However, C code behind the APIs needs to be compiled separately, using a C compiler.

Is XCFramework static or dynamic?

An XCFramework can be either static or dynamic and can include headers. (for more information, see link).


3 Answers

Frameworks are basically just bundles of dynamic/shared libraries. Since this is not allowed in the App Store, you have to build static libraries and link them with your App's executable at compile time.

However, to ease the pain a little, you can have a Xcode project for each library and compile each library into a static lib. Another way would be to put all required source files into the main Xcode project and configure it appropriately so it all builds at once (I did this with small libraries like Minizip, for instance).

Hope that helps.

like image 187
BastiBen Avatar answered Oct 21 '22 12:10

BastiBen


I combined techniques I found here and here into my open source library here (NOTE - I have since removed the relevant scripts from this project. I have updated the link to point to the last revision that included these techniques.). Think that's basically the solution you're looking for.

Basically, it has a target that will build a fat static library (lipo builds for arm6, arm7 and i386). It has another target that takes that library and builds it into a framework.

There's no reason you can't use the same techniques for a C project. In fact I've started work on porting C the VTD XML parser to an Objective C framework using the same techniques.

like image 21
DougW Avatar answered Oct 21 '22 13:10

DougW


the problem you are trying to make already exists - it's called DLL hell

Best way is to stick with plain old static libraries when making small apps and organizing source/headers structure

like image 33
fazo Avatar answered Oct 21 '22 13:10

fazo