Build and guard against missing library using Objective-C - iphone

I have a library (lib.a) and a header file (lib1.h). The problem is the library is too big and only a relatively small subset of users need it.
Would it be possible to build my Xcode project without the library? Currently I get:
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Would it be possible to guard against usage of the library not existing inside the Xcode project? I found the following, but I'm not sure if it's sufficient (assuming I can get the project to build without the lib):
if ([MyLibABC class]) { ... } else { NSLog(#"Add Lib.a"); }
Side note:
In Java I would use reflection to call methods implemented in the library (lib.jar) and if the library is not present I would catch the ClassNotFoundException and show an error message or something (lib.jar missing, please consult the documentation).
I'm targeting >= iOS5.

Unlike desktop operating systems which support dynamic libraries, iOS supports only static libraries. The only way around this would be building two targets - one that uses the library, and the other one that does not.
You can build both targets from the same set of sources by using conditional compilation. In the version that conditionally compiles out the references to the "big library" there would be no references to it, so the linker would not complain about missing references.

Related

Swift Package Manager (package successfully added, but Module not found)

I'm new in Swift. I want to create iOS app that can connect to PostgreSQL database. First I found library https://github.com/vapor/postgresql.git that should be added to my project via Swift Package Manager. Using tutorial I added required library to my project successfully (File -> Swift Packages -> Add Package Dependency):
list of added packages from SPM
But when I try to import this module into my view controller, Xcode shows error that module is not found:
not found
I tried several times to rebuild my project, created new project just for testing this issue. Also I found information about build phases and added this lib as a dependency:
build phases
But I still get error: "No such module PostgreSQL".
Can anyone help me?
I found solution by myself. I compiled C static library "libpq" (can be found in PostgreSQL sources) and added it to my swift project. Included this library by adding special bridging header file. Finally I got what I wanted.
P.S. If someone what to repeat, he or she should know: static C library must be compiled for iOS device architecture (and also in iOS simulator architecture which differs from iOS device arch.).

Linking dynamic library within static framework

I am building the static iOS framework upon multiple libraries in Xcode. One of them should be the card.io. I cannot use cocoapods or carthage. So far I imported .frameworks within the .framework and it works pretty well. However card.io uses static library (.a file) with bunch of headers. It works well in dynamic type of .frameworks (or iOS app project) but in static project I get these errors on including of the .a files:
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lCardIO
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lCardIO is not an object file (not allowed in a library)
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lopencv_core
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lopencv_core is not an object file (not allowed in a library)
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lopencv_imgproc
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lopencv_imgproc is not an object file (not allowed in a library)
Regarding these errors I downloaded the source codes of card.io and it looks that there is dynamic .framework target waiting for build. I tried to use this one instead of .a files and headers - so my project at least can be builded. Because the card.io does not contatin architectures for simulator (which, by the way, it should with this release 5.2.2) I am not able to test it in unit tests, so when I test this solution on device I get this error:
dyld: Library not loaded: #rpath/CardIO.framework/CardIO
Referenced from: /var/containers/Bundle/Application/55D3AF7F-83F4-4B3D-A667-0FCO93CCC441/App/AppDemo
Reason: no suitable image found.
So far my knowledge+google+stackoverflow is stuck here, because it looks that xcode does not support the .framework within .framework this way.
To this moment I spent two days with this "issue", so the question is: Does exist any solution for including dynamic library into static framework? Or any solution to include card.io into static framework?
EDIT:
Well actually the solution was more stupid than I would think (as always). Just to include card.io in the .framework go to "Project Description -> Build Settings -> Library search paths" and type the path where should xcode look for the libraries. This approach solves the first one issue of this post - this means the implementation of the .a libraries and headers.
At this moment I cannot guarantee it will work in releases based on my framework so I will update this post to confirm it later. I hope it helps someone...

MacOS Swift Framework testing fail

I have a framework written in obj-c and swift.
Now i try to run a related unit test target, but I get this error:
2014-07-10 07:45:54.064 xctest[4908:303] The test bundle at /Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest could not be loaded because an unanticipated error occurred: Error Domain=NSCocoaErrorDomain Code=3587 "The bundle “SOGraphDB-Mac Tests” couldn’t be loaded because it is damaged or missing necessary resources." (dlopen_preflight(/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests): Library not loaded: #rpath/libswiftAppKit.dylib
Referenced from: /Users/steve/Temporary/Build/Products/Debug/SOGraphDB.framework/Versions/A/SOGraphDB
Reason: image not found) UserInfo=0x10011c640 {NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests, NSDebugDescription=dlopen_preflight(/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests): Library not loaded: #rpath/libswiftAppKit.dylib
Referenced from: /Users/steve/Temporary/Build/Products/Debug/SOGraphDB.framework/Versions/A/SOGraphDB
Reason: image not found, NSBundlePath=/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest, NSLocalizedDescription=The bundle “SOGraphDB-Mac Tests” couldn’t be loaded because it is damaged or missing necessary resources.}
Any idea what the root cause can be?
The error seems realated to "Library not loaded: #rpath/libswiftAppKit.dylib"
Both (framework and test bundle) compile without any error or warning (in Beta2)
I had a similar problem, though mine was an iOS test target, linking to a Swift framework, that failed to run on Xcode 6 GM. The test target had run successfully on an early beta of Xcode 6, but the final version reported the runtime error: Library not loaded: #rpath/libswiftCore.dylib
I noticed that a newer project did not have the failure, so I compared the build settings and test code. I was able to resolve the problem with three steps:
The test target needs the "Embedded Target Contains Swift Code" setting to be YES. This tells the linker to add the Swift runtime libraries to the executable.
The test target needs an explicit value for the "Runpath Search Paths" build setting. This tells the loader where to find the Swift runtime libaries. I copied the following setting from a fresh new test target:
LD_RUNPATH_SEARCH_PATHS = $(inherited) #executable_path/../Frameworks #loader_path/../Frameworks
The test cases need to explicitly import any modules that are used by the linked framework. In my project, the framework used UIKit but the test cases only used my framework. When I added an explicit import UIKit to the test cases, the link problem went away.
Since I keep running into this issue whenever I mess with build settings, here's the cleanest answer I can provide as of Xcode 8b5:
If unit tests don't run on iOS, make sure you have:
Runpath Search Paths: #loader_path/Frameworks
If unit tests don't run on macOS, make sure you have:
Runpath Search Paths: #loader_path/../Frameworks
This will show up as LD_RUNPATH_SEARCH_PATHS in your pbxproj file. You can also add $(inherited) to make sure project-wide paths are added as well, but those are probably empty.
Lastly, I didn't need the executable_path/... settings, doesn't make a difference for me whether they're there or not for unit tests.
I had the same problem.
Ended up copying libswiftAppKit.dylib out of the Xcode application directory (I am using beta 3), into a directory that I could reference (not part of an application bundle), then adding the library to the "Link binary with libraries" setting for the test bundle. My tests then started working.
FYI, the path for the dylib was at '/Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftAppKit.dylib'
Probably not a good final solution, but it works for the interim.
My solution was to add a single file to my test target, AppKitTests.swift (or AppKitSpec.swift in my case), and all the file contains is:
// While I don't know why, The.framework is built linked to libswiftAppKit.
// Frameworks do not embed the Swift libraries themselves, it's up to the host
// app to include the necessary libraries. So here we are, including AppKit on
// behalf of The.framework so that the tests can run.
import AppKit
Oh, I also added AppKit to the test target's set of frameworks.
I had this occur to me in Beta 4 and it turned out that I had done it to myself without realizing it.
I had a Framework project called Fnord. My FnordTests target contained a couple of unit test classes that had import Fnord at the top of each file. For some reason Xcode was complaining about those imports and so I commented them out. This is when I started getting the same symptoms while trying to run my unit tests.
I noticed that the settings for my FnordTests target were such that the target would be linked with Fnord.framework, and I realized that it might not be able to do that with the aforementioned import Fnord statements diked-out.
So I put them back in, and everything went back to normal. Curiously, Xcode no longer showed those import statements as being problematic, but this is beta so I can forgive that.
I hope this helps someone.
I ran into the same issue, adding
import AppKit
in the Swift file fixed it

Correct way to use a private library in objective c?

I downloaded a collection of private libraries from this link. When I click download I get all frameworks. So these are only header files not the .framework files that are available in Xcode. So I linked them by the usual method of going to build phases, in it I go to link binary with libraries click on + and choose the header files from a framework (preferences framework in my case). After these files are added to my project I try to make an object from one of the libraries and try to call their instance methods. When I try to execute this program I get this error. I get this whether I run it on the device or simulator.
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_DevicePINController", referenced from:
objc-class-ref in UAViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1
(use -v to see invocation)
DevicePinController is a part of a private framework preferences.h.I am trying to make an object of it UA
EDIT: I tried using other framework headers such as bluetooth and I get this error in all.
EDIT: I tried adding the entire framework to the project instead of adding individual header files.Now the error is
d: framework not found BluetoothManager
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You'll need to actually build the framework. You can't just link against a header file; that doesn't make sense.
Try adding all the .m files in the Preferences folder as Compile Sources, and remove the header file from Link Binary with Libraries.
Added: I realize now this answer is incorrect. The files OP is trying to use are not a library, but header files from Apple's private frameworks. Here's a related answer: https://stackoverflow.com/a/13388225/893113

Struggling with Xcode 4

I've recently downloaded Xcode 4 and now two of my projects that were working perfectly fine before have started giving me errors. Both errors are effectively to do with linking options, but I can't figure out how to change these options and get rid of the errors.
The first problem is with a project written in C++ using the SDL_ttf and SDL_image frameworks. The project builds correctly, but when I try to run, it gives me the following warnings on the console:
warning: Unable to read symbols for #executable_path/../Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf (file not found).
warning: Unable to read symbols from "SDL_ttf" (not yet mapped into memory).
warning: Unable to read symbols for #executable_path/../Frameworks/SDL_image.framework/Versions/A/SDL_image (file not found).
warning: Unable to read symbols from "SDL_image" (not yet mapped into memory).
Since the files are not being found, the executable cannot load any images making it exit straight away when I try to load images. I think the issue here is that the frameworks are not in the directory above the executable, they are in /Library/Framework/ which worked fine before in Xcode 3.2. How do I resolve this?
The second problem comes when compiling an application I wrote for iOS. Along with giving me a bunch of warnings about depracated code on iOS 5, which I will deal with later, it fails to build due to a linker error which I have no clue how to resolve, it says:
ld: library not found for -lz.1.2.3
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang failed with exit code 1
I can answer the second question.
-lz.1.2.3
should be
-lz
You might have accidentally added the zlib.1.2.3.dylib to the project's "link binary with libraries" build phase. You should use the standard zlib without any version in its filename, or simply add -lz to the Other Linker Flags under Build Settings and not add zlib in the "link binary with libraries" build phase.
As for the missing frameworks my best guess is that it has to do with the relative path, ie if you can get rid of the /../ part and instead provide an absolute path that might resolve the issue.