Using Shared Object Files - c++

I installed opencv 2.4.3 on ubuntu 12.04 using cmake. At the time of installation, .so files were put in /usr/local/lib directory. When I compile my code using the following line in terminal using the default location of .so files (i.e. /usr/local/lib) then it works fine
g++ fileName.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc
I am trying to experiment with the shared object files. I copied the required shared object files from /usr/local/bin and put them in a new folder location /home/nishant/Desktop/lib. I copied the following files and their respective .so.2.4 and .so.2.4.3 files:
1) libopencv_core.so
2) libopencv_highgui.so
3) libopencv_imgproc.so
Then I changed there names to libcore.so , libhighgui.so and libimgproc.so respectively. I changed their name so that the old .so files should not be used from /usr/local/lib. Now when I try compiling my code using the shared object files in the new folder location then I get the following error:
Terminal Command:
g++ filename.cpp -L/home/nishant/Desktop/lib -lcore -lhighgui -limgproc
Error:
/usr/bin/ld: cannot find -lcore
/usr/bin/ld: cannot find -lhighgui
/usr/bin/ld: cannot find -limgproc
collect2: ld returned 1 exit status
It is interesting to note that the following works:
g++ filename.cpp -L/home/nishant/Desktop/lib -lopencv_core -lopencv_highgui -lopencv_imgproc
Reason: The .so files from /usr/local/lib are being used.
My Question : How can I use the .so files from the new folder location to make my code work.

I see three ways:
1.
Please update your LD_LIBRARY_PATH - add path to your libraries before running g++.
2.
Please add the directory where your libraries are located to /etc/ld.so.conf and run:
sudo ldconfig
Then run g++.
3.
Please create a new file: /etc/ld.so.conf.d/local.conf and add path do the directories with your libs to it and run:
sudo ldconfig
Then run g++.

Related

Error loading shared libraries after cross compiling: No such file or directory

I'm having problems to load share libraries after cross-compiling my C++ code using Docker Buildx, having a Raspberry Pi Zero W as the target.
After I perform the build, I copy the generated binary to a Raspberry Pi Zero already running and with OpenCV4 installed.
When I run the executable, the following error message is shown:
pi#raspberrypi:/mnt/system/$ ./software.run
./software.run: error while loading shared libraries: libopencv_freetype.so.4.2: cannot open shared object file: No such file or directory
Despite OpenCV4 being already installed, this particular lib wasn't in the /usr/lib. So, I copied it, run sudo ldconfig but, even after this procedure, my software still cannot find the lib.
I even added the /usr/lib to the path of the system, but, it didn't work.
pi#raspberrypi:/usr/lib $ sudo ldconfig -v | grep libopencv_free
ldconfig: Can't stat /usr/local/lib/arm-linux-gnueabihf: No such file or directory
ldconfig: Path `/lib/arm-linux-gnueabihf' given more than once
ldconfig: Path `/usr/lib/arm-linux-gnueabihf' given more than once
ldconfig: /lib/arm-linux-gnueabihf/ld-2.28.so is the dynamic linker, ignoring
ldconfig: /lib/ld-linux.so.3 is the dynamic linker, ignoring
libopencv_freetype.so.4.2 -> libopencv_freetype.so.4.2.0
pi#raspberrypi:/mnt/system/ $ file software.run
software.run: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=409a24c10761e2b9fd7a310cddfb09c86fb3a207, not stripped
pi#raspberrypi:/mnt/system $ echo $LD_LIBRARY_PATH
/usr/lib
Makefile:
CC = g++
STD = --std=c++14
SOFTWARE_SRC = $(wildcard src/software/*.cpp)
SOFTWARE_BIN = software.run
CV_LIBS = $(shell pkg-config --cflags --libs opencv4)
SOFTWARE_INC = -Iinclude -I/usr/include -I/usr/local/include
SOFTWARE_LDFLAGS = -lraspicam_cv -L/opt/vc/lib -lmmal -lmmal_core -lmmal_util -lwiringPi
all: software-out
software-out:
$(CC) $(STD) $(SOFTWARE_SRC) -o $(SOFTWARE_BIN) $(CV_LIBS) $(SOFTWARE_INC) $(SOFTWARE_LDFLAGS)
Other software that also uses OpenCV is working properly.
I also build a "Hello World" software just to validate my cross-compiling environment and it is working.
Thank you all in advance
EDIT
After several attempts, I was able to fix the problem by building the libs locally.
I couldn't identify what caused it, but, the libs generated by the Buildx environment weren't working properly in the Raspberry Pi Zero.
I'm building a truly cross-compiling environment to address this issue.

libopencv_highgui.so.4.4: cannot open shared object file: No such file or directory

I am compiling my code with
g++ -std=c++11 -I /usr/local/include/opencv4/ recordVideo.cpp -L /usr/local/lib -lopencv_objdetect -lopencv_features2d -lopencv_imgproc -lopencv_highgui -lopencv_core -lopencv_videoio
I get error
error while loading shared libraries: libopencv_highgui.so.4.4: cannot open shared object file: No such file or directory
But it is present in /usr/local/lib
as libopencv_highgui.so.4.4
libopencv_highgui.so.4.4.0
solved using link https://github.com/cggos/dip_cvqt/issues/1
Find the folder containing the shared library libopencv_core.so.3.2 using the following command line.
sudo find / -name "libopencv_core.so.3.2*"
Then I got the result: /usr/local/lib/libopencv_core.so.3.2.
2. Create a file called /etc/ld.so.conf.d/opencv.conf and write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/ to my opencv.conf file.
3. Run the command line as follows.
sudo ldconfig -v
Try to run the test binary again.

MacOS: g++ cross-compiler cannot find ld from dmg image

When I compile the sources from just a directory it's OK. But if I do it from dmg image I got this:
$ make
"/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/i386-pokysdk-darwin/usr/bin/i586-poky-linux/i586-poky-linux-g++" -std=c++11 -Wall -O2 --sysroot="/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux" -c -MD main.cpp -I/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux/usr/include/c++/4.9.1 -I/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux/usr/include/c++/4.9.1/i586-poky-linux -o main.o
"/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/i386-pokysdk-darwin/usr/bin/i586-poky-linux/i586-poky-linux-g++" -std=c++11 -Wall -O2 --sysroot="/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux" main.o -I/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux/usr/include/c++/4.9.1 -I/Volumes/:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp/myApp.app/Contents/MacOS/devkit-x86/sysroots/core2-32-poky-linux/usr/include/c++/4.9.1/i586-poky-linux -lopencv_core -lopencv_highgui -lopencv_imgproc -lzmq -o aaa.bin
collect2: fatal error: cannot find 'ld'
compilation terminated.
make: *** [aaa] Error 1
After this error I unpack contents from *.dmg then run it and compilation completes successful.
Why and what I need to do to make it work from dmg?
My *.dmg contains an application and a cross-compiler. Source files for compilation are outside and created by this application. It is simple IDE.
Finaly I find out the cause of this problem. It is two bug-or-features one in macdeployqt and one in g++.
When I wrote my building script I thought it will be good to get dmg image in same directory from which I run my script. And I added a command with absolute path:
macdeployqt /Users/me/develop/macos_build_script/myApp/myApp_Release/myApp/myApp.app -dmg
The image was built and my app ran well. But. Silently macdeployqt made a name for dmg mount point from absolute path. The absolute path became a single directory name with colons instead slashes:
:Users:me:develop:macos_build_script:myApp:myApp_Release:myApp. The root of dmg did not contain this directory, it only appears when dmg image is mounted.
Then this directory with colons will be part of variables of makefile.
Cross-compilator i586-poky-linux-g++ builds an object files when paths with colons present. And I thought it's all ok with g++ if it using this coloned paths for object files. I thought the cause somwhere in other place. But I was wrong.
Backslashing of colons din't help.
In some moment I builded the dmg image by hands from a directory in which myApp.app bundle subdirectory placed with this short command:
macdeployqt myApp.app -dmg
And the problem is gone. The mount point of dmg image now is just /Volumes/myApp. I do nothing except jump to dir where myApp.app is and making the relative path.

eclipse CDT: searching /usr/lib although not in lib search path

I am trying to run a bit of pcl code (from their website). I am currently, on ubuntu, using boost 1_51 (that I built) and in my Eclipse C++ Build->Settings I have
/home/aly/libs/boost_1_51_0/stage/include in the include path and /home/aly/libs/boost_1_51_0/stage/lib in the library search path. I do not have /usr/include or /usr/lib.
At runtime I am getting some boost error, which made me think perhaps it is not running against the correct libraries as boost_1_46 is built in /usr/lib (this is the most up to date version in the ubuntu repos). So I removed a lib file (libboost_timer.so) from the /usr/lib dir and recompiled (which was fine) and then ran. At runtime I got the following error:
error while loading shared libraries: libboost_thread.so.1.46.1: cannot open shared object file: N
My question is, why if /usr/include and /usr/lib aren't in my include path and library search paths, is my program looking for the 1.46 version?
Additional info:
To compile eclipse is doing:
Invoking: Cross G++ Linker
g++ -L/home/aly/libs/OpenCV-2.4.3/release/lib -L/home/aly/libs/boost_1_51_0/stage/lib -o "3DObjectDetection" ./src/HFNodeSplitCalculator.o ./src/HFNodeUtils.o ./src/HFTreeNode.o ./src/HoughForest.o ./src/ImagePatch.o ./src/Main.o ./src/PatchGenerator.o ./src/utils.o -lvtkmetaio -lvtkImaging -lvtkIO -lvtkViews -lvtkVolumeRendering -lvtkalglib -lvtkDICOMParser -lvtkInfovis -lvtkFiltering -lvtkGeovis -lGL -lopencv_core -lopencv_highgui -lopencv_imgproc -lpcl_io -lpcl_visualization -lpcl_common -lpcl_surface -lpcl_kdtree -lpcl_search -lpcl_geometry -lpcl_features -lvtkproj4 -lvtkParallel -lvtksys -lvtkRendering -lvtkCommon -lboost_serialization -lboost_timer -lpthread -lboost_context -lboost_date_time -lboost_system -lboost_thread -lboost_filesystem -lboost_random -lboost_regex -lglut -lvtkexoIIc -lvtkCharts -lvtkGenericFiltering
Finished building target: 3DObjectDetection
And my $LD_LIBRARY_PATH is
:/home/aly/libs/boost_1_51_0/stage/lib/:/home/aly/libs/OpenCV-2.4.2/build/lib/:/usr/lib/:/home/aly/libs/cuda-5.0/lib:/home/aly/libs/cuda-5.0/lib64
As you can see the boost lib dir appears before /usr/lib

cannot open shared object file: No such file or directory

I met the share library not found on the head node of a cluster with torch. I have built the library as well as specify the correct path of the library while compiling my own program "absurdity" by g++. So it looks strange to me. Any idea? Thanks and regards!
[tim#user1 release]$ make
...
...
g++ -pipe -W -Wall -fopenmp -ggdb3 -O2 -I/home/tim/program_files/ICMCluster/ann_1.1.1/include -I/home/tim/program_files/ICMCluster/libsvm-2.89 -I/home/tim/program_files/ICMCluster/svm_light -o absurdity xxxxxx.o -L/home/tim/program_files/ICMCluster/ann_1.1.1/release/lib -L/home/tim/program_files/ICMCluster/libsvm-2.89/release/lib -L/home/tim/program_files/ICMCluster/svm_light/release/lib -lm -ljpeg -lpng -lz -lANN -lpthread -lsvm -lsvmlight
[tim#user1 release]$ ./absurdity
./absurdity: error while loading shared libraries: libsvmlight.so: cannot open shared object file: No such file or directory
[tim#user1 release]$ ls /home/tim/program_files/ICMCluster/svm_light/release/lib/libsvmlight.so -l
-rwxr-xr-x 1 tim Brown 121407 Jan 31 12:14 /home/tim/program_files/ICMCluster/svm_light/release/lib/libsvmlight.so
[tim#user1 release]$ LD_LIBRARY_PATH= /home/tim/program_files/ICMCluster/svm_light/release/lib:$LD_LIBRARY_PAT
[tim#user1 release]$ export LD_LIBRARY_PATH
[tim#user1 release]$ ./absurdity
./absurdity: error while loading shared libraries: libsvmlight.so: cannot open shared object file: No such file or directory
[tim#user1 release]$ ls /home/tim/program_files/ICMCluster/svm_light/release/lib
libsvmlight.a libsvmlight.so
Copied from my answer here: https://stackoverflow.com/a/9368199/485088
Run ldconfig as root to update the cache - if that still doesn't help, you need to add the path to the file ld.so.conf (just type it in on its own line) or better yet, add the entry to a new file (easier to delete) in directory ld.so.conf.d.
Your LD_LIBRARY_PATH doesn't include the path to libsvmlight.so.
$ export LD_LIBRARY_PATH=/home/tim/program_files/ICMCluster/svm_light/release/lib:$LD_LIBRARY_PATH
sudo ldconfig
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib).
Generally package manager takes care of this while installing the new library, but not always (specially when you install library with cmake).
And if the output of this is empty
$ echo $LD_LIBRARY_PATH
Please set the default path
$ LD_LIBRARY_PATH=/usr/local/lib
When working on a supercomputer, I received this error when I ran:
module load python/3.4.0
screen
python
To resolve the error, I simply needed to reload the module in the screen terminal:
module load python/3.4.0
python