3

What is the actual use case of shared object (.so) file ?

As per my understanding it is used to link with other higher level languages like JAVA (JNI), etc. Also it is used to create the library files for various OS flavors/processor architecture.

How to make use of this feature ?

For example, if I want to create an generic executable and needs to be work in all flavors of Linux OS is the shared object file can be used ? How it is done ?

1

2 Answers 2

1

To make use of Shared objects, First you need to create a shared library. It is also called Dynamic library.

The Shared libraries are used in Dynamic run time linking. That means at running time of the program these shared libraries are loaded into RAM.

Command to create Dynamic library or Shared library-

cc -shared -o libany_name.so file1.o file2.o file3.o

where any_name name represents you can give any name to the library. For ex- libadd.so

You can execute your program with Shared library by-

cc filename.c libany_name.so

When you create a executable file-

cc filename.c 

or

cc filename.c libany_name.so

it will create a Dynamically Executable file. These Dynamically executable files needs some dependency files in run time. Those dependency files are automatically loaded into by Compiler.

To avoid this go with Static library or Static Executable files. Static executable file contains all the functions itself. The size of the executable file will be more.

Command to create a static library-

ar rcs libany_name.a file1.o file2.o file3.o

Command to create a static executable file-

cc -static filename.c
1

To complement Satish's answer, ELF shared objects have generally some shared text segments (in addition of some private, unshared, segments), which are mmap(2)-ed in virtual memory using MAP_SHARED. A typical Linux system has many -hundreds- shared libraries (e.g. /usr/lib/lib*.so files), so they may share some common RAM (two different processes using the same library would typically share the RAM for its text segment) and updating them is easy (if you update some lib*.so future runs of programs linked to it will use the updated version).

Shared objects can also be dynamically loaded at runtime using dlopen(3) (then dlsym to get some symbols inside them). This is how plugins are loaded. The loading application has to define and document some conventions, e.g. requirements about symbols to be found in the plugins and how they are used.

A good reference on that is Drepper's paper: How to Write Shared Libraries ; see also Levine's book: Linkers and Loaders and Advanced Linux Programming.


Your generic executable goal might be an illusion. First, there are various Linux systems running on different processors or ABI (x86 32 bits, x86-64, x32, ARM). Then, even if restricting to x86-64 Linux, various distributions have different versions of libc and of other libraries needed by your executable.

The simplest way would be to make your program free software (so publish its source code); then it could be packaged by distribution makers. Also, it would help if you packaged your software for common distributions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.