Linking with x86 shared library on Linux x64


Introduction

Dynamic Link Libraries, also known as Shared Libraries, are collections of compiled code, data, and resources that can be loaded and executed by an application at runtime. Shared libraries are commonly used in development of Linux applications, and are an integral part of x86 architecture. In this article, we will explore process of linking with x86 shared libraries on Linux x64 systems.

Understanding x86 Shared Libraries

An x86 shared library is a file that contains compiled code, data, and resources that can be shared among multiple applications at runtime. Shared libraries are loaded into memory only when required by an application, reducing memory footprint of application. Shared libraries are commonly used to share code among multiple applications, or to provide additional functionality to an application.

The x86 shared library format is designed to be platform independent, which means that a shared library compiled on one system can be used on another system, as long as both systems have same architecture. This makes x86 shared libraries an ideal solution for creating portable software.

Linking with x86 Shared Libraries

To link an application with an x86 shared library on Linux x64, we need to follow these steps −

Step 1: Create a shared library

The first step is to create a shared library that we want to link with our application. We can use any programming language to create a shared library, as long as it is compiled for x86 architecture.

For this example, we will use C programming language to create a shared library called libexample.so. We will create a simple function in shared library that adds two integers and returns result.

Here is code for our shared library −

int add(int a, int b) {
   return a + b;
}

To create shared library, we need to compile code using following command −

gcc -shared -o libexample.so example.c

This command will create a shared library called libexample.so in current directory.

Step 2: Link application with shared library

The next step is to link our application with shared library. To do this, we need to use -l option with linker command.

Here is code for our application −

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
   int a = 10;
   int b = 20;
   int c = add(a, b);
    
   printf("The result is: %d
", c); return 0; }

To link application with shared library, we need to compile code using following command −

gcc -o app app.c -L. -lexample

This command will link our application with shared library libexample.so.

Step 3: Run application

The final step is to run our application. We can do this by executing following command −

./app

This will execute our application and print result of function call to console.

Additional Considerations

When linking with shared libraries, there are some additional considerations that we need to keep in mind. Here are a few of them −

Library Search Path

When linking with a shared library, linker needs to know where to find library. By default, linker looks for libraries in a few standard directories, such as /usr/lib and /usr/local/lib. However, if our library is located in a different directory, we need to tell linker where to find it.

We can do this by using -L option, followed by directory path. For example, if our library is located in directory /home/user/lib, we can use following command to link our application with library −

gcc -o app app.c -L/home/user/lib -lexample

Symbol Visibility

When creating a shared library, we can specify visibility of symbols that are exported by library. By default, all symbols in a shared library are visible to other applications that link with library. However, this can lead to naming conflicts if multiple libraries export symbols with same name.

To avoid this, we can use visibility attribute to specify visibility of each symbol. There are three levels of visibility: default, hidden, and protected. default visibility means that symbol is visible to other applications that link with library. hidden visibility means that symbol is only visible within library. protected visibility is similar to hidden, but allows derived classes to access symbol.

To specify visibility of a symbol, we can use attribute ((visibility("visibility_type"))) attribute. For example, to make a function called add() hidden, we can use following code −

int __attribute__ ((visibility("hidden"))) add(int a, int b) {
   return a + b;
}

Linker Flags

When linking with shared libraries, we can use a number of linker flags to customize linking process. Here are a few commonly used flags −

  • -shared − This flag is used to create a shared library. It tells linker to generate a shared library instead of an executable.

  • -fPIC − This flag is used to generate position-independent code. It is required when creating shared libraries, as it allows library to be loaded at any address in memory.

  • -Wl,-rpath=directory − This flag is used to specify runtime library search path. It tells linker to include directory in list of directories that are searched for shared libraries at runtime.

Conclusion

In this article, we have explored process of linking with x86 shared libraries on Linux x64 systems. We have seen how shared libraries can be used to share code among multiple applications, and how they can be used to provide additional functionality to an application. By following steps outlined in this article, we can easily link our applications with x86 shared libraries on Linux x64 systems.

Updated on: 03-Mar-2023

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements