XCode 4.3: Static Library Generation - objective-c

I've done a some looking around but most of the answers I've found have been or felt incomplete and have left me a little confused. I have been given a C Library that I need to compile into a static library using XCode 4.3 and then use in a separate iOS app project, but I'm unsure about how to proceed. I'm not sure if the directory structure matters or not, but here it is anyways:
Library -> Section1 -> src -> .c files
-> sec1 -> .h files
-> sec1.h
-> Section2 -> src -> .c files
-> sec2 -> .h files
-> sec2.h
I've been trying to work from this: http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html which was linked in a question similar to this one though being from 2008 its fairly out of date, nor could I get it to work. There is also this question: Including external C library with Xcode but it doesn't go into the details of actually generating the library, before then including in a separate project.
If someone could provide a clear and up-to-date answer I, and many others, would very much appreciate it I'm sure. Let me know if any more information is needed!

To build the static library:
Create a static library project in Xcode
Add all the .c and .h file to the project
Compile
The easiest way to use this library is then to add this static library project to you application project. This avoids having to worry about creating fat libraries (i.e. libraries with code for both the simulator and device).
To add the static library project to your application project:
Choose File > Add Files to ""...
Add the .xcodeproj for your static library
Click on your app's .xcodeproj in the Project Navigator to show build options
Click on your app's target and choose the "Build Phases" tab.
Expand the "Link With Binaries Section"
Click the '+' button
Expand the "Workspace" section (you should see your library, a .a file, there)
Click on your library and you should be good to go.
Apologies for excruciating level of detail above, but somehow people always seem to forget to do steps 4-8 and then they wonder why they are getting link errors!
Xcode will not be able to find the headers for your library. You can either add the public headers to your project as you would any other header file or set the "Header Search Paths" in your build settings.

Try the Universal Framework project, as seen on github: https://github.com/kstenerud/iOS-Universal-Framework/. I have used this extensively, and it works nicely. You just create a new XCode project for that library, put in all the source and header files, and it will build a static Framework. That you can use in other projects, and you also don't have to worry about the header search paths.

Related

Importing Static library into Custom Universal framework

I have created a custom universal framework by following this tutorial
tutorial.
Here My question is how to import a static library .a files into framework.??
Thanks in advance.
Just drag and drop the .a file into your frameworks folder.
Or you can go to Build Phases -> Link Binary With Libraries -> Click on the '+' sign -> Add Other (bottom left) -> choose your .a and click Add
Make sure to include the header files of your static library in your new project as well.
In my custom framework project setup I have added the static library which I have created to link binary with libraries and I have added the static library header files to Aggregate target copy files build phase. thats it builded like charm. and that framework is working fine.

How do you add GPUImage to an iOS project?

I am trying to make a camera/photo app that will add a filter on an image. I have heard of Brad Larson's GPUImage and so I downloaded it and tried to manipulate it to be more familiar with the code.
Now, I made a new project in Xcode and added it on my frameworks, but i don't have any idea how to use it on a new project.
How can I properly use GPUImage in my new project?
I don't know how I could be much clearer than the step-by-step instructions I put on the very first page of the GitHub project and in the README.md under the section "Adding the framework to your iOS project":
Once you have the latest source code for the framework, it's fairly
straightforward to add it to your application. Start by dragging the
GPUImage.xcodeproj file into your application's Xcode project to embed
the framework in your project. Next, go to your application's target
and add GPUImage as a Target Dependency. Finally, you'll want to drag
the libGPUImage.a library from the GPUImage framework's Products
folder to the Link Binary With Libraries build phase in your
application's target.
GPUImage needs a few other frameworks to be linked into your
application, so you'll need to add the following as linked libraries
in your application target:
CoreMedia
CoreVideo
OpenGLES
AVFoundation
QuartzCore
You'll also need to find the framework headers, so within your
project's build settings set the Header Search Paths to the relative
path from your application to the framework/ subdirectory within the
GPUImage source directory. Make this header search path recursive.
To use the GPUImage classes within your application, simply include
the core framework header using the following:
#import "GPUImage.h"
As a note: if you run into the error "Unknown class GPUImageView in
Interface Builder" or the like when trying to build an interface with
Interface Builder, you may need to add -ObjC to your Other Linker
Flags in your project's build settings.
Also, if you need to deploy this to iOS 4.x, it appears that the
current version of Xcode (4.3) requires that you weak-link the Core
Video framework in your final application or you see crashes with the
message "Symbol not found: _CVOpenGLESTextureCacheCreate" when you
create an archive for upload to the App Store or for ad hoc
distribution. To do this, go to your project's Build Phases tab,
expand the Link Binary With Libraries group, and find
CoreVideo.framework in the list. Change the setting for it in the far
right of the list from Required to Optional.
Additionally, this is an ARC-enabled framework, so if you want to use
this within a manual reference counted application targeting iOS 4.x,
you'll need to add -fobjc-arc to your Other Linker Flags as well.
To see this in action, look at any of the many sample applications I ship with the framework.
Please read this page http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework and this: https://github.com/BradLarson/GPUImage
Note: Simply, you can copy all source code files in framework/source folder to your project, and import GPUImage.h to use.
Regards!
Here's how to do it in Swift 5 in Xcode for iOS with Visual Directions:
Two things before you begin.
you should start this process on a copy/duplicate of your project until you get it correct (it took me several tries), or even start with a new project. If something goes wrong you want to keep your original project intact
if you have a project open with the GPUImage in it that you are trying to copy, make sure to close it first. You will get an error saying that there is a project with it already open
1- go to the GPUImage gitHub page
2- in the upper right hand corner, click the green Code button and then click Download Zip
3- in your current Xcode project (the one that you want to use GPUImage) click the first folder underneath the blue project icon
4- after that go to the top of Xcode and click File > Add Files to "yourProject"
5- when the new window appears go to Downloads (or wherever you downloaded GPUImage in the second step) > GPUImage3-master > framework > GPUImage.xcodeproj and then click Add. It's important that you make sure Copy Items if Needed and Create groups are checked.
6- you will now see GPUImage.xcodeproj inside your Xcode project underneath that same folder from step 3 (no need to do anything with it)
7- click your main project's blue project icon > Build Phases and the middle/center pane should be on TARGETS. Make sure you DON'T click the GPUImage.xcodeproj, that is the WRONG folder
8- while still under Build Phases click Dependencies then click the plus sign
9- when the pop up appears select GPUImage_iOS then select Add (make you sure you pick the iOS version and not the macOS version)
10- you will now see the GPUImage_iOS dependency under Dependencies (no need to do anything here)
11 -now while still under under Build Phases click Link Binary with Libraries then click the plus sign
12- when the pop up appears under Workspace > GPUImage > select GPUImage.framework from 'GPUImage_iOS' target ... then press Add (make sure you select the iOS version and not the macOS version)
13- now the library is linked (no need to do anything here)
14(a)- This is important, if underneath Build Phases there isn't a Copy Files folder than go to the top of Xcode and select Editor > Add Build Phase > Add Copy Files Build Phase. If the folder is already there I'm not sure what to do if the folder already exists. The Copy Files folder wasn’t initially there for me and I had create it using step 14(b). I would assume if it’s already there just open it and continue to step 16
14(b)- if for some reason Add Copy Files Build Phase is grayed out, do this or under Build Phases, in the upper left hand corner press the plus sign, then select New Copy Files Phase
15- now you will see a brand new Copy Files directory underneath Build Phases
16- if Copy Files isn't already open, press the toggle button to open it and next to Destination change Resources to Framework
17- while still under Copy Files, press the plus sign, when the pop up appears, if this is an iOS project, under Products, select the top GPUImage.framework and press Add. This last step is under Brad's directions
Add a new Copy Files build phase, set its destination to Frameworks,
and add the upper GPUImage.framework (for iOS) or lower
GPUImage.framework (for Mac) to that
18- now the GPUImage.framework has just been added to Copy Files
19- your Build Phase screen should look like this
20- clean the project Shift+Command+K
21- in whatever file you intend on using the GPUImage in go to the top and enter import GPUImage (it should start to appear under autocomplete) then build your project.
22- test to see if a Class and a Method from GPUImage works inside your project
// nothing is supposed to actually happen here, just test to see if the SmoothToonFilter() class and filterWithOperation() method are recognized without any errors
let testImage = UIImage(named:"some_image_from_your_assets")!
let toonFilter = SmoothToonFilter()
let _ = testImage.filterWithOperation(toonFilter)
Done!
Probably the simplest of all ways of integrating GPUImage into your project is via cocopods. And while the GPUImage CocoaPods repo isn't actually maintained by Brad, it's upkeep is pretty regular, and it makes integration a breeze.
> Go get it

Compile xcode project into a library file

I have two projects that are build on 2 different compilers. I need to combine both into one single app. Found out from a source that it is possible to compile any one of the project into a library and add it into the other to use it.
I fairly new this concept. Has any one done anything like this? How do i convert one of the project into a library and how would i connect the two? Will i still able to call the views in that project(library) in my other project? I am using XCODE 4.2 and iOS 5.
Create a "Cocoa Touch Static Library" to build your "Project"/target, and include the then-built library (with its public headers) into the second one. Note that the details depend quite a bit on what you mean by "project" and by "call the views" (which looks a bit wrong).
If you have two existing projects (with .xcodeproj files), open the "app" project (guessing one is of such kind), and include the lib project inside it (by drag & drop, or by "Add Files..."). Not the opposite.
Then in the "Build Phases" of the app project, you can add your "lib" project as a target dependencies (making your lib to always build when building your app). Finally, add the lib project product (the .a) in the "Link" build phases of the "app" project.

Adding a static library to an XCode 4 project breaks "build and archive" feature

I built a static library with MGTwitterEngine and some OAuth / TouchJSon libs.
Then I added that within the main XCode proj of an app.
I'm having trouble with this feature of XCode (build and archive). After I added the libs, the "build and archive" no longer generates a .IPA
Do you know a good tutorial to make some proper use of this archive feature ?
What should I do within the XCode Projet to make sure it will work correctly with my static libs ?
ps: The second is the most important to me.
Thanks in advance ;)
You should change your static library's "Build Settings", make sure Skip Install is set to YES
If still not work, check your static library's "Build Phases", make sure:
After that, your Archive should be OK.
But if your Xcode start to report who can not find your static library's .h files, you can just add them into your project as references, or change your project's "Build Settings", let it find your .h files in your static library's folder.
According to the Apple engineers, you should not use the "Copy Headers" build phase with static libraries on iOS - it's only intended for dynamic libraries (oddly using "Copy Headers" works fine for most cases, but definitely breaks the 'Archive' case).
Instead add a "Copy Files" build phase, setup like this:
If you are using the static libraries template, this build phase should already be present.
Drag all the header files that should be exposed into this section.
You also need to see "Skip Install" for the library to yes as other people have answered.
The video for WWDC 2012 Seassion 408 "Working with Schemes and Projects in Xcode" covers this from timestamp 45:18.

Cannot find output .a of Cocoa Static Library (in xcode 4)

I have a project with two targets, one is a Cocoa Static Library, the other is the accompanying test project. Despite building the main project in different ways over and over again, I cannot find the .a file that I expect it to produce.
In fact, I cannot find the build folder associated with the project. I need to link to the library in an app, but cannot do so if I can't find the file to link to.
These properties are correctly set:
(Build Products Path) SYMROOT = build
(Intermediate Build Files Path) OBJROOT = $(SYMROOT)
All tests pass (which means the code MUST be building right?)
Breaking the code causes the build to break - again suggesting that it is building.
Also, the "Products > libproject.a" file is red in the xcode project navigation
I also checked the DerivedData directory, but all the seems to get created is the objects fot the OCunit stuff. Still no .a file against which I can link.
Where is my .a file?
Any help would be much appreciated.
It's probably in ~/Library/Developer/Xcode/DerivedData/ somewhere.