dyld: Library not loaded - When app run from the command line - iphone

I face some difficulties when adding a framework to my project when i run an iPhone app from the command line.
My final goal is to run applications tests from an automated build process.
When i run my app from xCode :
I add a "Copy File" build phase to my target and everything goes fine.
The copy is set to $(BUILT_PRODUCTS_DIR) as "Absolute path"
When i run my app from the terminal (using iphonesim project) i get this error :
dyld: Library not loaded: #rpath/OCMock.framework/Versions/A/OCMock
UIKitApplication:indemnisation[0xb894][26380] Referenced from: /Users/Admin/Library/Application Support/iPhone Simulator/User/Applications/CD5729B5-A674-49B2-91F6-AD398094B6F8/indemINT.app/indemINT
What i dont understand is that the copy build phase just copies the framework files in the same directory as the app.
When i run the app from the command line the framework files are already in the same directory. Does anyone knows why it doesn't work ?
I've also tried to add the OCMock.framework in the following directories (without success) :
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library
/Library/Frameworks
/Users/Admin/Library/Application Support/iPhone Simulator/User/Applications/CD5729B5-A674-49B2-91F6-AD398094B6F8
Thanks in advance,
Vincent.

I just had this same exact error in Xcode 4.2 (4D199) on Lion. I dragged/dropped the OCMock.framework folder into my project and selected the "copy files" checkbox. I saw the above answers and realized I neglected to add a "Copy Files" build phase to move the framework into place. I added one and dragged it right after the compile files build phase and tried to run my tests again. I couldn't get it to work until I had changed the destination in the "Copy Files" phase to the "Products Directory". Using "Frameworks" or "Shared Frameworks" did not work.

The iPhone does not support traditional Mac OS X frameworks. While the iPhone does use folders that end in ".framework", these folders are structured differently than typical Mac OS X frameworks. Most notably, iPhone frameworks are only permitted to use static libraries, while typical Mac OS X frameworks are dynamically loaded. That the message comes from dyld indicates that you are using a shared libary; however, applications targetting the iPhone may only be statically linked.

OCMock is distributed as a framework, and the iPhone does not allow you build your own arbitrary frameworks (there are good reasons to do this on a device with only 128MB of RAM and no swap).
The solution I see in a lot of places around the web is to put OCMock.framework in /Library/Frameworks or anywhere else in the standard framework search paths. This isn’t a very good solution: your build system is now dependent upon the state of your particular machine. Not good.
Another option would be to add the source for OCMock to your unit test target. This would work but is unnecessary, as your tests will never run on the iPhone,2 so why bother building them for ARM? Executables built for the iPhone simulator, being Mac OS X binaries, can link against dynamic libraries just fine. We can use this to our advantage.
What follows is what I believe is the best way to get OCMock working with iPhone projects:
First, add OCMock.framework to your project. Make sure that it’s being added to your Unit Tests target, not your application.
Next, add a Copy Files phase to your Unit Tests target. Set it up like so:
Destination :Absolute Path
Full Path :$(BUILT_PRODUCTS_DIR)
Now, drag OCMock.framework onto your new Copy Files phase to add it to the list of files to be copied.
Finally, drag the Copy Files phase, which I renamed it to “Copy OCMock”, between the Compile Sources and Link Binary With Libraries phases.
That’s it! Run (and by run I mean build) your tests and everything should work correctly.

Related

Convert regular xcode application project to iOSOpenDev

I'm new to jailbroken dev. I have a regular xcode application project. Now I need to convert it, or create a new application project using iOSOpenDev for jailbroken dev. I found the same question:
xcode project conversion to iOSOpenDev
But I have no idea what to do with the first step. Do I need to add some settings or change some values of existed settings? Pleas give some more details.
thanks!
About the first step, I did some tryings and it worked out. The following steps is just the same as kokoabim said:
1.change the install path to /Applications; add iOSOpenDev's include,lib,framework folder path to Framework Search Paths,delete iphonesimulator from Supported Platforms in Build Settings, Header Search Paths, Library Search Paths(I'm not sure if it's necessary);
Add a run script build phase (must be last in order) to existing target that executes /opt/iOSOpenDev/bin/iosod --xcbp.
3.Create the Debian package control file under the existing target's folder as (target's directory)/Package/DEBIAN/control.
4.Perform a Build For Profiling (Cmd-Shift-I).

iPhone application build fails with "dyld: Library not loaded: /System/[...]" error? Why?

For my own reasons and not for the app store I am referencing the Apple private framework Apple80211 in an iPhone app. I got the framework from an earlier version of the iPhone SDK.
I added the "existing framework", verified that that absolute path to the framework is
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/System/Library/PrivateFrameworks/Apple80211.framework
and tried to build the app.
But the build (to device) fails with the error
dyld: Library not loaded: /System/Library/PrivateFrameworks/Apple80211.framework/Apple80211
Referenced from: /var/mobile/Applications/3691587D-87AF-44EA-A505-F73D17F39B3B/iWire2.app/iWire2
Reason: image not found
I cannot figure out why Xcode would look for the library in a /System path (instead of using the path given in the frameworks list) or how to change that behaviour.
I tried the tricks I found here and elsewhere for similar (and identical) sitiations:
Delete the myusername.* files in the xcodeproj bundle
Delete the build directory
Clean all targets
Start new project
Result is always the same.
How can I add this (or a) framework to an iPhone project AND get Xcode to at least look at the library located where I point to rather than a location in /System?
You can try to open project file with text editor, found records for that library, and check which exact path is stored there. Probably, XCode added some defines inside it.

CorePlot: how to run on device? (works OK on simulator)

My app uses CorePlot and runs fine on simulator
(it accesses libCorePlot-CocoaTouch.a from:
/Users/myName/core-plot/build/Debug-iphonesimulator ).
To run on device, xcode expects libCorePlot-CocoaTouch.a in:
/Users/myName/core-plot/build/Debug-iphoneos
but that file is missing, so xcode gives error:
'/Users/Giovanni/core-plot/build/Debug-iphoneos/libCorePlot-CocoaTouch.a: No such file or directory'
I tried generating one by doing:
CorePlot-CocoaTouch.proj > set Xcode to build on 'device' > 'Build And Run'
but 'Build and Run' is grayed out.
Copying the simulator version obviously does not work.
Any ideas?
Consider adding CorePlot-CocoaTouch.xcodeproj as a sub-project and then add libCorePlot-CocoaTouch as a dependency.
Try opening the Core Plot project itself and building the framework first. You should build (not "Build and Run") the CorePlot-CocoaTouch target. Do it for each combination of SDK {simulator, device} and configuration {debug, release} (total of four builds). This will create the .a files you need.
After Core Plot is built, go back to your project and build it. It should now link and run correctly.
Check out my answer here if you don't want to use the CorePlot-CocoaTouch.xcodeproj import method (don't need to import the whole project, only the CorePlotHeader folder and the libCorePlot-CocoaTouch.a library file) Fix linker errors for Core Plot when using Xcode 5
Ultimately you might need to delete the old library by right clicking on your MyApp.app file in the Products folder and copy and past the new CorePlot_1.4 .a file into the Debug-iphoneos folder...clean, build, and it should run on an iOS 7 device.
SOLUTION: the issue was CorePlot-CocoaTouch.xcodeproj must also be added as Targets>MyApp>getInfo>DIRECT dependency (in addition to as subproject and in addition to libCorePlot-CocoaTouch being added as Targets>MyApp>getInfo>LinkedLibraries). Thanks

Can Xcode include application resource files that are generated during the build process?

I have a bunch of content files for my iPhone app that I generate via shell script. It takes way too long to be a part of the Xcode build process, so I run it periodically.
I don't want to have to continually add these files to my Xcode project in order to get them included my app resources folder. Is there a way to get Xcode to copy the contents of a folder into the app resources at build time? (It'd be a bonus if I could specify other locations, such as the approot documents folder.)
I tried adding a new 'Copy Files Build Phase' to my target, but that didn't seem to work. This seems like a common problem, but I couldn't find anything about it here.
-- Edit (What I did)
Per Jasarien and cdespinosa's suggestions, I did the following in a build script. I decided not to copy to the destination, because that would only work when using the simulator, I don't think that'll work when deploying to a device.
cp -rf "$PROJECT_DIR/mystuff" "$CONFIGURATION_BUILD_DIR/$CONTENTS_FOLDER_PATH/"
-- Edit 2
This doesn't appear to get files onto my iPhone. Any ideas?
You can setup a Run Script build phase in your app's target. Write the script so that it copies the resources into the app bundle.
This is the path i use for output:
${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}
Just copy your files in that directory, it's the bundle (simulator or device).
You can also take a look at my answer here : how to set up a Xcode build rule with a variable output file list?

Adding a static library to an iPhone project

The motivation for this question is me trying to get LDAP functions to work with an iPhone application which is a project I'm attempting for part of my dissertation.
When I was developing the application I used the ldap.framework framework that is part of Mac OS X. This works fine in the simulator, but when I try to now get the app on a device it tells me that I'm not allowed to use this framework.
After some research I found that I could build openldap using the arm architecture and add the static library to my application destined for my device.
I eventually managed to compile configure and build openldap by setting variables as mentioned here and using the following commands...
Ade$ ./configure CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-4.0.1 \
LD=$DEVROOT/usr/bin/ld --host=arm-apple-darwin --with-yielding_select=yes
Ade$ make depend
Ade$ make
I was told that the file I'm looking for will have an extension of '.a' so I searched for a '.a' file that mentions ldap...
Ade$ sudo find / -name *ldap*.a
Password:
/Users/Ade/Desktop/openldap-2.4.16/libraries/libldap/.libs/libldap.a
/Users/Ade/Desktop/openldap-2.4.16/libraries/libldap_r/.libs/libldap_r.a
So I assume these are the files I need?
My question is what do I do next? I know I need to add the library to the Xcode project and probably add a load of '.h' files too?
If anyone can give me a pointer to documentation or shed any light on the next stage I would be really grateful.
Many thanks,
Ade
ps. I have also talked about this process on my blog at www.greenpasta.com.
I've done this same thing to build an LDAP client for the iPhoneOS 2.2. You just to drag the .a into the "link with libraries" build stage. I recommend using the regular (non _r) version of the library, unless you specifically need reentrancy on your ldap stuff (which I don't recommend). You can also add the .h's to your project, which is generally the easiest way to access them.
Simply drag the .a files into the Xcode project and choose "copy files into project". I'm not familiar with OpenLDAP but I think the _r version is just a threadsafe version. I would recommend using that and not copying the other. You should probably not copy both files into Xcode or you will get link errors.
Then do the same for the .h files that define the client APIs of OpenLDAP - again I'm not sure which these are but I'm sure you can find out easily.
I would advise organising the .a and .h files together in a Xcode group under resources.
Include the header files in your source and you should be good to go.
You may also need to add -lldap to your linker command (in the build settings pane).