How to run objective-c console application (with a library) outside of xcode? - objective-c

I have a simple console application intended to exercise a library from a larger project. The console application is one target within an XCode project that also contains the library ( plus some other targets I don't care about right now). The console target references the library by listing it in the Build Phases "Target Dependencies" and "Link Binary with Libraries".
I can run the console program just fine from within XCode by clicking the "Run" button and examining the console output under "All Output".
However I get a load error if I try to run outside of XCode. I select the console application under "Products" in the project navigator, and then I locate the "Full Path" in the "Identity" section of the information panel. When I copy this path to a terminal window and run it, I get an error like:
dyld: Library not loaded:
#executable_path/../Frameworks/[LIBRARY].framework/Versions/A/[LIBRARY]
Referenced from: [THE PATH THAT I TYPED]
Reason: image not found
What should I do differently to get my console application to execute from the console?
(Note, in the "Copy Files" phase, I have tried leaving the "Subpath" blank and setting it to "#executable_path/../Frameworks" per this answer, which was about a cocoa app, but neither way has helped.)

As the commenters above explain, dyld is the dynamic loader and it is looking for my library at the path it printed. (When it says #executable_path, it means the location of the console application I'm actually trying to run.)
Looking at the compiled output from XCode, I see a folder hierarchy like this:
Debug/
MyProgram
Frameworks/[LIBRARY].framework/Versions/A/[LIBRARY]
For whatever reason, this structure does not match what the loader wants -- it is looking for the library at ../Frameworks rather than ./Frameworks.
Rather than messing with the environment variables used by the loader or trying to tweak the program itself, I just copied these files a new folder with the structure specified in the error message:
foo/
bin/MyProgram
Frameworks/[LIBRARY].framework/Versions/A/[LIBRARY]
I can now run foo/bin/MyProgram from the terminal without any problem.

Related

How to link a stand-alone Swift OS X app to a private framework (also created in Swift)?

I get the following error when trying to embed a private framework into a Swift OS X app:
dyld: Library not loaded: #rpath/CustomCalcs.framework/Versions/A/CustomCalcs
Referenced from: /Users/sam/Library/Developer/Xcode/DerivedData/TestFramework-ckcpduuqyfclssceomyzqbxgbjdi/Build/Products/Debug/TestFramework.app/Contents/MacOS/TestFramework
Reason: image not found
I am trying to simulate the scenario where I am the receiver of a .framework file from a 3rd party and don't have access to the source code or Xcode project file (even though in my simple test of course I do)
Steps I have taken:
1. Add file to my target project making sure to copy it into the project
2. The Xcode syntax highlighting/completion then becomes aware of the classes within the framework - this is good
3. I add a "copy file" step in the Xcode "build phase". I have added it to "frameworks"
4. When examining the directory that the app runs from within DerivedData, I can find no framework directory and no .framework file
5. When I run the app the error above occurs
It would appear that I need to setup a step whereby the .framework file is copied into a deployment directory AND somehow the app knows to look in this location. How specifically do I go about doing this in Xcode?
I may be wrong but is seems like a case of "Linked Frameworks and Libraries" vs "Embedded Binaries".
Click on your project Workspace
Select your project Target
In the General tab, remove the Framework from "Linked Frameworks and Libraries"
Add it to "Embedded Binaries"
Build
It will be automatically added to the "Linked Frameworks and Libraries" also but you'll have it once instead of two times.

RNCryptor Dyld Error Message: Library not loaded

So, I've tired everything to get this to work after I've archived the application, but it keeps failing to open with the error message:
Dyld Error Message:
Library not loaded: /Library/Frameworks/RNCryptor.framework/Versions/A/RNCryptor
Referenced from: /Users/USER/Desktop/Project.app/Contents/MacOS/Project
Reason: image not found
I've added the Framework to the Copy Files Build Phase, I've changed the Runpath Search Paths to #loader_path/../Frameworks and the Framework Search Path to #executable_path/../Frameworks and it still doesn't want to open.
It opens fine in Xcode itself, but as soon as it's archived, it complains and doesn't run.
Any help is much appreciated :)
Ok, so I was able to fix it.
So the application was actually looking for the Framework in /Library/Frameworks/ on my Mac, I originally had thought it was looking for a reference in my actual .app file.
To fix it I opened up Terminal, cd' into the folder containing a copy of RNCryptor.framework and used this command
install_name_tool -id #executable_path/../Frameworks/RNCryptor.framework/RNCryptor RNCryptor.framework/RNCryptor
Then removed the old framework from the Xcode Project and re-inserted it using the Framework I just used from the directory I cd' into, and added the new Framework to the Copy Files Build Phase.
And voila, it worked :)

How to add framework to xcode

I am new to Xcode and want to add and use framework which I have taken from internet but
when I add it to my products folder by right clicking on products and using add new files it
runs fine and when i use my project's project.app file on my mac or on any other mac it
gives me dyld error that the bundle image not find. And when I use bundle image in my
framework folder its give me ld error that bundle not found help please.
ld: framework not found SMS-Bundled
Command /Developer/usr/bin/clang failed with exit code 1
*updated
when i add my framework to framework folder now its giving me error at run time...
dyld: Library not loaded: #executable_path/../Frameworks/SMS-Bundled.framework/Versions/A/SMS-Bundled
Referenced from: /Users/usman/Library/Developer/Xcode/DerivedData/Serial_Tools-detoutxzbnjrngcuulrbgcasxmjs/Build/Products/Debug/Serial Tools.app/Contents/MacOS/Serial Tools
Reason: image not found
(In Finder)
Go to the folder where your custom framework is. copy the framework. Now go to your project's folder. Create a new folder called Frameworks, paste the custom framework inside this folder.
(In Xcode)
Drag the custom folder FROM finder to the Frameworks folder on the project explorer (where all the rest of the frameworks are)
Now select your project, go to Build Settings, scroll down to "Framework Search Paths" field, double click the value to open it, press the + to add a new path, write ./Frameworks as the new path.
From now on if you want to add other private/custom frameworks all you have to do is copy them into the created frameworks folder, and simply drag and drop it into the xcode frameworks folder. (You must drag it from the project framework folder in finder to the framework folder in xcode).
PD: You don't necessarily have to name this folder frameworks, it can be whatever, just make sure to change the Framework Search Paths accordingly.
In your project build setting, under "framework search paths" mention the path of the framework.
This will resolve your issue.

Xcode 4 adding dylib

I am trying to create and then add the dylib to a project.
I created it by using the "Cocoa-Library" template and setting the type to "Dynamic" (not sure if it should be dynamic or static...). Then I created a simple obj-c class called Test and wrote a method in it that prints out something to console.
I compiled and used the generated .dylib file and put it in another project. Now whenever I try to use it, I get this message on runtime"
dyld: Library not loaded: /usr/local/lib/TESTLib.dylib
Referenced from: /Users/***/Library/Developer/Xcode/DerivedData/TestingDYLIB-axmoocnxbwznoyerfogosumqufxc/Build/Products/Debug/TestingDYLIB.app/Contents/MacOS/TestingDYLIB
Reason: image not found
I also created a Copy File phase and set the destination to "Frameworks". I still get the same error.
What am I doing wrong?
The issue is not where Xcode is looking for the library at compile time, which is what Simon Whitaker's answer addresses.
The issue is that the application which uses the dylib cannot find it at runtime. When an application is built that uses a dynamic library, it copies the install_name of the dylib into the output binary.
Creating a copy files phase and copying the dylib to the Frameworks subdirectory of the app's bundle is the right thing do do.
However, you need to do an additional step. You need to compile the dynamic library with an install_name appropriate for a bundle app. By default, a dynamic library is created with an install_name of /usr/local/lib. The app can't find your library there because it doesn't exist. Even if you put the library there, your users certainly won't have it, so that would be the wrong solution.
The right solution is bundling the library with the app. To set the install name for the dynamic library, go into the dynamic library project and set the "Dynamic Library Install Name" option to: #executable_path/../Frameworks/libmydynamiclibrary.dylib
Xcode is looking in /usr/local/lib/ for the library. If the library is in another location, add that location to your Library Search Paths:
Select project file in Xcode 4
Select the target, then click the Build Settings tab
Make sure All is selected in the filter bar (not Basic)
Scroll down to the Search Paths section and you'll find Library Search Paths in there

dyld not loaded problem xcode 4

I dragged skype fromwork into my frameworks folder and then imported <Skype/Skype.h>. When I run, it gives me this error:
dyld: Library not loaded: #executable_path/../Frameworks/Skype.framework/Versions/A/Skype
Referenced from: /Users/test/Library/Developer/Xcode/DerivedData/TEST-edktdfqvxhqmfpfqrdqrmjojnfly/Build/Products/Debug/TEST.app/Contents/MacOS/TEST
Reason: image not found
Try setting the "Library Search Path" build settings for your project.
In Xcode 4.x:
You need to drag the .framework from the Project navigator to the Copy Files build phase.
In Xcode 3.2.x:
Choose Project > New Build Phase > New Copy Files Build Phase
In the Destination drop down box select Frameworks
Close the Copy Files Phase Info window to return to the Xcode main window
In the Groups & Files pane expand Targets > YourApplicationExectuable, where you should now see a Copy Files entry
Still in Groups & Files pane drag your framework into the Copy Files entry
Recompile
If you don't do this, the framework will not be actually copied into your application bundle, so it will not be found when your app tries loading it into memory.
Just as a side note, I spent months trying to track down an answer to this problem, always seeing this answer but never being able to get it to work.
MY MISTAKE:
The "Copy Files" build phase is not shown by default. I was seeing the "Copy Bundle Resources" phase and wrongly assuming that was where I needed to list the framework.
At the bottom of the Build Phases pane click on Add Build Phase. "Copy Files" is listed there.
Doesn't work for me. I had to make a link to the framework in the ~/Library/Developer/..../Debug/../Frameworks for it to work.