Is there any way to change the dynamic library search path in Xcode - objective-c

Below is my scenario,
In my Application i had to make use of libopus library , i downloaded and install, compile --> install procedure is normal as its for any other open source library,
I linked libopus.a with my application, the way i did is , by default it will get installed in /usr/local/lib, so i drag from there and add it to my application,
Worked fine and no error on my machine,
On Another machine, i was expecting it to be run smoothly as i included this library statically, but its throwing error as
dyld: Library not loaded: /usr/local/lib/libopus.0.dylib
so i concluded, libopus.a somehow including libopus.0.dylib also dynamically,
Now i am able ot add a copy phase in my build setting , so it will get copied in ../Framework folder
if i do otool -L libpus.a then it shows following result
otool -L /usr/local/lib/libopus.a
Archive : /usr/local/lib/libopus.a
/usr/local/lib/libopus.a(bands.o):
/usr/local/lib/libopus.a(celt.o):
/usr/local/lib/libopus.a(cwrs.o):
/usr/local/lib/libopus.a(entcode.o):
/usr/local/lib/libopus.a(entdec.o):
/usr/local/lib/libopus.a(entenc.o):
/usr/local/lib/libopus.a(repacketizer.o):
It doesn't show as its depend upon the dylib library
Now my Question is
How to tell Application to look into this path first
I tried following option,
install_name_tool but it seems it will work on other machine , so the user need to run this script NOT DEVELOPER,
trying to set the some option in the xcode to set the RUNTIME Search path to locate that particular dylib but not getting succeed so far

install_name_tool is run by the developer during the build process, not by the user.
If you're building the library, you should use libtool(1) with the option -install_name #rpath; otherwise, you can use install_name_tool(1) with -id #rpath to do the same thing on the dylib. Then, when you're building your application, set the "Runpath search paths" to the path where you will install the library.
Apple has some good documentation on this in their Mach-O Programming Topics and Dynamic Library Programming Topics.

Related

"Failed to load dynlib/dll" error for libdmtx.dll when running an executable generated by auto-py-to-exe

I am using the library pylibdmtx to generate a data matrix in my python script. Since the library uses the dll libdmtx.dll, I added it to the "Additional Files" (--add-data) section of auto-py-to-exe.
When I run the executable on my computer where I wrote developed the python project, the executable works perfectly but when I transfer the executable to a different computer and try running it there, I get the Failed to load dynlib/dll error.
I checked the temp folder that the executable was creating and using and found libdmtx.dll to be present there. I am not sure why the executable works only on my computer but not the others. Am I missing adding any other dependencies for libdmtx.dll?
Image of the error when the executable is run

cmake always use my pc installed library instead of my target path

I get a project that was built for x86, and I am trying to make it work with mips. But I encounter problem when modifying the CMakelists.txt.
So here is the problem, the following code always use my PC's x86 library:
PKG_CHECK_MODULES(LIBCRYPTO REQUIRED libcrypto)
IF(LIBCRYPTO_FOUND)
INCLUDE_DIRECTORIES(${LIBCRYPTO_INCLUDE_DIRS})
LINK_DIRECTORIES(${LIBCRYPTO_LIB_DIRS})
ENDIF(LIBCRYPTO_FOUND)
I googled and found they always use system library first and can use find_package with NO_CMAKE_SYSTEM_PATH flag. But its not working and give me the following message. I don't know what its talking about..
Could not find a package configuration file provided by "libcrypto" with
any of the following names:
libcryptoConfig.cmake
libcrypto-config.cmake
Add the installation prefix of "libcrypto" to CMAKE_PREFIX_PATH or set
"libcrypto_DIR" to a directory containing one of the above files. If
"libcrypto" provides a separate development package or SDK, be sure it has
been installed.
My question is how to properly link a library to the path I assigned to and works like original piece of code?

Debugging QuickLook plug-in with 'bundle is damaged' error

We're adding a QuickLook plug-in to our project.
Everything is fine until macOS trying to invoke our plug-in, at which point we're getting the beloved The bundle couldn’t be loaded because it is damaged or missing necessary resources error.
We've checked with otool -L on the plug-in's binary that all dependencies are in place, however as soon as the OS is asking our plug-in for a preview for the file type supported by us we get:
22/04/17 12:03:05,716 quicklookd[55323]:
[QL] Can't load plug-in at file:///Users/myname/Library/Developer/Xcode/DerivedData/The_Project-gpihzjouhxvifqcslmywktktizer/Build/Products/Debug/MyApp.app/Contents/Library/QuickLook/SomeQuickLookPlugIn.qlgenerator/:
The bundle “SomeQuickLookPlugIn” couldn’t be loaded because it is damaged or missing necessary resources.
The one thing we're not quite sure about is the dependency to our internal frameworks.
We've set up the plug-in similar to our main app, i.e. the private framework dependency resolves to:
#executable_path/../Frameworks/MyFW.framework/Versions/A/MyFW (compatibility version 1.0.0, current version 1.0.0)
..which would work OK if #executable_path were either the main app's binary or the plug-in's main binary as we copied the frameworks in both places in the bundle.
Any thoughts?
Ideally we would like the OS to tell us which dependency it failed to resolve -
is there any debug flag that can be set..?
As per https://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html and http://www.dribin.org/dave/blog/archives/2009/11/15/rpath/ you should
set the Installation Directory for your referenced framework(s) to #rpath
in the app set Runtime Search Paths to #loader_path/../Frameworks
and in the QuickLook plug-in set Runtime Search Paths to #loader_path/../../../../../Frameworks as suggested by catlan -
that way you don't need to duplicate referenced frameworks inside the QuickLook plug-in
Compile, run, and everything should just work if everything else is set up correctly.
In addition you might want to check the code-signing settings in your plug-in to make sure there's no problems there.
One thing you can do is remove (or turn-off) code signing from your app and then see if it will load the plug-in…
To check if code-signing is the problem you can turn it off temporarily for your app using the Terminal to codesign --remove-signature YourApp.app and see if it works..
Run Search Paths should be #loader_path/../../../../../Frameworks because it is installed into Main.app/Contents/Library/QuickLook/QuickLookPlugin.qlgenerator/Contents/Mac/QuickLookPlugin, so we need to go five folders down from the #loader_path to find the frameworks folder.

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

How do I use a dynamically load library in a command line utility? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
using frameworks in a command line tool
Hey,
I've written a command line 'foundation tool' that uses the RegexKit.framework extensively. Everything works when run in Xcode but if I compile the release build and try to run it in Terminal I get the following error:
dyld: Library not loaded: #executable_path/../Frameworks/RegexKit.framework/Versions/A/RegexKit
Closer inspection reveals that the RegexKit.framework bundle is sat in the same directory as my executable file... I've done some research and I'm thinking that as command line tools don't use application bundles there's no where for Xcode to copy the framework to. So I'm guessing that I need to compile the framework as a static library and include it in my code... am I right? If so, how do I go about doing this? Is there anything I can do in Terminal to point to the framework externally?
Any help would be very greatly received, I've been banging my head against this for a few days now!
Thanks in advance,
Tom
So... What I did in the end was to recompile the framework with a different Installation Directory (in the Deployment section, under the Build tab in the Target's Info) - I set it to just #executable_path.
I then compiled the framework and replaced the one in my Utilitie's project, I also changed the Copy Files build phase to copy the framework to "Executables" rather than Frameworks.
The good news is that this fixes my original problem - but obviously the framework has to be in the same directory as the executable.
So this got me unstuck but I'd still love to know how to compile RegexKit.framework statically!
You shouldn't be installing the framework in the Executable folder of your bundle. It should be in the Frameworks folder. You need a Copy Files phase in your project that copies the framework and you need to set the Destination to "Frameworks". "Copy only when installing" should be unchecked.
When testing this, you should make sure you perform a clean build. I typically delete the build folder rather than using Xcode's Clean menu option since it's quicker and more comprehensive.
Also: you cannot statically link to a framework. If you want to statically link to something, it needs to be a static library so in this case, you'd need to hack about with RegexKit. Bear in mind that static libraries cannot contain resources, whereas Frameworks, being bundles, can.