Xcode -dynamic not specified static library error - ios

I have a sub project within Xcode which creates a static library referenced by the parent project. All has been well until the release of iOS 7.1 and Xcode 5.1, suddenly I'm getting the following warning.
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: -dynamic not specified the following flags are invalid: -ObjC
warning: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/usr/lib/libsqlite3.0.dylib is a dynamic library, not added to the static library
Does anybody know what I need to do to fix this warning?

I think the solutions lies in the basic concept of dynamic and static libraries. as far as libraries go, dynamic libraries (libsqlite3.0.dylib in your case) are system libraries and you do not need to add them to your static library! all you need to do is to add their reference in your final Target you want to use them in (your application).
so, in short: Try removing the reference to sqlite3.0.dylib from your static library (I'm guessing another project you are using inside your top level application) and add it to your application (not static library) and you're probably good to go. (solved my problem)
[Edit]:
please commit your project's git or do whatever you do to make a backup from your project, there is a chance that Xcode may break your project with no reason after removing these dynamic libraries and NO, deleting project's Derived data won't solve the problem.

Related

Error compiling Redis library into iOS Project ("_OBJC_CLASS_$_ObjCHiredis", referenced from:)

I am trying to integrate the simulator static library of Redis into my iOS project and have done the following things.
drag and drop the static library into my project folder
Add header search path in the build settings
Check whether the static library is added into the build phases in settings.
Set the header search path
Set the other linker flags setting to "-all_load"
I am not sure where i have gone wrong but am geting the following error while compiling it.
Any help in this regard is greatly appreciated.
I was also getting such errors and figured out that adding .m files in "Build Phase" -> "Compile Sources" for the target fixed my problem.
Actually this happens if we miss to tick the Target Name in "Add to targets" name while adding any new library to it. (For your case, after drag and drop you might have seen a dialog box with options to copy items if needed, Add to target etc.)
Finally build and run. Hope this solves your problem!
The static library which you are dragging is not build for i386 architecture(simulator), try running your app on device, or build for device. Basically your app with the corresponding library will work on all such architectures for which it is built(armv7, armv64).
If you want to use the library and build your app on the simulator you need the static library which is also built for i386. And this could be provided by the developer of the static library.
Basically a fat(static library) file is created using static libraries for i386 and armv7(any required architectures) and shipped with the SDK of the static library which works on simulator as well as device.
The fat file is created by executing a lipo command on individual architecture static libraries.

Can the -ObjC flag be applied selectively to static libraries?

TL;DR
How can I make the -ObjC linker flag target a specific static library and not all the static libraries I am linking against in order to avoid unused object files being linked in with my app?
Too Long; Did Read
So you are developing a new iOS app and you add in your homegrown "objcutil" static library which contains a variety of useful Objective-C classes (not implemented as categories) to do various things that have been useful in the past. So far, so good, and only those object files that are being referenced in the utility library are being linked with the app.
Then you decide to integrate the Google Maps SDK which wants you to use the -ObjC Other Linker Flags and all of a sudden dependencies in the utility library fail to be resolved, because you haven't configured Xcode to link to those libraries.
OK I can resolve the missing dependencies easily enough, however you now have unused object files and library dependencies that you don't need and you'd like to be a bit tidier than that.
So how do you avoid OCD overload?
Some reference from the ld manpage:
-ObjC Loads all members of static archive libraries that define an Objective C class or a category. This option does not apply to dynamic
shared libraries.
Xcode Version: 5.1.1
OSX Version: 10.9.4
OK so the answer is to use -force_load instead of -ObjC as -force_load is more focused.
So WRT to the Google Maps SDK, if you followed the instructions and copied the static framework into the app project directory, then the framework will be in the project root directory and you can remove -ObjC from the Other Linker Flags and replace it with
-force_load GoogleMaps.framework/Versions/Current/GoogleMaps:
Nothing else needs changing.
For other libraries you will need to use the full static library path as the argument to -force_load.

Adding static library to project >> Undefined symbols

I have created a static library which I added to another project in the same workspace in XCode. The library builds fine, both for Simulator and an actual device.
All errors refer to classes in the library, so it's not about another framework.
I get two undefined symbols errors that both refer to "_OBJC_CLASS_$_Campaign"
When I build for an actual device I get Undefined symbols for architecture armv7s
When I build for a simulator I get Undefined symbols for architecture i386
I tried creating a fat library: same issue.
I toggled "build for active architecture only" on and off: doesn't help.
I deleted and rebuilt the XCode workspace: no solution.
I'm very sure I added the correct .m files to the "compile sources" of the library, and I properly added the library to the other project through "Link binary with libraries". I also tried just dragging the .lib to the Frameworks folder.
I added the header file of the .lib to the project.
I would be glad with any suggestion!
The problem is likely caused by the fact that your static library import something from another library.
You need to figure out what symbols are missing exactly and add library that defines them. Build log output should be very helpful in this regard.
EDIT: Based on discussion in comments, problem is caused by the fat that there's no implementation for Campaign class. Without implementation, compiler doesn't generate class and this results in linking error. There are no compile-time errors because interface of the class is declared though.
You should add such implementation, either in a separate Campaign.m file, or in one of already existing files.

Export an `OBJC_CLASS` from one static lib as part of another

I want to create a static library (actually, a framework, but I know how to do that part) that bundles up code from, among other things, another static library. However, the OBJC_CLASS exports from the original library end up as undefined symbols.
For example, in Xcode 5.1.1 (using default settings/choices at every step, unless otherwise specified):
Create a new "iOS Framework & Library Cocoa Touch Static Library" project named LibA.
Build (for either simulator or a real device, doesn't matter).
Create another new "iOS Framework & Library Cocoa Touch Static Library" project named LibB.
Drag libLibA.a from the LibA products to the Frameworks folder in the LibB project tree.
Drag LibA from the include directory next to the static lib to the top level of the LibB project tree.
Edit LibB.h as shown below.
Build (same target as before).
Create a new "iOS Application" (any type) project named AppC.
Drag libLibB.a from the LibB products to the Frameworks folder in the AppC project tree.
Drag LibB from the include directory to the top level.
Drag LibA from the first project's include directory to the top level.
Verify that LibA appears in the Link Binary With Libraries phase.
In any method of any class the wizard generated (e.g., -[MasterViewController awakeFromNib]), add (void)[[LibB alloc] init].
At the top of the .m file you just edited, add #import "LibB.h".
Build.
Here's the LibB.h promised above:
#import <Foundation/Foundation.h>
#import "LibA.h"
#interface LibB: LibA
#end
I get the following error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_LibA", referenced from:
_OBJC_CLASS_$_LibB in libLibB.a(LibB.o)
"_OBJC_METACLASS_$_LibA", referenced from:
_OBJC_METACLASS_$_LibB in libLibB.a(LibB.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Looking at the files, the problem is obvious:
$ nm -g libLibB.a
U _OBJC_CLASS_$_LibA
0000031c S _OBJC_CLASS_$_LibB
U _OBJC_METACLASS_$_LibA
00000308 S _OBJC_METACLASS_$_LibB
U _OBJC_METACLASS_$_NSObject
U __objc_empty_cache
The symbols for _OBJC_CLASS_$_LibA and _OBJC_METACLASS_$_LibA are exported as undefined.
I can reference methods, C functions and structs, globals, etc. from LibA. Even categories on Foundation objects (as long as I do the category-dummy trick). It's only the class and metaclass objects that I can't figure out how to export.
Here's what I've tried to fix it:
Turn off Dead Code Stripping (in all three projects).
Add -ObjC as an extra linker flag (in all projects). (This makes no sense for static libs, and all it does is give you a warning error telling you exactly that, but everyone suggests it to me.)
Create an "Exported Symbols File" (for LibB). (This also only makes sense for dynamic libs.)
Pass ${PROJECT_DIR}/libLibA.a as an "Other Linker Flags" (for LibB) instead of adding libLibA as a framework (in case -lLibA is processed differently from libLibA.a).
What I've tried that I still think may be on the right path, but I'm not sure:
Try to figure out appropriate libtool options that have no corresponding settings in Xcode. (I can wrap it in a Makefile, or and Xcode custom build step, if necessary.)
Enable "Perform Single-Object Prelink", then add ${PROJECT_DIR}/libLibA.a to "Prelink libraries". I get warnings about duplicate symbols and then success but with an empty libLibB.a, so obviously there's something else I need to do. I've done this with .dylibs and dynamic Frameworks on OS X, and there wasn't anything else I needed to do there… but never with static libs.
Workarounds that I know about (and I'll use one of these if there's no real solution):
Require that anyone who wants to use LibB also has to add LibA to their project. And, in particular, the pre-built copy of LibA that we provide.
Distribute LibB as source to be included in your project, instead of a static lib and headers.
Manually ar libLibA.a and LibB.o, then ranlib like it's 1999 (although the docs say this doesn't work, it seems to).
(None of these are too terrible for my simple test project, but in real life, this is not an open source project, that LibA is actually 80 different libs from 3 different projects, and some of the LibA code builds fat armv7/armv7s (which means ar doesn't work on it…), and we're planning to do the usual hack of lipo'ing together the simulator and native builds and making a framework out of them, all of which makes things more of a problem.
I think I may have solved it with single-object prelink (basically this means it does an ld -r to build a giant object file, then passes that to libtool), although I'm still not sure, and I don't love the solution. So, I will post what I've got as an answer, but hope someone else comes along with a better answer.
To get single-object prelink to work, you need to (in LibB):
Add libLibA.a as a Framework.
Make sure it does not appear in the Link Binary With Libraries build phase.
Set "Dead Code Stripping" to No.
Set "Don't Dead-Strip Inits and Terms" to Yes.
Set `Perform Single-Object Prelink" to Yes.
Set "Prelink libraries" to ${PROJECT_DIR}/libLibA.a
Set "Preserve Private External Symbols" to Yes.
(The second step is what I was doing wrong earlier…)
Unfortunately, this seems to break the dependency rules completely, so that every build recompiles every .m (and .pch) that's part of the target, even if nothing has changed.
Other than that annoyance, this seems to work for both AppC and my real project just fine.
AppC does not need "Preserve Private External Symbols"; my real project does. I believe this is because one of the third-party libraries does an ld -r with an empty -exported_symbols_list explicitly to "transform all symbols to private_extern. Otherwise, class objects don't end up that way. However, I'm not 100% sure I understand this one.
Adding this to Other Linker Flags appears to work
-force_load $(CONFIGURATION_BUILD_DIR)/libLibA.a

Is there a better way to link to a configuration specific static lib on iOS than OTHER_LINKER_FLAGS?

Situation: Linking against an SDK (which I'm building) that has Release, Debug, & Distribution versions of it's static library (.a file). There doesn't seem to be a way in Xcode GUI to indicate that a static library is only used for a given Configuration.
I can use "Other Linker Flags" (OTHER_LDFLAGS) in the Build pane of the target or project settings like this:
-all_load -ObjC "${SRCROOT}/MySDKFolder/${CONFIGURATION}-universal/libMYsdk.a"
which seems to work. Just wondering if anyone knows a better way. ( the -all_load and -ObjC are to get ObjC categories linked in properly).
I'm using gcc 4.2 at this point (SDK default for 4.3 sdk and 3.x.x Xcode).
So, it turns out I was letting the Xcode UI confuse me. If you add the generic search path with the ${CONFIGURATION} variables and then add one of the static library instances to the project and target it'll do the right thing and link with the right one even though if you get info on that library in the project it'll show you the specific path.
However, if you want it to actually have Xcode notice changes made to one of the static libraries, you'll need to add ALL versions of your static library to the project and target (yes, it'll look like it's linking with all three, no worries!).
To have Xcode detect the the .a file changed as the result of a build script phase you have to actually have all the source files that are used to build the .a file in the Run Script Input pane (a pain!) to not have to run your build script every time; And all three variants of the .a file in the output pane so Xcode knows what to check after your script runs to see if it needs to relink the project.

Resources