Static and dynamic linking linux

Linux: C/C++ standard library static vs dynamic linking [duplicate]

Probably on any OS it is possible to compile C++/C standard library statically or dynamically. On Windows I prefer static builds always, because it helps to avoid «dll hell» problem with different versions of libraries installed or not installed on specific Windows version, edition and service pack, etc. Static linking makes software more portable and less dependent on what end user did with his operating system (I even saw examples when end user could make SHIFT+DEL on some DLLs in system32 , he couldn’t explain why, or when users claim that my app contains virus because it tried to download dynamically linked prerequisites from official Microsoft website. ) So, on Windows static linking is usually better than dynamic one in my experience. However, I am new to Linux, so can anyone share his experience? My question is: what kind of linking (dynamic or static) is preffered on Linux if we ignore the fact that dynamic one allows to save memory & hard drive space and if we plan to distribute software with automated install program (hard drive space and memory are cheap enough now, so there are not reason to sacrifice hours of working time required to create really good and portable installer to win some megabytes of RAM or hard drive space). Are there any Linux-specific issues with dynamic/static linking?

@BasileStarynkevitch It is easier to link it statically than package for every distribution out there.

4 Answers 4

On Linux you normally have a package manager that ensures you only have one version of libraries installed. So there normally is no dll hell and no problem with linking dynamically. Linking dynamically is the standard way on Linux.

Читайте также:  Logitech lightsync g102 linux

@MaximYegorushkin: It will possibly work on distros that provides the same package management system.

I’d say the answer depends on how you distribute the software.

If you package the software for a specific Linux distribution and version dynamic linking is usually preferred. You know which libraries to find on the system and you can specify dependencies.

However, if you want to distribute the software as a Linux binary that runs on «any» system (such as various games or software like Matlab for example) you will end up with the same dll (or .so) hell problem as on windows. You don’t know which versions of which libraries are on the system. Thus, you will have to provide your own .so files or link statically.

Let me add, that shared libraries can also be loaded programmatically using the dl library (link with -ldl). As such, it is easy to first check in a program whether some required library version is available, and then either load this library, or to display a notification to the user, telling him precisely what is missing so that he can just pop up his package manager and install that lib.

That is true, but independently distributed binaries are potentially linked against older versions of a lib that may no longer be available in the package manager.

See the whole point in using dynamic linking is to reduce the size of executables and memory usage.If you neglect that there is too less to talk about.

On the other hand you mentioned about saving memory and disk space.It is necessary to save disk space because when you want to export your app/program, you can’t put a 2Gb app on the internet for download(for example openCV library is about 2.1GB). The solution is to dynamically link them and load only those modules which are necessary to you.This enables efficient multitasking also(creates just one copy of the module and the whole program uses the same copy). peculiarly:

For example, a media player application might originally be shipped with a codec that supports the mp3 file format. If the media player were statically linked it would not be possible to dynamically update it to support a different file format, without replacing the entire application. Dynamic linking means that a new version of the shared library containing a more up-to-date codec, which includes some enhancements and bug fixes, could be dynamically loaded by a dynamic linker into memory at run-time to replace the original shared library. A shared library can also be shared by more than one application. For example, two different media players could both use the same shared library containing the same codec. This potentially means that the device running the application requires less physical memory, depending on the size of the dynamic linker.

third, in linux everything is dynamically linked except for the /bin/ash.static whic also has its dynamic version /bin/ash but this shouldn’t stop you from static linking in linux. when using gcc the linking is by default dynamic.I guess you should use the «-static» flag to statically link the libraries

Читайте также:  Git windows and linux

Источник

How we use dynamic linking in Linux? Give example.

Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly.

Static Linking in Linux

Static linking is the process of linking an object code directly to the executable code of the application during linking or building time of the executable. Static linking is a compilation / build time process. Let us consider mathlib.c file. We want to make it as a static library.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.

Linux library project


Linux library source


Linux library header


Linux static library

Now make a archive or static lib with the object file.
$ar rc libmath.a mathlib.o

Linux library application


  1. compile the application code (place math.h in include folder)
    $cc -c libapp.c -Iinclude -o app.o
  2. Link with static library math.a
    $ld app.o libmath.a -o app
  3. Run the application
    $./app

Dynamic Linking

Dynamic linking is the process of linking a library function at the time of loading the application or during the runtime of the application. In contrast to static linking this happens during execution. Dynamic linking is divided into two categories — «Implicit dynamic linking» and «Explicit dynamic linking».

Implicit Dynamic Linking

Implicit dynamic linking is the process of linking a function from a shared library during the loading of the application. Let us consider once again mathlib.c file for dynamic linking. We want to make it as a dynamic library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.
$cc -fPIC -c mathlib.c

Now make a shared library with the object file.
$cc -shared libmath.so mathlib.o

  1. compile the application code (place math.h in include folder)
    $cc -c libapp.c -Iinclude -o app.o
  2. Link with import library math.lib
    $ld app.o -lmath -o app
  3. Copy the libmath.so in lib path or current path and run the application
    $./app

Explicit Dynamic Linking

Explicit dynamic linking is the process of linking a function from a shared library during the runtime of the application using verious helper function of the dynamic loader. Let us consider once again math.c file for explicit linking. The steps for creating a shared library are same as that of implicit linking.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.

Now make a shared library with the object file.
$cc -shared libmath.so mathlib.o

  • dlopen() — loads a dynamic link binary
  • dlsym() — returns the function pointer if found the function entry
  • dlclose() — unloads the dynamic link binary

Источник

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