Linux add library directory

Specify location of static libraries for C++ application in Linux

first of all, I hope that I ask the question in the right context here. I build an application in C++ with Code::Blocks. The application uses static libraries that are provided by a third party and cannot be installed on a system via the package management. Therefore I ship these libraries when I distribute my application. Here is what my target configuration looks like:

I can build this target fine and run it. Everything works. Today, I tried to run in on Debian Squeeze and just copied a folder which contained both the executable and the libraries from the third party. I thought that as long as everything is in one folder the executable will find the .so files. I was wrong. I get the message:

/home/my_app/my_app: error while loading shared libraries: lib1.so: cannot open shared object file: No such file or directory 

I don’t get this message on my developement machine because Code::Blocks is able to set a working directory for the executable. I could remove the error message by putting the location of the .so files inside /etc/ld.so.conf.d/my_app.conf. Is there anyway I can build the executable so it searches the libs in the execution directory? Or this is a problem specific for Debian? Or can I specify the working directory for the process before I execute the executable? I want to avoid changing the systems configuration / environment before you can start the application.

Источник

Andrew Beacock’s Blog

Sometimes in Linux when you install a new software package the instructions tell you to add a directory of shared libraries to your $LD_LIBRARY_PATH environment variable in your .bashrc.

You may have noticed that if you then create a shortcut icon on your desktop to this application it won’t start because it can’t find the libraries.
A typical solution is to write a wrapper shell script to set the LD_LIBRARY_PATH and then call that application.

Well, I’ve discovered how to add them to your system’s library path allowing all environments to access them. Note: There are differences between Debian and Ubuntu (the two flavours of Linux that I’m familiar with).

Ubuntu
Create a new file in /etc/ld.so.conf.d/ called .conf

Edit the file and add a line per directory of shared libraries (*.so files), it will look something like:

Debian
Edit /etc/ld.so.conf

Add a line per directory of shared libraries (*.so files) to the bottom of the file, it will look something like:

/usr/X11R6/lib
/usr/lib/APPLICATION/lib

If you run your new application it should now work fine without you having to set any LD_LIBRARY_PATH environment variables.
If you still have problems you can obtain a list of the libraries that are on the system path by re-running the ldconfig command in verbose mode:

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps
Читайте также:  Скриншот экрана линукс минт

Источник

How do you specify the location of libraries to a binary? (linux)

For this question I’ll be using a specific example, but really this generalizes to pretty much any binary on linux that can’t seem to find its’ dependent libraries. So, I have a program that won’t run because of missing libraries:

./cart5: error while loading shared libraries: libcorona-1.0.2.so: cannot open shared object file: No such file or directory 
linux-vdso.so.1 => (0x00007fff18b01000) libcorona-1.0.2.so => not found libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/libstdc++.so.6 (0x00007f0975830000) libm.so.6 => /lib/libm.so.6 (0x00007f09755af000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f0975399000) libc.so.6 => /lib/libc.so.6 (0x00007f0975040000) libz.so.1 => /lib/libz.so.1 (0x00007f0974e2b000) /lib64/ld-linux-x86-64.so.2 (0x00007f0975b36000) 
oliver@human$ find / -name libcorona-1.0.2.so 2> /dev/null /usr/local/lib64/libcorona-1.0.2.so /home/oliver/installed/corona-1.0.2/src/.libs/libcorona-1.0.2.so 

3 Answers 3

For a once-off, set the variable LD_LIBRARY_PATH to a colon-separated list of directories to search. This is analogous to PATH for executables, except that the standard system directories are additionally searched after the ones specified through the environment.

LD_LIBRARY_PATH=/usr/local/lib64 ./cart5 

If you have a program that keeps libraries in a non-standard location and isn’t able to find them on its own, you can write a wrapper script:

#!/bin/sh if [ -n "$LD_LIBRARY_PATH" ]; then LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64 else LD_LIBRARY_PATH=/usr/local/lib64 fi export LD_LIBRARY_PATH exec /path/to/cart5 "$@" 

The list of standard system directories is kept in /etc/ld.so.conf . Recent systems allow this file to include other files; if yours contains something like include /etc/ld.so.conf.d/*.conf , create a new file called /etc/ld.so.conf.d/mala.conf containing the directories you want to add. After you change /etc/ld.so.conf or an included file, run /sbin/ldconfig for your changes to take effect (this updates a cache).

( LD_LIBRARY_PATH also applies to many other unices, including FreeBSD, NetBSD, OpenBSD, Solaris and Tru64. HP-UX has SHLIB_PATH and Mac OS X has DYLD_LIBRARY_PATH . /etc/ld.so.conf has analogs on most unices but the location and syntax differs more widely.)

Источник

Where do I put third-party libraries to set up a C++ Linux development environment?

I’m not new in C++ although I’m new in Linux. I’m using CMake to precompile a cross-platform game engine with some third-party components, but I have a lot of doubts about using libraries. My question is how to work with third-party libraries and where to put them. Apt installs libs in their official place (/usr/local, /usr/lib/ ..) but I develop in Windows using local libs that are in a folder in my project dir. Also, I need a good tutorial to know the rules of how libraries work. For example: when trying to compile my project, luabind is asking for liblua.s0.1, but AFAIK there is no way to generate this library with the source provided by Lua (at least doing make, make install). I know, this question is fuzzy but I haven’t enough experience to be more concise. Update: After reading some answers, a more concise question is the following. If I install all third-party libraries, how can I distribute my program? How do I manage dependencies without using a large readme?

Читайте также:  Default web browser in linux

4 Answers 4

Where to put libraries

The best solution is to use your Linux distribution’s packaging system ( apt-get , yum , or similar) to install libraries from distro-provided packages wherever possible.

If the distro’s packaged libraries aren’t of a recent enough version, or if you need some nonstandard build options, or if you need a library that your distro doesn’t provide, then you can build and install it yourself. You have two main options for where to put the library:

  • /usr/local (libraries under /usr/local/lib , headers under /usr/local/include ). This installs the libraries systemwide and is probably the simplest solution, since you should then be able to build against them without taking any extra steps. Do NOT install libraries directly under /usr , since that will interfere with your distro’s packaging system.
  • Under your project directory, as you did under Windows. This has the advantages of not requiring root access and not making systemwide changes, but you’ll have to update your project’s include paths and library paths, and you’ll have to put any shared library files someplace where the dynamic linker can find them (using LD_LIBRARY_PATH or ld.so.conf — see the link for more details).

How libraries work

See David A. Wheeler’s excellent Programming Library HOWTO. I’d recommend reading that then posting any specific questions as new topics.

How to distribute your program

Traditionally, Unix / Linux programs do not include copies of their dependencies. It’s instead up to the end user or developer to install those dependencies themselves. This can require a «large README,» as you said, but it has a few advantages:

  • Development libraries can be installed, managed, and updated via the distro’s package manager, instead of each source copy having its own set of libraries to track.
  • There’s only one copy of any given library on a system, so there’s only one place that needs updating if, for example, a security flaw is found. (For example, consider the chaos that resulted when zlib, a very widely used compression library, was found to have a security flaw, so every application that included an affected version needed to be updated.)
  • If your program is popular enough (and is open source or at least freely available), then package maintainers for various Linux distributions may want to package it and include it in their distro. Package maintainers really don’t like bundled libraries. See, for example, Fedora’s page on the topic.

If you’re distributing your program to end users, you may want to consider offering a package ( .dpkg or .rpm ) that they could simply download and install without having to use source. Ideally, from the end user’s perspective, the package would be added to distros’ repositories (if it’s open source or at least freely available) so that users can download it using their package managers ( apt-get or yum ). This can all get complicated, because of the large number of Linux distros out there, but a Debian/Ubuntu compatible .dpkg and a Red Hat/CentOS/Fedora-compatible .rpm should cover a good percentage of end users. Building packages isn’t too hard, and there are good howtos online.

Читайте также:  Microsoft linux rdp client

Источник

Setting Library path in Linux

You need to use ldconfig config file and ldconfig command which 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 (for Debian distributions), and in the trusted directories such as /lib64 or /usr/lib64 (/lib or /usr/lib on 32 bit systems). The /etc/ld.so.conf contains lib settings which can be used to add or delete paths.

However, you need to simply drop your config file in /etc/ld.so.conf.d/ directory and it will be used by /sbin/ldconfig to configure dynamic linker run time bindings.

Add Your Path

Create a file called /etc/ld.so.conf.d/myapp.conf:

Activate Your Library Path

You must run the following command to activate path:

Verify Your New Library Path

# ldconfig -v | less 
# ldconfig -v | grep /usr/local/lib
/usr/local/lib: libGeoIP.so.1 -> libGeoIP.so.1.4.6 libGeoIPUpdate.so.0 -> libGeoIPUpdate.so.0.0.0 /usr/lib64/mysql: libmysqlclient_r.so.15 -> libmysqlclient_r.so.15.0.0 libmysqlclient.so.15 -> libmysqlclient.so.15.0.0 /lib: libutil.so.1 -> libutil-2.5.so

How Do I Delete The Library Path?

# rm /etc/ld.so.conf.d/myapp.conf # ldconfig

How Do I Edit The Library Path?

Simply edit the file and reload the changes:

# vi /etc/ld.so.conf.d/myapp.conf # ldconfig

How Do I Compile Program With Shared Libs And GNU GCC?

You can use the following gcc:

$ gcc -Wl,-R/path/to/lib -I/path/to/include -L/path/to/lib -o myAppName mycode.c -llibapp2

Add to LD_LIBRARY_PATH

Add the library path you need and set this at shell for temporary use or add to the shell initialization file for permanent effect:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/foobar/lib

Check shared libraries used by program

You can see the list of the shared libraries used by a program using ldd. So, for example, you can see the shared libraries used by ls by typing:

Generally you’ll see a list of the sonames being depended on, along with the directory that those names resolve to. In practically all cases you’ll have at least two dependencies:

  • /lib/ld-linux.so.N (where N is 1 or more, usually at least 2). This is the library that loads all other libraries.
  • libc.so.N (where N is 6 or more). This is the C library. Even other languages tend to use the C library (at least to implement their own libraries), so most programs at least include this one.

Beware: do not run ldd on a program you don’t trust. As is clearly stated in the ldd(1) manual, ldd works by (in certain cases) by setting a special environment variable (for ELF objects, LD_TRACE_LOADED_OBJECTS) and then executing the program. It may be possible for an untrusted program to force the ldd user to run arbitrary code (instead of simply showing the ldd information). So, for safety’s sake, don’t use ldd on programs you don’t trust to execute.

Источник

Оцените статью
Adblock
detector