Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating static framework for OS X

Tags:

xcode

macos

With the use of a mild hack, it is possible to make static frameworks for iOS. Static frameworks are quite convenient to use: they can simply be dropped into new projects without extra steps (like adding them to the build and adding header search paths).

I've recently started doing OS X programming, and the first thing I noticed was that static frameworks don't seem to be available. Dynamic frameworks are obviously available and recommended, but as I want to make a little private framework intended for application use (not installation in /Library/Frameworks), using a dynamic framework in new application projects still requires a bunch of extra steps.

In my ideal world, I'd create a static framework (a framework which contains header files and a compiled .a file), drag & drop the framework onto a new project, and start coding. Is there any way to make such a static framework on OS X?

P.S. I already tried setting the Mach-O output type to "static library" in a normal framework project, but I just get the error Framework target has invalid MACH_O_TYPE value of 'staticlib'..

like image 770
nneonneo Avatar asked May 10 '13 18:05

nneonneo


2 Answers

You can create a dynamic framework on Mac OS X. In your dynamic framework you can set the LD_DYLIB_INSTALL_NAME as @rpath/Foo.framework/Versions/A/Foo

If you have an app that wants to link with this framework then you make sure you run the

  install_name_tool -add_rpath <rpath> <full-path-to-app-binary>

So if I had Foo.app

  install_name_tool -add_rpath Foo.app/Contents/Library Foo.app/Contents/MacOS/Foo 

Now if you just copy your Foo.framework into Contents/Library it should get loaded and everything should work.

I hope this helps.

Probably simpler would be to use a static library with public headers. When you build the static lib you can have Xcode copy the headers for you automatically. And in your target you can add the folder to your search path.

If you use a static library Xcode will strip away some dead code that your app doesn't really need but is compiled into the static lib.

like image 188
dimitrirostavo Avatar answered Sep 22 '22 07:09

dimitrirostavo


Static frameworks aren't really supported on OS X. They're fairly brittle anyway, and solve a specific problem that exists on iOS but not on OS X.

If you're looking to make it easy for developers to use a library you create, you have a couple options:

  1. Use Cocoapods. They have a tutorial for publishing your library on CocoaPods. This is probably the easiest way to distribute a library on OS X.
  2. Package your library as a framework. If you set the install name correctly (to @rpath/<library name>), the downstream developer merely needs to copy the framework into their Xcode project and set the runtime search path of their application to @executable_path/../Frameworks).
like image 42
mipadi Avatar answered Sep 19 '22 07:09

mipadi