Creating a library (.a) in Mac OS X and Xcode - xcode

I'm in the process of porting some code from Linux to Mac OS X. The code is a static library that you add to your code. Basically after you compile the code you get a MyLib.a that can be linked to whatever project you want.
I'm managing the "project" via a Makefile.
Is there a way to have a project on Xcode that ultimately produces the same kind of library? How?
Thank you for the help.

Sure. You don't say so, but I am assuming you are using C or C++ as source.
Assuming you are using Xcode 3.2.2, you can create a new project (File > New Project...), which will bring up a New Project window with available project templates. Select the "Framework & Library" category under Mac OSX from the list on the left. You should now see a "BSD C Library" and "STL C++ Library" among the other library and framework types. For building a static library like you described you want the "BSD C Library" option. There are static and dynamic options for this which you will see when you click on the icon, but static is default.
Depending on which version of Xcode you are using, the location of the project template may be a little different since Apple has been changing this UI for the New Project window fairly frequently. So if you are not using 3.2.2 you may need to poke around the available projects a little bit.
Oh, and don't worry if you are trying to build C++. Event though this says it is for C, C++ libs build just fine as well.

Related

Building a C static library using recent Xcode? Including that in Swift?

Does anyone have a guide to setting up a project that produces a static library under recent versions of Xcode? There are a few for older versions, but they use a project setting called "C/C++ Library" which seems to have been removed.
I notice "External build system" is still in there, and maybe that's a solution? The C code won't be changing structure too much, so a makefile could be a solution - but I've never used this type before, so I'd like to hear from people that have.
Ultimately the C code will be consumed by a Swift/Cocoa application. Am I correct in thinking that I simply include both projects in a workspace, drag the .a and bridging .h into the Swift project, and go?

How to compile swift library project into .so file?

I'm new to XCode and Objective C / Swift. I'm trying to make a Swift library project that doesn't depend on cocoa or anything. So it's just Swift files. What type of project should I be creating (I'm assuming there are multiple ways of doing this? How do I need to configure the project so that it builds to a .so file? And finally, how do I build it to a .so file?
I'm using Xcode6-beta.
You can create a Swift library for iOS by creating a new Xcode Project and selecting iOS/OS X -> Framework & Library -> Cocoa Touch Framework and then setting the language to Swift on the next screen.
Once this project has been created you are not required to link against the Cocoa frameworks, and can remove any references to them if needed.
Upon compiling this project Xcode will create a .framework folder in your build directory which contains a dynamic library which you should then be able to link against. Note however that I haven't had any success myself yet at linking directly against this file, even though it appears to be a standard dynamic library.
In terms of using this shared object file on Android I believe you will be out of luck, as the Swift language (much like Objective-C) requires a runtime library which is simply not available on other platforms at this time.
... I think you are limiting your train of thought a bit or expecting leaps and bounds to quickly swift is ready to work with objective c and cocoa so you aren't going to be able completely avoid cocoa however you wont have to write any of it the compiler will do that for you. but you will still have to use cocoa frameworks or modules like foundation( contains many specialized objects for program development), UIKit, CoreData however these don't require you to write the cocoa. and if Im not mistaken the .so file is made on compile so you never actually touch that file but rather is supporting files .swift
to do it
if your looking to do everything programmatically or if you want to xib or storyboards just make a new project I prefer an empty project.(set programming language to swift and decide if you want core data or not finish the project wizard thing and bam you have a YourAppDelegate.swift you can make another .swift file or a storyboard and hopefully you know what to do from there. Swift book is available on ibooks for free its nicely written
although it will be quite a while before cocoa and swift are completely separate... if ever

Creating static framework for OS X

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'..
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.
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:
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.
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).

Xcode 3/4 Compatibility when referencing static libraries

We have multiple development machines, some running Xcode 3.2.5 and others running Xcode 4.2. We have a static library that is referenced by our project. It contains Objective C categories, so we need to use the -force_load flag with the path to the library.
The problem that we are having is that on Xcode 4.2, the library is linked from a different location than the path that we specified in -force_load. This causes duplicate symbol errors since it's trying to link two copies of the same library.
The path Xcode 4.2 links from is:
/Users/USERNAME/Library/Developer/Xcode/DerivedData/PROJECTNAME-ehxeazvigqrpvogkxlkcmdzpisdw/Build/Products/CONFIGURATION-PLATFORM/
whereas the location that Xcode 3.2.5 links from is:
PROJECT_ROOT_OF_STATIC_LIBRARY/build/CONFIGURATION-PLATFORM/
Is there a way to get Xcode 4.2 to use the same locations as Xcode 3.2.5 when linking static libraries so that we can use the force_load linker option? If not, can anyone suggest an alternative solution to get Xcode 3.2.5 and 4.2 to play nicely together?
It sounds to me like you are doing things the hard way.
I've never used -force_load to load static libraries. With the libraries I use, they are all stored as frameworks in a directory on my system which is separate from the project. I then use the XCode standard add frameworks dialogs to bring them into the project and "-ObjC -all_load" to ensure they work and categories are active.
I've successfully move a few projects from XCode 3 to 4 and not had the problem you are mentioning. I suspect that if you can stop using -force_load your problems will go away.

Project that builds both framework for OS X and static library for iOS

I am trying to build a library of code that can be built into a .framework for OS X as well as a shared library for iOS. I'm not sure this is possible, there seems to be some strange behavior on XCode 3.2.5's part.
I want one project that holds all my shared code. Keeping the #if and #else definitions out of the equation here, I'd like this code to be able to be built with the 10.6 SDK as well as the iOS 4.2 SDK. I'd like to have 2 separate targets in my project, one building the .framework, and the other building the shared library lib*.a. They would each only get to compile the code that is relevant to them, but this single project would hold all of it.
My first steps seemed ok - created a fresh new project for building .frameworks, then I added a target to build an iOS shared library. As soon as I flip back to the .framework target, I get an error trying to build:
"target specifies product type 'com.apple.product-type.framework', but there's no such product type for the 'iphonesimulator' platform"
I didn't change the "product type" in the framework target (not even sure how), but it now seems to think that it's supposed to build everything for iphonesimulator, and that product doesn't support frameworks (which indeed it doesn't).
Has anyone else gotten this to work? Is this possible with XCode 3.2.5? Will this be possible in XCode 4?

Resources