Dynamic linked library in linux

The Dynamic Link Library (DLL) is stored separately from the target application and shared among different applications, compared to Static Library. The DLL is the file extension on Windows while on Linux, it is *.so (Shared Object).

The *.so/*.dll can be loaded right before the application starts or during the application’s runtime. On Windows, the Win32 API LoadLibrary is used while on Linux gcc compiler, the dlopen function is used.

Creating *.so on Windows

There are many ways and many languages to create a *.so on Linux e.g. You can write a C or C++ source code and compile it with the following command:

gcc -shared -o libhello.so -fPIC hello.c
gcc -shared -o libhello.so -fPIC hello.c

Example hello.c that exports the hello function:

int hello()  return 8; // my lucky number >

Let’s do this slightly differently. In this post, we learn that you can now use Delphi to compile the source code into Linux 64-bit Server native code directly.. So, let’s do this by creating a tiny Delphi DLL/*.so library. This library exports a single function that takes a double and returns its double value.

build-dll-using-delphi-for-linux-64-bit How to Use the Dynamic Link Library in C++ Linux (gcc compiler)? c / c++ delphi programming languages

And, as you hit F9 to build, you will have the libProject6.so file, the Linux *.so library (if you change target platform to Windows, the code will be compiled into Project6.dll automatically, that is the beauty of the Delphi IDE).

Copy the file to Ubuntu and create the following C/C++ source code in the same directory.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// https://helloacm.com/how-to-use-the-dynamic-link-library-in-c-linux-gcc-compiler/ #include #include #include // function export from the *.so typedef double (*double_ptr)(double); int main(int argc, char **argv)  char *error; double_ptr GetDouble; void *handle; // open the *.so handle = dlopen ("./libProject6.so", RTLD_LAZY); if (!handle)  fputs (dlerror(), stderr); exit(1); > // get the function address and make it ready for use GetDouble = (double_ptr)dlsym(handle, "GetDouble"); if ((error = dlerror()) != NULL)  fputs(error, stderr); exit(1); > // call the function in *.so printf ("%f\n", (*GetDouble)(2.0)); // remember to free the resource dlclose(handle); >
// https://helloacm.com/how-to-use-the-dynamic-link-library-in-c-linux-gcc-compiler/ #include #include #include // function export from the *.so typedef double (*double_ptr)(double); int main(int argc, char **argv) < char *error; double_ptr GetDouble; void *handle; // open the *.so handle = dlopen ("./libProject6.so", RTLD_LAZY); if (!handle) < fputs (dlerror(), stderr); exit(1); >// get the function address and make it ready for use GetDouble = (double_ptr)dlsym(handle, "GetDouble"); if ((error = dlerror()) != NULL) < fputs(error, stderr); exit(1); >// call the function in *.so printf ("%f\n", (*GetDouble)(2.0)); // remember to free the resource dlclose(handle); >

The RTLD_LAZY resolves undefined symbols when code from the dynamic library is executed, which is faster to open. Alternatively, RTLD_NOW resolves all undefined symbols before dlopen returns and fails if this cannot be done.

Compile the source code using (-ldl, the Interfaces for Dynamic Loader):

Источник

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.

Источник

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

Источник

Читайте также:  Using screen on linux
Оцените статью
Adblock
detector