Consider the following Linux command to compile and run the MySQL Connector/C++ Example 1.
g++ test.cpp -lmysqlcppconn; ./a.out
I understand that the -l flag adds the specified library to the list of libraries to link, and the -L flag adds the specified directory to the list of directories to look in.
Q: Given that I did not specify the -L flag, how do I determine where mysqlcppconn is located?
My program compiles and runs without errors; however, I want to know where the MySQL Connector/C++ is installed. I've managed to find some MySQL headers in /usr/include/ and /usr/include/cppconn, and there is a directory called mysql in /usr/lib, but nothing named mysqlcppconn is inside.
Thank you! I am re-introducing myself to developing on a Linux environment, and have lots to re-learn and catch up on.
In Ubuntu or other debian derived system, you can use command dpkg with -L option to see installed files from deb package.
$ dpkg -L libmysqlcppconn7v5
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7.1.1.9
/usr/share
/usr/share/doc
/usr/share/doc/libmysqlcppconn7v5
/usr/share/doc/libmysqlcppconn7v5/changelog.Debian.gz
/usr/share/doc/libmysqlcppconn7v5/copyright
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7
So the installation location for mysqlcppconn is /usr/lib/x86_64-linux-gnu/libmysqlcppconn.so
The library file is located in /usr/lib and called libmysqlcppconn. Both a static file and shared object file exist.
This answer is provided by πάντα ῥεῖ in the comment section.
The command ldd a.out will show what libraries the executable a.out is using.
Related
I'm compiling a c++ program using g++ and i am using two libraries called libsdl2-dev and libsdl2-image-dev
I installed both these libraries in my ubuntu machine with the commands
apt install libsdl2-dev libsdl2-image-dev and when I compile the program everything works fine. Then I copied these libraries from /usr/lib/x86_64-linux-gnu/ to my working dir with the binary file to be able to give this folder to someone else.
The problem comes when the user that hasn't installed these libraries tries to open my program by writing ./main (the binary file). Since he hasn't installed these libraries he would get an error like "can't open shared object: no such file or directory".
This happens because the binary file looks for these libraries in /usr/lib etc...
What i need
I need that my binary file looks for these libraries in the same folder,and not in /usr/lib/x86 etc.., from what I read I have to do something like rpath
The IDE used is Sublime Text and the syntax used to compile all my files is this:
g++ -c src/*.cpp -std=c++14 -m64 -g -Wall -I include && g++ *.o -o bin/debug/main -lSDL2main -lSDL2 -lSDL2_image && ./bin/debug/main`
Structure of folders
I got the project dir with and inside that i got 4 more directories, each one called: bin (with the debug subdirectory, where we got the final compile), include (with hpp files), res (with all textures), and src with all cpp files to compile, the other files are project files and .o files
I'm using Ubuntu 20.04-2 LTS and the same is for the other user's PC
Thanks in advance for any help!
That's because the dynamic linker loading runtime dependencies looks for them in some specified locations, which are "by default" your system library directories (where those libraries got installed by apt).
The other user should ideally install those libraries too (which could be done "automatically" if you build a .deb package with proper dependencies)
Otherwise you would have to change the runpath of your program by adding -Wl,-rpath='$ORIGIN', which makes the dynamic linker look for dependencies just where the binary is located.
$ORIGIN here is a special variable meaning "this executable" which is what you wanted to achieve.
see rpath
and A description of RPATH $ORIGIN
I found a way to resolve!
I used the program patchelf to add an rpath to my directory (linked to the binary file) now everything works
use ldd ./bin/debug/main to check the library
export LD_LIBRARY_PATH =$LD_LIBRARY_PATH:"your library path"
run the program,if this is not work. use patchelf to change the rpath to you r library
I'm trying to use the wavelet libraries for c++ (https://sourceforge.net/projects/wavelet2d/files/wavelib-0.4.0.0/) on my MacOSx and having hard trouble in making it work.
Here is my command line :
g++ -I /usr/local/include/wavelib -L /usr/local/lib -lwavelet2d testWavelib3.cpp -o testWavelib3
I get the following error :
library not found for -lwavelet2d
The library file is named 'libwavelet2d.so.1' in the directory /usr/local/lib.
Do you have any ideas?
You mention that you have libwavelet2d.so.1 in /usr/local/lib, but not libwavelet2d.so. Typically what this means is that you have installed the "runtime package" for this library but not the "development package". There should be a symlink /usr/local/lib/libwavelet2d.so -> libwavelet2d.so.1.
You can make the symlink yourself to try it:
ln -s libwavelet2d.so.1 /usr/local/lib/libwavelet2d.so
At build time, the file without the version suffix (.1) is required. At runtime, only the suffixed file will be referenced.
Update: issue is solved. The library was Made for Armv7a CPUs but it was "soft float" Not "hard float". It seems like my machine is HF and Not SF compatible
My program depends on an externally build .so library called libMyLib.so. When I compile the program like this:
$ g++ -std=c++11 main.cpp -o run -pthread
it reports that there are a lot of undefined references, obviously because i didn't include libMyLib.so when compiling. So the compiler knows what he needs to compile the program. When i compile the program like this:
$ g++ -std=c++11 main.cpp -o run -pthread -lMyLib
it doesn't report any errors and creates the file "run". Notice that libMyLib.so is already in /usr/local/lib and it looks like it is linked when compiling since the references are defined now and the "run" file is created. But as i run the file, this happens:
$ ./run
./run: error while loading shared libraries: libMyLib.so: cannot open shared object file: No such file or directory
I've checked with ldd and it shows me this:
$ ldd run
...
libMyLib.so => not found
...
So ldd doesn't find the library on execution, but it finds it while compiling. I'm quite new to Linux and linking libraries so i don't know what to do.
Also, running ldd on the .so file returns this:
$ ldd /usr/local/lib/libMyLib.so
not a dynamic executable
I've already checked an this message may occur when running a .so file on the wrong platform. But i've checked, the library is compiled for arm (I'm running on a raspberry pi -> arm):
$ objdump -f /usr/local/lib/libMyLib.so | grep ^architecture
architecture: arm, flags 0x00000150:
I also update the linker:
$ sudo ldconfig -v
...
/usr/local/lib:
libwiringPi.so -> libwiringPi.so.2.44
libwiringPiDev.so -> libwiringPiDev.so.2.44
libMyLib.so -> libMyLib.so.1
...
I really have no clue why this might still happen. Can anyone help me?
/usr/local/lib is one of the directories that the linker searches by default
for libraries specified with the -l option, so your linkage succeeds.
At runtime however, the program loader by default searches for the linked
libraries in:-
/lib, /usr/lib and among the libraries whose names and locations have been cached in the ldconfig cache, /etc/ld.so.cache.
The directories listed in the value of the environment variable LD_LIBRARY_PATH,
in the current shell.
The ldconfig cache is only updated when ldconfig is run. See man ldconfig.
The loader fails to find libMyLib.so at runtime because you have not
run ldconfig since you placed that library in /usr/local/lib and
neither have you correctly added /usr/local/lib to the LD_LIBRARY_PATH
in the same shell in which you try to run the program.
It is inconvenient and otherwise undesirable to require a special setting of
LD_LIBRARY_PATH to enable a program to run.
To enable the loader to find your library, run ldconfig as root. This
will succeed provided that /usr/local/lib is listed in /etc/ld.so.conf,
or in one of the files included by /etc/ld.so.conf. If it's not, then
you can explicitly cache the shared libraries from /usr/local/lib by running
ldconfig /usr/local/lib, as root.
First check LD_LIBRARY_PATH variable is having the path to your library directory
$ echo $LD_LIBRARY_PATH
If not there then update the library path.
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/library
Use strace to debug.
strace -f ./run
I have downloaded and installed the NTL library on my Ubuntu. I'm currently using gedit to write my program and having included this ZZ.h header in my program. This is how i compile my program in the terminal: - g++ keygen.cpp -o keygen -I ../include -L ../lib -lntl -lm.
I'm pretty sure this line is correct but for some unknown reason, i get the following error:
KeyGen.cpp:9:20: error: NTL/ZZ.h: No such file or directory
KeyGen.cpp:15: error: expected constructor, destructor, or type conversion before ‘int’
The solution seems pretty straightforward to me: which is to add the NTL library directly to my program folder. I did just that, but still i get the same error.
If you don't need the latest (6.0.0) version of NTL you may do as follows in your Ubuntu:
user#host:~$ sudo apt-get install libntl-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libntl-5.4.2
The following NEW packages will be installed:
libntl-5.4.2 libntl-dev
0 upgraded, 2 newly installed, 0 to remove and 112 not upgraded.
Need to get 2,035 kB of archives.
After this operation, 7,016 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://ftp.de.debian.org/debian/ squeeze/main libntl-5.4.2 amd64 5.4.2-4 [767 kB]
Get:2 http://ftp.de.debian.org/debian/ squeeze/main libntl-dev amd64 5.4.2-4 [1,268 kB]
Fetched 2,035 kB in 2s (1,017 kB/s)
Selecting previously deselected package libntl-5.4.2.
(Reading database ... 59184 files and directories currently installed.)
Unpacking libntl-5.4.2 (from .../libntl-5.4.2_5.4.2-4_amd64.deb) ...
Selecting previously deselected package libntl-dev.
Unpacking libntl-dev (from .../libntl-dev_5.4.2-4_amd64.deb) ...
Can not write log, openpty() failed (/dev/pts not mounted?)
Setting up libntl-5.4.2 (5.4.2-4) ...
Setting up libntl-dev (5.4.2-4) ..
user#host:~$
after that the complete compiled NTL library with all development headers is installed in your system and you may compile your program with it without any additional -I<path>.
If you need a newer version that your distro has (check http://packages.ubuntu.com/en/source/trusty/ntl) you may try to build the library package yourself.
The problem with your attempt to compile and output an executable file appears to be compiler's inability to link the necessary library after obtaining an object .o file.
Many people often test the point of fault by separating the two stages by first compiling g++ -c then by linking the libraries for an executable g++ -o. Although -Wall switch does not always work, trying it to provide you with as much information as possible during compilation can also be helpful.
Check this webpage. As for using different switches to link libraries try this webpage.
I'm not certain if it was a typo; but I wonder if the space between the switch and directory:-I ../include and -L ../libwas the problem.
You said in comments:
Icreated a folder called 'include' within the .cpp folder and included the NTL library in that folder already
But your compilation command says:
g++ keygen.cpp -o keygen -I ../include -L ../lib -lntl -lm.
It seems to me, you meant:
g++ keygen.cpp -o keygen -I ./include -L ../lib -lntl -lm.
# ^^^^^^^^^
since .. goes up one directory.
I am trying to use filesystem from boost in c++
It seems the compilation is ok when using
"c++ -c Analyse.c -o Analyse.o -g -W -Wall -L/usr/local/lib -lboost_filesystem -lboost_system"
However I have the following error when trying to execute my code :
"error while loading shared libraries: libboost_filesystem.so.1.54.0: cannot open shared object file: No such file or directory", a find / -iname "libboost_system.so.1.54.0
I had some issues to install boost (I first installed the 1.49 and after that 1.54) so I was wondering if there could be any conflict between the 2 version ?
P.S : btw a "find / -iname "libboost_system.so.1.54.0" gave me the following
/usr/include/boost/boost_1_54_0/bin.v2/libs/system/build/gcc-4.7/release/threading-multi/libboost_system.so.1.54.0
/usr/local/lib/libboost_system.so.1.54.0
Try to add the directory before execution. For example:
LD_LIBRARY_PATH="/usr/local/lib/" ./Analyse.o
I encountered this issue very recently, after a fresh installation of boost. In my case, the solution was to simply run
sudo ldconfig
The explanation is that the system keeps a cache of the installed shared libraries (located in /usr/lib, /lib, /usr/local/lib). When the libraries are changed, or new ones are added, the cache is not updated until ldconfig is run. More details can be found in the ldconfig manual.