linking libraries -rpath LD_LIBRARY_PATH - c

I have some 3rd party libraries and includes (I have copied them to the this location /usr/ssd/include and /usr/ssd/lib) that I need to link with my application. I have just created a test application to see if I can link ok. However, when I try to run my app I get the following message.
./app: error while loading shared libraries: libssdn.so: cannot open shared object file: No such file or directory
On the command line I am compiling like this:
gcc -g -Wall -I/usr/ssd/include -L/usr/ssd/lib -lssdn test_app.c -o app
Everything compiles ok, as I don't get any warnings or errors. However, I get the error when I try and run the app.
In the usr/ssd/lib the library is called libssdn.so
I am been looking for solution and I have read something about -rpath, -Wl and LD_LIBRARY_PATH, but not sure what they are and how to include them when I compile.
I am using Ubuntu 9.04 Linux,
Thanks for any advice,

Test if adding /usr/ssd/lib to your $LD_LIBRARY_PATH helps:
In a shell:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/ssd/lib
If that solves the problem, make it permanent by adding /usr/ssd/lib to /etc/ld.so.conf or by running
ldconfig -n /usr/ssd/lib

My personal preference is not to bake the location of a shared object into an executable (which is what -rpath would do).
Instead, you should add /usr/ssd/lib to your LD_LIBRARY_PATH at run time. Assuming you are running bash or a bash like shell, do:
export LD_LIBRARY_PATH=/usr/ssd/lib:${LD_LIBRARY_PATH}
and once you do that, you can run your executable.

Related

Why can my C program run in "git bash", but not in "cmd"?

I wrote a demo using libpq to connect to a PostgreSQL database.
I tried to connect the C file to PostgreSQL by including
#include <libpq-fe.h>
after I added the paths into system variables I:\Program Files\PostgreSQL\12\lib as well as to I:\Program Files\PostgreSQL\12\include and compiled with this command:
gcc -Wall -Wextra -m64 -I "I:\Program Files\PostgreSQL\12\include" -L "I:\Program Files\PostgreSQL\12\lib" testpsql.c -lpq -o testpsql
It first raised three errors, like
libssl-1_1-x64.dll is missing
libintl-8.dll was missing
libcrypto-1_1-x64.dll was missing
After I downloaded these three files and put them into I:\Program Files\PostgreSQL\12\lib, and compiled it again, it shows the error
The application was unable to start correctly (0xc0150002)
when I type testpsql. But if I type ./testpsql on git bash, it works. Anyone can please tell me why?
The code that I used was the first example from here.
Environment: PostgreSQL 12, Windows 10, MinGW64
“Download the DLL files” sounds dangerous. From where?
I would get rid of these files again. Since you probably don't reference these libraries from your code, it must be the dependencies of libpq.dll and are probably found in I:\Program Files\PostgreSQL\12\bin (if you used the EDB installer).
The problem is probably that you the PATH environment variable is different in git bash and in cmd.exe, and in the latter case not all required shared libraries can be found on the PATH. The solution is to change the PATH so that it includes all DLL files the executable requires, not to start copying around files.
It is probably enough to include I:\Program Files\PostgreSQL\12\bin in the PATH. To resolve missing dependencies, use a tool like dependency walker or this replacement.

Cython not finding shared library

My issue started identical to this one: Python executable not finding libpython shared library
I updated .bashrc with export LD_LIBRARY_PATH=$HOME/local/lib/python/2.7.6/lib and things were fine. Python works, and I installed pip. But now, I'm running into something similar when installing cython with pip. I get this error message when I execute pip install cython:
gcc -pthread -shared build/temp.linux-x86_64-2.7/tmp/pip_build/cython/Cython/Plex/Scanners.o -L. -lpython2.7 -o build/lib.linux-x86_64-2.7/Cython/Plex/Scanners.so
/usr/bin/ld: cannot find -lpython2.7
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
I cannot add $HOME/local/lib/python/2.7.6/lib to /etc/ld.so.conf and run ldconfig as I do not have root. I was under the impression that setting the LD_LIBRARY_PATH was the way around this, but this appears to not be true for compilation. Is there a way to get the compiler to see this local library without running root commands?
Update:
The LD_LIBRARY_PATH is only used by the dynamic loader at runtime, not at build time, so that is not the issue. The issue is that you forgot to put the -L/path/to/pylib before the -l. I've never had to use LIBRARY_PATH because a build requires path extension that is specific to a given build, so you never set LIBRARY_PATH you just use -L. You would only set if if you are going to regularly do builds that use a specific library, and even then I find it better to use -L because sooner or later this will cause linker to find the wrong lib and by then you will have forgotten that it's because LIBRARY_PATH is set permanently.
There are many ways to set -L values in a build: if you run the compiler from command line you don't need that env var, you just specify as many -L as required as part of the command; if you use a makefile, you edit whatever make variable you are using, such as CFLAGS or other, different platforms have different conventions. So whereas setting -L directly will always work, setting CFLAGS will only work if that is the variable used by the makefile.
Now this is a python installation so where to set this may not be obvious, but I am sure there is another way than setting LIBRARY_PATH. In principle any python package you install, if it involves compilation of C++ modules, could require edit of the setup.py to set library paths. For example
Extension(...,
library_dirs=['/usr/X11R6/lib'],
...)
Since you mention nympy, another place to set this might be in site.cfg (see Supplying NumPy site.cfg arguments to pip).
Old (wrong) answer:
Set your LD_LIBRARY_PATH in your bash console. If this doesn't work then it's because you have the wrong path: check by echoing the environment var.
Once you get that to work, edit your .bashrc or .profile then exit your shell and restart it. Echo the env var to verify that contains the part you added.
Also, ensure that you are appending to the path rather overwriting it:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/...
export LD_LIBRARY_PATH
Because python lib might depend on .so in other folders, if the linker can't find them it may appear as though it is the python lib that was not found. This is not explained on the page you linked to in your question.
OK after some more digging I found this: LD_LIBRARY_PATH vs LIBRARY_PATH
Setting LIBRARY_PATH to the same path as LD_LIBRARY_PATH made the compiler aware of the python lib. cython/numpy/scipy all built and installed no problem afterwords.

How can I build the mongoose web server on Linux?

I apologize if this seems like a stupid question, but I guess I am more used to packages that come with Makefiles or configure scripts.
I downloaded the Mongoose source tar file from the site, and untarred it.
I then tried to compile an executable out of it using
gcc -g -c mongoose.c -o main.o -lpthread -ldl.
However, after trying to execute, I get the error -bash: ./main.o: cannot execute binary file
When I looked into mongoose.c source, I did not find a main function.
Where can I get the main function so that the Linux mongoose web server can be compiled to work the same way as the Windows mongoose.exe?
Mongoose does come with a Makefile, and will compile as a standalone command-line program. Lua and SQLite are included.
The easiest way to compile the latest version is to cd into the "build" directory and run make unix. I'm not sure about the archived version on the downloads page, but trunk has been pretty stable.
I just checked out a clean copy of Mongoose from github earlier tonight and built it with no problems, so I can confirm that this works (assuming you have any other dependencies set up properly, of course).
It's because mongoose is not supposed to be used standalone, but to "embed" it into your program. You need to create a program which calls the correct function from mongoose.c.
Also, the -c flag to GCC tells it to create an object file, which needs to be linked to create an executable. So you try to execute a file which is not executable.

Can not find linux/modversions.h

I am trying to install the driver for a serial device, and when I run the installation executable I get this error:
cc -DLINUX -c -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -m64 -mcmodel=kernel -I/usr/src/linux-3.8.0-27-generic/include -I/usr/src/linux-2.4/include -I/usr/src/linux/include -D__SMP__ npreal2.c
npreal2.c:40:31: **fatal error: linux/modversions.h: No such file or directory**
compilation terminated.
I don't find any solutions to this after searching the forums. I noticed that there is a modversions.h in the /usr/src/linux-3.8.0-27-generic/include/config , but not in the linux folder.
Please help!
Try passing -I /usr/src/linux-3.8.0-27-generic/include/config as an argument to make?
or
Check if the header is a part of a certain package and update the package.
You can compile modversions on your system by navigating to the linux directory (usually usr/src/linux). Inside the linux source directory, there should be a file called Rules.make. Inside this make file are build commands for making modversions.h. You can make it by running:
make update-modverfile
Now, while this will make the modversions.h library, if you compile it with a newer compiler than the libraries that this file relies on, many times you will get an error when trying to run a program that uses this header. This then turns into a nightmare.
Another method, I tried it successfully with Xubuntu 13.10:
Open /etc/default/grub
Add this Line and save it.
GRUB_CMDLINE_LINUX="CONFIG_MODVERSIONS=true"
reboot
(no, sudo update-grub,ok)
open a terminal window, enjoy.
locate modversions.h
(Please don't forget modversion'S')

How to build example with shared object in GnuTLS

I'm trying to compile example from GnuTLS. I can compile GnuTLS with no problem.
I usually use this command when I have default GnuTLS package installed. I compile the example with this commend.
gcc -o server ex-serv-srp.c -lgnutls
I build GnuTLS from source. I can compile the example with the same command but when I try to run the example I get this error:
./server: error while loading shared libraries: libgnutls.so.28: cannot open shared object file: No such file or directory
The location of libgnutls.so.28 is in /usr/local/lib directory. How I can link the example during compilation time so that they will know where to find libgnutls.so.28
Regards
For a permanent solution add /usr/local/lib to /etc/ld.so.conf and rerun ldconfig, otherwise do as zvbra proposes.
You should set LD_LIBRARY_PATH variable like this export LD_LIBRARY_PATH=/usr/local/lib.

Resources