Detailed steps for creating a C Shared Library

Source: Internet
Author: User

1) Basic Concepts
The shared library is also. o file set, but these files are generated by the compiler in a special way (in Linux, the shared library file is in the "ELF" format, and the shared library already has executable conditions ).

The code of the shared library is loaded into the memory only when the executable program runs. It is only referenced in the compilation process, so the code size is small.

The addresses (variable references and functions) of each member in the module are relative addresses. When a program using this shared library is running, the shared library is dynamically loaded into the memory and linked to the main program in the memory. Multiple executable programs can share the code segment of the library file (not the data segment ).

Shared Library Member objects can be executed (supported by libdl. so ).

2) how to create and use a shared library
1. Compile the source file:
Source code 1: my_strcpy.c :( implement a strcpy function)
# I nclude <stdio. h>
# I nclude <string. h>
# I nclude <stdlib. h>

Void my_strcpy (char * des, const char * src)
{
While (* des = * src );
}

Source code 2: my_strcmp.c (implement a strcmp function)
# I nclude <stdio. h>
# I nclude <string. h>
# I nclude <stdlib. h>

Int my_strcmp (const char * obj1, const char * obj2)
{
While (* obj1 & * obj2)
{
If (* obj1-* obj2)
{
Return (* obj1-* obj2 );
}
Else
{
Obj1;
Obj2;
}
}
Return 0;
}

2. Generate a. o file
Gcc-fPIC-c my_strcpy.c my_strcmp.c
Note: What is different from creating a static library here is to add the parameter-fPIC, which means to generate code irrelevant to the location, this parameter must be specified because the relative address (offset) is used for the shared library link.

3. Create a shared library
Gcc-shared-Wl,-soname, libmylib. so.1-o libmylib. so.1 *. o
-Shared indicates that a shared library is to be created.-Wl and option indicates that the option is passed to the linker. Here, soname is passed to the linker to specify the version number of the shared library, -o is followed by the actual shared library name. Note that the version number of the shared library is the same as the actual name, so that we do not need to create a symbolic link to point to the actual name, you can save a symbolic link. The version number of the shared library is saved in the actual library. Run this command: readelf-a libmylib. so.1 | grep libmylib. so.1:
0x0000000e (SONAME) Library soname: [libmylib. so.1]

The version number of the shared library is reserved for the dynamic loader (dl). The dl will go to the library to find the version number to complete the dynamic loading function.

Now, the dynamic loader can find the shared library we created, but the compiler cannot find this library yet. We must create a soft link to the actual library file, the soft link file name must start with lib and start. so, which is the format required by the compiler. So we only need to execute: ln-s libmylib. so.1 libmylib. so.

That is to say, the shared library is different from the static library. The static library is only required during compilation, while the shared library is required during compilation and loading, because it is not actually compiled into the executable program, the program only saves the symbol reference to the library function.

4. Test the Shared Library
The test code main. c is the same as the static library

Compile: gcc-o main. c-L.-lmylib
We will find that the compilation is successful, but the execution of./main compile will print:

./Main: error while loading shared libraries: libmylib. so.1: cannot open shared object No such file or directory

This message indicates that an error occurred while loading the shared library. The loader cannot find libmylib. so.1. Why? This is because the loader loads the Shared Library only in the path specified by the system by default. The specified paths include/usr/lib/AND/lib /. There are two ways to solve this problem: Execute: export LD_LIBRARY_PATH =. /Add the current path to the environment variable of the loader's loading path. Of course, this command must be re-executed every time the terminal is opened; the second method is to create a soft link libmylib under/usr/lib/or/lib. so.1 point to the real library file libmylib. so.1, this way we can find our library files every time we load them, but we do not recommend this approach unless the shared library we create is very mature and often used.

Execute./main this time to see the result:
Hello linux.
Hello world.
S1 <s2

3) Summary
The shared library is also a set of. o files, but it is in ELF format.

Shared libraries are loaded only when the program starts to run. during compilation, you only need to specify the library functions to be used.

A dynamic library is another form of change for shared libraries. The dynamic library is also loaded when the program is running, but unlike the shared library, the library function used does not actually start to run the program, but is loaded only when the statements in the program need to use the function. The dynamic library can release the memory occupied by the dynamic library during the running of the program. The shared library and dynamic library do not include the library function content in the program, but only contain references to it, so the code size is small.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.