How to build universal static library that works for os3.x and os4.x - iphone

Hi I'm trying to create a static library that can added to any ios project, but I can only get it to work such that if I build the library in ios3, it'll work for ios3 projects but not ios4 and vice versa.
The errors I get are:
Undefined symbols:
".objc_class_name_UIImage", referenced from:
literal-pointer#_OBJC#_cls_refs#UIImage in Test3-Release.a(TestViewController.o)
".objc_class_name_NSNotificationCenter", referenced from:
literal-pointer#_OBJC#_cls_refs#NSNotificationCenter in Test3-Release.a(Test.o)
So it looks like it's not finding Foundation and UIKit frameworks for the library?
Here's what I've done so far:
I have 2 projects: 1 is a library project with a library build target. The other is a Test Project that tries to use the library built in the first library.
1) Create a project with a static library target and added all of the implementation files
2) Added libraries like UIKit, Foundation, etc to the "Link Binary with Libraries folder and Set all the linked libraries to have a weak type in my library target.
3) Built the library, added it to my test project, along with my header files.
4) In my test project, I set other linker flags to -ObjC -all_load for all build configurations
When I compile it gives me a bunch of errors as if I didn't include any frameworks.
Anyone have any ideas on what I should do?
Hint (Maybe?)
I also noticed that adding any frameworks to the "Link Binary with Libraries" folder in my library target doesn't seem to do anything. I added CoreLocation to that folder, but when I try to compile in my Test Project it won't compile until I add CoreLocation to my Test Project Frameworks folder.

your dependent project (that uses static library) hassn't linked Framewrok libraries.
Just add to dependent project frameworks (uikIt,Foundation, etc) all will work fine.

Related

Is it possible to import and add .xcframework file to a static xcode library project?

I have set of .xcframework files, and currently I working on a static library
I need to integrate those .xcframework files into my static library, but it seems Build settings menu and build phase menu on a static library project is different and I can't add .xcframwork files like I used to do on a Xcode app project
You add dependencies under Build Phases tab, in Link Binary With Libraries step:
Be advised that in this scenario you cannot embed any framework. It either has to be delivered alongside the library or adding the dependencies has to be part of the library integration instructions. I.e. the consumers of your library will need to add this xcframework dependency manually in their target.

Compile project that uses many modules

I'm writing an app that is divided into frameworks. I would like the Xcode to compile all modules at once (whenever needed) when I'm trying to build main project target. Unfortunately when I press "play" I get an error like:
Header 'Observation-Swift.h' not found
module.modulemap
and
Could not build Objective-C module 'Observation'
World.swift
All targets are written in swift, so Objective-C is a mistake.
The only way to make this compile is to compile all targets one by one. Is there some way to make all targets compile automatically? Also there are some dependencies between targets.
WebService imports Observation
World imports Observation and BackgroundTask
I have found the solution. I needed to select every target that have dependencies and add used frameworks under "Linked Framweorks and Libraries". Then Xcode knows in what order to compile frameworks and it works.

iphone - ".../libtool: can't locate file for: -lxml2"

I have a 2 projects in an xcode workspace. One is a static library and the other one use the static library.
In the static library I added the GDataXMLNode.h and GDataXMLNode.m files, which I use in the main project. The GDataXMLNode.h is a public file so it's visible in the main project. I also linked the target of both projects with libxml2.dylib. And I also include in the Header Search Paths and User Header Search Paths the location /usr/include/libxml2.
Although these, I get an Apple Mach-O Librarian Error:
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: can't locate file for: -lxml2
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: file: -lxml2 is not an object file (not allowed in a library)
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: file: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/libxml2.dylib is a dynamic library, not added to the static library
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: file: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/libxml2.dylib is a dynamic library, not added to the static library
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: file: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/libz.dylib is a dynamic library, not added to the static library
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool: file: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/libz.dylib is a dynamic library, not added to the static library
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/libtool failed with exit code 1
Do you know how can I fix this?
I had the exact same error recently. As you found, removing "-lxml2" from the "Other Linker Flags" in Build Settings helped resolve the errors. This is true when you are building a static library. You will need to include that linker flag ("-lxml2") in any target that actually includes your static library. For example, if you created unit tests for your static library as I did (i.e. MyStaticLibraryProjectTests) then you would need to make sure to add the linker flags to the MyStaticLibraryProjectTests target but not to the actual MyStaticLibraryProject target. More often than not I generally forget to select a specific target when I go to look at my Build Settings tab. I usually assume that if I click on the big blue project icon that the build settings are just for the entire project but it is very important to select the correct target when modifying those settings.
Early on most projects generally do only have a single target but as you get more advanced it is possible to have multiple targets for each project. Hope that helps.

Building a static library for iOS

I have built a static library for iOS. My problem is that I use ASIHTTPRequest as an example in my library, and when I add my library to an app that already uses the ASIHTTPRequest library, I get compiler errors, until I remove all ASIHTTPRequest .m files from the project that uses my library.
Is there a way around this ? Like for example build the static library and make it require the ASIHTTP libraries instead of them being added to the project ?
EDIT :
Here is an example of the error I get once I "archive" the project.
ld: duplicate symbol _main in /Users/tj/Library/Developer/Xcode/DerivedData/db-afhqjyfitjcnbqdmywezdvmryvyp/ArchiveIntermediates/db/IntermediateBuildFilesPath/db.build/Release-iphoneos/db.build/Objects-normal/armv6/fmdb.o and /Users/tj/Library/Developer/Xcode/DerivedData/db-afhqjyfitjcnbqdmywezdvmryvyp/ArchiveIntermediates/db/IntermediateBuildFilesPath/db.build/Release-iphoneos/db.build/Objects-normal/armv6/main.o for architecture armv6
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2 failed with exit code 1
Trausti
What I would suggest is to make ASIHTTPRequest into a static library of its own, by adding it as a dependency project in your static library's project. Essentially what you do, is to create an xcode project, pack ASIHTTPRequest into it, close that project, then drag&drop the ASIHTTPRequest project file into your static library project's file navigator. This will add it as a dependency project and automatically build it as a static library and link your project against it.
When using your static library in one of your application projects do the same. It is easier to manage your static library as a dependency project which in turn has ASIHTTPRequest as another level of dependency. This way both of your projects will be able to see its headers, and link against it and there will be no duplicate symbol issues.
If you are trying to publish your static library to other developers then it is a more difficult situation...

iPhone static library linking problem

I having trouble with app that uses my static libs. Here is my setup:
I have 3 libs, Utilities, Share and Misc. Share and Misc depend on Utilities. They are compiling just fine when I compile them separately. Every lib is in its own XCode project.
I want to build an app that uses features from Utilities and Share, so I added them into the project as direct dependencies. I see them listed in target "Link binary with libraries". I also see quite a lot of linking errors in the compilation results. Those error relate to 3rd parties code that is used in libraries, for example ASIHTTPRequest. Such 3rd party stuff I keep in one place and I just added path to that directory in "Header Search Paths".
Example error:
objc-class-ref-to-ASIHTTPRequest in libShare.a(SomethingThatIsInLib.o)
It's confusing because libs alone are compiling just fine but when I try to compile the app, it's throwing such errors. What am I doing wrong?
Add the -all_load and -ObjC flags to "Other Linker Flags" in the Xcode project settings.