Create shared libraries linux

Intro to Linux Shared Libraries (How to Create Shared Libraries)

A library is a file containing compiled code from various object files stuffed into a single file. It may contain a group of functions that are used in a particular context. For example, the ‘pthread’ library is used when thread related functions are to be used in the program.

Broadly, a library (or Program Library) can be of two types:

In this article, we will discuss specifically Shared Libraries.

Shared Libraries

Shared Libraries are the libraries that can be linked to any program at run-time. They provide a means to use code that can be loaded anywhere in the memory. Once loaded, the shared library code can be used by any number of programs. So, this way the size of programs(using shared library) and the memory footprint can be kept low as a lot of code is kept common in the form of a shared library.

Shared libraries provide modularity to the development environment as the library code can be changed, modified and recompiled without having to re-compile the applications that use this library. For example, for any change in the pthread library code, no change is required in the programs using pthread shared library. A shared library can be accessed through different names:

  • Name used by linker (‘lib’ followed by the library name, followed by ‘.so’ . For example libpthread.so)
  • Fully qualified name or surname ( ‘lib’ followed by the library name, followed by ‘.so’, followed by ‘.’ and a version number. For example: libpthread.so.1)
  • Real name (‘lib’ followed by the library name, followed by ‘.so’, followed by ‘.’ and a version number, followed by a ‘.’ and a minor number, followed by a ‘.’ and a release number. The release number is optional. For example, libpthread.so.1.1)

A version number is changed for a shared library when the changes are done in code make the shared library incompatible with the previous version. For example, if a function is completely removed then a new version of the library is required.

A minor number is changed in case there is a modification in the code that does not make the shared library incompatible with the previous version being used. For example, a small bug fix won’t break the compatibility of the existing shared library, so only a minor number is changed while version remains the same.

Читайте также:  Check kernel parameters linux

Now, one may wonder why so many names for a shared library?

Well, these naming conventions help multiple versions of same shared library to co-exist in a system. The programs linking with the shared library do not need to take care of the latest version of the shared library installed in the system. Once the latest version of the shared library is installed successfully, all the programs automatically start linking to the latest version.

The name used by the linker is usually a symbolic link to the fully qualified surname which in turn is a symbolic link to the real name.

Placement in File System

There are mainly three standard locations in the filesystem where a library can be placed.

We will go by the Filesystem Hierarchy Standards(FHS) here. According to the FHS standards, All the libraries which are loaded at start up and running in the root filesystem are kept in /lib. While the libraries that are used by system internally are stored at /usr/lib. These libraries are not meant to be directly used by users or shell scripts. There is a third location /usr/local/lib( though it is not defined in the latest version of FHS ). If it exists, it contains all the libraries that are not part of standard distribution. These non-standard libraries are the one’s which you download and could be possibly buggy.

Using ldconfig

Once a shared library is created, copy the shared library to a directory in which you want the library to reside (for example /usr/local/lib or /usr/lib). Now, run ldconfig command in this directory.

You remember that we discussed earlier that a linker name for shared library is a symbolic link to the fully qualified surname which in turn is a symbolic link to the real name. Well, this command does the same.

When you run an ELF executable, by default the loader is run first. The loader itself is a shared object file /lib/ld-linux.so.X where ‘X’ is a version number. This loader, in turn, finds and loads all the shared libraries on which our program depends.

All the directories that are searched by the loader in order to find the libraries are stored in /etc/ld.so.conf. Searching all the directories specified in /etc/ld.so.conf file can be time-consuming, so every time ldconfig command is run, it sets up the required symbolic links and then creates a cache in file /etc./ld.so.cache where all the information required for the executable is written. Reading information from cache is very less time consuming. The catch here is that ldconfig command needs to be run every-time a shared library is added or removed. So on start-up, the program uses /etc.non-standard/ld.so.cache to load the libraries it requires.

Читайте также:  Linux ubuntu форматировать флешку

Using Non Standard Library Locations

non-standard library locations. One of the following three steps could be carried out :

Add the path to /etc/ld.so.conf file. This file contains paths to all the directories in which the library is searched by the loader. This file could sometime contain a single line like:

In that case, just create a conf file in the same directory. You can directly add a directory to cache by using the following command:

ldconfig -n [non standard directory path containing shared library]

Note that this is a temporary change and will be lost once the system is rebooted. Update the environment variable LD_LIBRARY_PATH to point to your directory containing the shared library. The loader will use the paths mentioned in this environment variable to resolve dependencies.

Note that on some Unix systems the name of the environment variable could differ.

Note: On a related topic, as we explained earlier, there are four main stages through which a source code passes in order to finally become an executable.

Example (How to Create a Shared Library)

Lets take a simple, practical example to see how we can create and use shared libraries. The following is the piece of code (shared.c) that we want to put in a shared library:

#include «shared.h» unsigned int add(unsigned int a, unsigned int b)

#include extern unsigned int add(unsigned int a, unsigned int b);

Lets first make shared.c as a shared library.

gcc -c -Wall -Werror -fPIC shared.c gcc -shared -o libshared.so shared.o

The first command compiles the code shared.c into position independent code which is required for a shared library.
The second command creates a shared library with name ‘libshared.so’.

#include #include»shared.h» int main(void)

gcc -L/home/himanshu/practice/ -Wall main.c -o main -lshared

This command compiles the main.c code and tells gcc to link the code with shared library libshared.so (by using the flag -l) and also tells the location of shared file(by using the flag -L).

  1. Now, export the path where the newly created shared library is kept by using the following command:
export LD_LIBRARY_PATH=/home/himanshu/practice:$LD_LIBRARY_PATH

The above command exports the path to the environment variable ‘LD_LIBRARY_PATH’.

# ./main Inside add() The result is [3]

So we see that shared library was loaded and the add function inside it was executed.

Читайте также:  Unpack tar bz2 linux

Источник

C++ Dynamic Shared Library on Linux

This is a follow-up to Dynamic Shared Library compilation with g++. I’m trying to create a shared class library in C++ on Linux. I’m able to get the library to compile, and I can call some of the (non-class) functions using the tutorials that I found here and here. My problems start when I try to use the classes that are defined in the library. The second tutorial that I linked to shows how to load the symbols for creating objects of the classes defined in the library, but stops short of using those objects to get any work done. Does anyone know of a more complete tutorial for creating shared C++ class libraries that also shows how to use those classes in a separate executable? A very simple tutorial that shows object creation, use (simple getters and setters would be fine), and deletion would be fantastic. A link or a reference to some open source code that illustrates the use of a shared class library would be equally good. Although the answers from codelogic and nimrodm do work, I just wanted to add that I picked up a copy of Beginning Linux Programming since asking this question, and its first chapter has example C code and good explanations for creating and using both static and shared libraries. These examples are available through Google Book Search in an older edition of that book.

I’m not sure I understand what you mean by «using» it, once a pointer to the object is returned, you could use it like you use any other pointer to an object.

The article I linked to shows how to create a function pointer to an object factory function using dlsym. It doesn’t show the syntax for creating and using objects from the library.

You will need the header file describing the class. Why do you think you have to use «dlsym» instead of just letting the OS find and link the library at load time? Let me know if you need a simple example.

@nimrodm: What’s the alternative to using «dlsym»? I’m (supposed to be) writing 3 C++ programs that will all use the classes defined in the shared library. I also have 1 Perl script that will use it, but that’s a whole other problem for next week.

Источник

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