Shared Library Installation - linux

I need to install a shared library in the location other than the standard location(/usr/lib) in Ubuntu 16.04. I have read this article. But I can't able to understand it clearly. Could anyone please explain how we can actually install and use a shared library in simple terms, so a beginner can understand.
And also what mean by this error message:
"./executable: error while loading shared libraries: maths.so.1: cannot open shared object file: No such file or directory"
I often get this message while running programs that use shared libraries.Please let me know if you need any further information.
Thanks
PreeJackie

Assuming foo.c your source file example.so is the shared library at /home/lib location
gcc -o foo foo.c -L/home/lib -lexample -Wl,-rpath=/home/lib
foo.c - your c file.
example.so - your custom shared library.
gcc -o output_file input_file -L<shard library path> -l<library name without .so> -Wl,-rpath=<shared library path>

Related

linking fortran code to library

I'm trying to link my main code to a library
main code:
program main1
call test1
end program main1
library:
subroutine test1
print*,'ok'
end subroutine test1
then I create library :
gfortran -shared -fPIC -o lib1.so 1.f90
and compile main code
gfortran -c main.f90
and link
gfortran main.o lib1.so
but I've got this error :
./a.out: error while loading shared libraries: lib1.so: cannot open shared object file: No such file or directory
what am I doing wrong?
your example does work, but you are just missing a small thing: When using a shared library, your program (main.f90 / a.out) will try to find the linked library in one of the library folders (such as /lib*, /usr/lib* or /usr/local/lib*).
If you want to specify another folder for your shared library (for example for testing/debugging), you can use the enviroment variable LD_LIBRARY_PATH to "tell" linux another place to look for shared libraries.
So, assuming you wrote your program in the folder /home/mojijoon/fortran you can get the correct output after setting the library path by:
$: export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/home/mojijoon/fortran
$: ./a.out
ok
You can find more information on shared libraries (and the LD_LIBRARY_PATH enviromental variable) here:
tldp.org - shared libraries

Unable to link shared libraries not under the system library directories on Ubuntu 12.04

I created a shared library, libsslab_core.so.1.0.0 using gcc with proper options. I am pretty sure that the shared library works because I already linked it to another source code (I explicitly tells a compiler, gcc, the location of the library using -l option of the compiler).
After testing the library works, I tried to integrate the library into my Linux machine. I went to the /etc/ld.so.conf.d/ and added a file, sslab.conf. In the file I just typed the absolute path of the library, /opt/lib/sslab. Next, I executed ldconfig as a root to update the cache file of ldconfig. And I checked if the system finds the newly added library by typing ldconfig -p | grep libsslab. My Linux machine found the library, so I thought everything is finished.
However, when I try to compile a source code using the library, it gives me the following error:
/usr/bin/ld: cannot find -lsslab_core
When I move the library to /usr/local/lib, update the content of sslab.conf, and execute ldconfig as a root. I can use the shared library without any problems.
Do you have any ideas about the problem that I've come across on Ubuntu 12.04?
For your information, I refer to a document in TLDP to generate my own shared library. Here is the link: http://www.tldp.org/HOWTO/Program-Library-HOWTO/

ldd library not found

I get the following error launching my program:
error while loading shared libraries: libnetcdf.so.6: cannot open shared object file: No such file or directory
The point is the libnetcdf.so.6 is the old version of the library, I have deleted it and built the new one.
So when I try ldd I see:
libnetcdf.so.7 => /usr/local/lib/libnetcdf.so.7 (0x00007f70f8c4b000)
but also
libnetcdf.so.6 => not found
Why this old reference? What can I do to solve?
You will need to re-link your application to libnetcdf.so.7 so the application looks for that rather than .6
You may have a symbolic link that the linker will look at with no version number (libnetcdf.so) if this isn't present you may need to create it before re-linking:
ln -s libnetcdf.so.7 libnetcfd.so
If you can't re-link the application then you can create a symbolic link to make the application look at your newer library (although this could cause segmentation faults if the library versions aren't binary compatible) to point to the actual .so file:
ln -s libnetcfd.so.7 libnetcfd.so.6
This will cause the application to find the shared object it requires but isn't the ideal solution.

Compiling my C program with my customized library (.h) using Linux

Hi team,
I have three files which I need to compile for testing, btw im using CentOS linux.
source_code.c
library.h
library.c
how do I put the library.h in the gcc library, so I can use it?
how do I compile the source_code.c to use that library?
Thank you very much.
This is basic knowledge of your tools, but you can do this:
#include "library.h" in the include section of the library.c code (at top of the file).
gcc source_code.c library.c in the linux terminal will link and compile both source_code.c and library.c. This will generate an executable named "a.out" (if there were no compilation problems). You can change its name, by adding the option -o name to the gcc command (gcc source_code.c library.c -o mycode will generate an executable named "mycode").
If you really need a library that will be used by a lot of other programs, you can look for "shared libraries", but I think that you are asking for a basic thing.
You dont need this library.h while building and executable (with gcc) as you should have specified the exact location of the library in the source file. All you need to do is gcc sourcefile1.c sourcefile2.c -o exename

Cannot access local shared library from /usr/local/lib

I'm venturing into the world of C++ and Linux, and am having problems linking against a shared library.
I have a library, libicuuc.so.44.1, installed in /usr/local/lib. There is also a link in the same directory, libicuuc.so.44 pointing to that library.
My /etc/ld.so.conf reads:
include /etc/ld.so.conf.d/*.conf
I have a file, /etc/ld.so.conf.d/libc.conf, that contains:
# libc default configuration
/usr/local/lib
However, when I compile my program (that includes LIBS += -licuuc), I get the following error at runtime:
error while loading shared libraries:
libicuuc.so.44: cannot open shared
object file: No such file or directory
I am using Qt Creator on Ubuntu 10.04.
Any help is greatly appreciated!
Did you modify by yourself /etc/ld.so.conf.d/libc.conf ?
If yes, then run (as root) ldconfig to re-read the config.

Resources