dyld: Library not loaded: But Library seems entirely unrelated - linker

I have an interesting issue: I have a C++ project (built with CMake/make) and probably some dependency I had compiled from source had their dependency updated via brew upgrade. Now, when I make, I get
dyld: Library not loaded: /usr/local/opt/x265/lib/libx265.179.dylib
Referenced from: /usr/local/opt/ffmpeg/lib/libavcodec.58.dylib
Reason: image not found
Abort trap: 6
However, the application does not use ffmpeg, libavcodec or similar directly. It does use OpenCV (which may link against those, I don't know), and the Spinnaker SDK for FLIR cameras (which depends on libavcodec).
Question: What command line tools can I use, to recursively search through the linked libraries for a binary to find the offending (missing) library? I ran otool -L on my binary, but it does not directly link against the offending libs.

Related

Where is freeglut.a in cygwin?

I'm trying to implement a little OpenGL renderer in VSCode with cygwin's gcc, using freeglut as windowing system and I'm failing linking against freeglut. I have no clue what the library is called, according to what I found on the web, I should be able to link with -lfreeglut, but this gives me a the following:
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lfreeglut
collect2: error: ld returned 1 exit status
I searched my cygwin folder for "glut" and found many things, but no library. Funny enough the headers are present. Btw, I installed the freeglut package (mingw64-x86_64-freeglut v 2.8.1-1) with cygwin's package installer. Also, I'm completely unexperienced in GNU systems, so the answer may be quite straightforward.
I think was never -lfreeglut but always -lglut
Try compiling with the normal GLUT library package
libglut-devel: OpenGL Utility Toolkit library
it includes the shared library
usr/lib/libglut.dll.a
both headers
usr/include/GL/freeglut.h
usr/include/GL/freeglut_ext.h
usr/include/GL/freeglut_std.h
usr/include/GL/freeglut_ucall.h
usr/include/GL/glut.h
In general you can use cygcheck for finding the needed package.
$ cygcheck -p libglut.dll.a
Found 5 matches for libglut.dll.a
libglut-devel-2.8.1-1 - libglut-devel: OpenGL Utility Toolkit librar
y (installed binaries and support files)
libglut-devel-3.0.0-1 - libglut-devel: OpenGL Utility Toolkit librar
y
libglut-devel-3.2.1-1 - libglut-devel: OpenGL Utility Toolkit librar
y
...

Compiling in a dynamic C library (dylib) into a program on OS X

I've written a little C program which uses libusb. Now I want to distribute this program to "normal" (not dev) Mac OS X computers. But when I ported the compiled file to a test machine I got the following error:
dyld: Library not loaded: /opt/local/lib/libusb-0.1.4.dylib
Referenced from: /Users/kitty/myprogram
Reason: image not found
Trace/BPT trap: 5
When I copy the files (works only with all the files)
/opt/local/lib/libusb-0.1.4.dylib /opt/local/lib/libusb-1.0.a
/opt/local/lib/libusb.a
/opt/local/lib/libusb-1.0.0.dylib /opt/local/lib/libusb-1.0.dylib
/opt/local/lib/libusb.dylib
from my machine to the target machine the program works flawlessly.
But I really want to create or compile everything into a single executable. How is this possible?
Using -static while compiling does not work since not all libraries can be compiled into the final app statically (see this SO question here).
So how can I make a single neat little application file?
You can convert a static library to a dynamic library, but I'm not aware of a way to do the reverse as you want it.
If you're building an app with a bundle, you need to put the library you want to distribute inside your bundle, in the Frameworks directory, and link against that.
If you are not building a bundle-based app, just a single binary, you may need to provide instructions for your users on how to install the library on their system (e.g. via Homebrew).
Here's how you do it for bundle-based apps:
Apple has a document about run-path dependent libraries but doesn't actually explain how to set this up for a newbie.
Here's how it should work:
Add the libusb.dylib you want to use to your project.
It should automatically get added to your "Link Binary with Libraries" phase in your project's "Build Phases". If not, add it here.
Add a new "Copy Files" build phase.
In the "Destination" drop-down box, select "Frameworks". This is the
Frameworks directory in your app's final bundle.
Then press the "+" icon in that copy build phase and add your library.
If you had any manual linking options like -L/usr/local/lib and -lusb, remove them.
Clean and build.
When you now look into your app bundle, you'll see that the library is copied to <bundle_path>/Contents/Frameworks/. You can now start the app from wherever you want, the dynamic link loader knows it needs to look at <path_to_binary>/../Frameworks/ to find your library.
But: you may need to rebuild your libusb to have the install_name set to #rpath/../Frameworks/libusb.dylib or use the install_name_tool CLI tool fix that path for your copy of libusb.dylib that you added to your project.

Link error using old libraries

I am trying to use third-party libraries (.a) , for which I dont have the source code, that worked in an older app. I imported the older app into Xcode 7 and built and ran it successfully. The libraries were dependent on system libraries which had extension .dylib The program still worked.
I then tried to build a new app using the same libraries. However, I had to replace the system libraries with their .tbd equivalents. When I issued the build command, I got link errors such as shown below where the library name is libavutil.a
ld: library not found for -lavutil
clang: error: linker command failed with exit code 1 (use -v to see invocation
I read in stack overflow posts about the need to add to the 'other linker flags' in the build settings.
My questions are:
Do I need to add flags (to Debug and Release) such as lavutil for libavutil.a or flags for the system libraries -lc++ for libc++?
Am I on the right track? Or is there some other solution to the
linker errors.
Reading through some SO posts on similar linker errors encountered by others, it was suggested that instead of adding a -lavutil like statement to the other linker flags, I could drag and drop the libraries into the window that opens up for multiple values. This worked well. Drag and drop maintains the right relative directory structure and removes the ambiguity that the linker may encounter to find the libraries. The linker errors were eliminated and I could execute the program.
In summary, I switched to Xcode 7, changed target to 9.2, dragged my third-party libraries, and built and ran.

Unable to load shared library: undefined symbols

I have my own program with plugins (dynamic shared libraries) on a linux (ubuntu) system. My libraries (plugins) use OpenCV (maybe not so important).
My plugins are in /usr/local/lib/mysoft/.
I have compiled my program successfully even with libraries, successfully installed so everything seems to be OK up to this point.
When I run my program, it loads a bunch of these libraries based on some configuration file. I have several libraries which are loaded successfully but I cannot load one library. It gives me error when loading (used dlopen() to open the library):
/usr/local/lib/mysoft/libMyPlugin2.so: undefined symbol: _ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi
Segmentation fault (core dumped)
Tue Nov 20 19:11:29 CET 2012
It obviously has some problems to find cv::resize which is part of OpenCV but I don't understand why.
I checked following things:
OpenCV is probably correctly installed since other libraries use it as well and are loaded without problems
no dependencies of my program, libMyPlugin2.so or OpenCV are missing (checked with ldd)
Architecture of all libraries and binaries seems to be the same (I checked it with objdump -f)
Does anybody have an idea what am I doing wrong?
This post seems to be so relevant but still didn't help:
Linux shared library that uses a shared library undefined symbol
Well I found the problem, hopefully it can help others...
The problem - I was missing one OpenCV library when compiling. So I replaced "opencv_core opencv_highgui" by "opencv_core opencv_imgproc opencv_highgui" and everything works.
So although I was able to compile it one of the dependencies was missing - I guess something has changed in OpenCV cause these sources worked perfectly (even with build) with older versions of the OpenCV.

How do I use "unity" to unit test C code on Mac (Lion)?

Let me start out by saying that I'm not a C developer and I know very little about actually writing real world C code. I've been doing some research to find a xUnit framework that I can use to write tests for C code and based on what I've found it seems like Unity is the one that I want to go with. It seems simple enough, but I really just don't know what to do after I download the zip file from Unity's website. It doesn't seem to have the normal configure/make/make install, and if it did, I'm not sure that is what I should be using anyway. It does, however, ship with some rake tasks, but none of those seemed to be any kind of "install" task. As a last resort I tried to just copy the 3 source files in with my code (which I really hope is not the right thing to do), but when I try that I get an error trying to compile my c file with gcc, but I think this should be working. Here is my set up:
src/
mycode.c
unity.c
unity.h
unity_internals.h
Here is the source for mycode.c
/* mycode.c */
#include "unity.h"
void test_sample(void)
{
TEST_ASSERT_EQUAL_INT(0, 0);
}
When I run gcc mycode.c I get:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
"_UnityAssertEqualNumber", referenced from:
_test_sample in ccyHByv6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
(I get a similar error when I try to compile unity.c with gcc). Which I assume means that the code that ships with unity requires a different compiler than what I have which is:
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)
or that maybe unity is not compatible with a 64 bit processor... (I'm running on Mac OS 10.7.3 with a 2.4 GHz Intel Core 2 Duo processor - another thing that may or may not be relavent is that I've got XCode Version 4.3 (4E109) and also Command Line Tools for XCode) At this point I'm just grasping at straws and I'm in way over my head.
My question is, what is the correct process to go through to take a 3rd party C library, such as Unity, and make it available to my C code? Do I need to install something like in Python or Ruby or add something to my path like in Java or something else? Shouldn't just dropping unity's code in with mine work? Am I doing something wrong or is Unity or both? I really just want to be able to test drive C code using Unity. Any help would be greatly appreciated. Thanks in advance!
First, try 'gcc *.c -o mytest'. This will compile all of the C source files into object files, and then link them together into the binary 'mytest'. Keep in mind that all C source files have to be compiled to object files before they can be linked together. (A library is just a bunch of packaged object files.)
If you had a unity library installed in /usr/lib, you could do something like 'gcc mycode.c -lunity -o mytest'. If you had a unity library sitting in the current directory, you might do 'gcc mycode.c ./unity.a -o mytest'. This tells the compiler to look for a file named 'unity.a' in the current directory. Some libraries build .so files ('shared object' files, similar to DLLs in Windows). Replacing 'unity.a' with 'unity.so' should work if that is the case. (I'm assuming a Unix/Linux environment here.)
As an alternative to Unity, look at Google Test, which can be used with C code. I know it is supported on the Mac as well. The primary benefit is a large and active community. More information on Google Test from another SO question: Is Google Test OK for testing C code?
I figured out my problem. It turns out that unity requires you to define a setup and a teardown function and if you do not, you will get errors similar to the one that I was running into.

Resources