Dynamic library *. so is often encountered in C and C ++ programming in Linux. Recently, I have found several articles on the website to introduce the compilation and link of dynamic libraries, after finally understanding this, I have never been quite familiar with it. Here, I will take a note and provide some help to other brothers who are worried about the dynamic Library Link Library.
1. Dynamic library Compilation
The following example shows how to generate a dynamic library. Here is a head file: so_test.h, three. c files: test_a.c, test_ B .c, and test_c.c. We compile these files into a dynamic library: libtest. So.
So_test.h:
# Include <stdio. h>
# Include <stdlib. h>
Void test_a ();
Void test_ B ();
Void test_c ();
Test_a.c:
# Include "so_test.h"
Void test_a ()
{
Printf ("this is in test_a.../N ");
}
Test_ B .c:
# Include "so_test.h"
Void test_ B ()
{
Printf ("this is in test_ B.../N ");
}
Test_c.c:
# Include "so_test.h"
Void test_c ()
{
Printf ("this is in test_c.../N ");
}
Compile these files into a dynamic library:Libtest. So
$ GCC test_a.c test_ B .c test_c.c-FPIC-shared-O libtest. So
2. Dynamic library links
In 1, we have successfully generated a dynamic link library libtest. So. Next we use a program to call the functions in this library. The source file of the program is test. C.
Test. C:
# Include "so_test.h"
Int main ()
{
Test_a ();
Test_ B ();
Test_c ();
Return 0;
}
L link test. C to the dynamic library libtest. So to generate the execution file test:
$ GCC test. C-L.-ltest-o Test
L test whether the connection is dynamic. If libtest. So is listed, the connection is normal.
$ LDD Test
L execute test and you can see how it calls functions in the dynamic library.
3. Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
L-FPIC: indicates the code compiled into a location independent, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.
L-L.: indicates that the database to be connected is in the current directory.
L-ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
L LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
L of course, if you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
4. Note
When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.
In makefile, how does one correctly compile and connect to generate the. So library file, and then how to compile and connect in The makefile of other programs to call the function of this library file ????
A:
You need to tell the dynamic linker and loader lD. so where can I find this shared library? You can set the environment variables to add the library path to the library directory/lib and/usr/lib, LD_LIBRARY_PATH = $ (PWD ), this method is not convenient to use the command line method. It is an alternative method.
^ ^
LD_LIBRARY_PATH can be in/etc/profile or ~ /. Profile or./bash_profile, or. bashrc,
Run source/etc/profile or./etc/profile after modification.
A better way is to add/etc/lD. So. conf and then execute/sbin/ldconfig
^ ^
Is to add the library path to/etc/lD. So. conf, and then run ldconfig as root
You can also specify the file path and name-I-l.
GCC = gcc
Cflags =-wall-ggdb-FPIC
# Cflags =
ALL: libfunc Test
Libfunc: func. O func1.o
$ (GCC)-shared-wl,-soname, libfunc. so.1-O libfunc. so.1.1 $ <
Ln-SF libfunc. so.1.1 libfunc. so.1
Ln-SF libfunc. so.1 libfunc. So
**************************************** ******************************** ****************
Ln-S is used to create soft links, which is equivalent to a shortcut in windows. In the current directory, the command for creating the file TTT in the upper-level directory named ttt2 soft link is ln-s .. /TTT ttt2. If the original file is deleted as the TTT file, ttt2 becomes an empty file.
Ln-D is used to create a hard link, which is equivalent to a copy of a file in windows. When the original file is deleted, the content of the "copy" is not affected.
When compiling the target file, use the-FPIC option of GCC to generate location-independent code and load it to any address:
Gcc-FPIC-g-C liberr. C-o liberr. o
Use the-shared and-soname options of GCC;
Use the-wl option of GCC to pass the parameter to the connector LD;
Use the-L option of GCC to display the connection to the C library to ensure that the required startup code can be obtained, so that different programs can be used, the execution may not be started on the System of the Incompatible version C library.
Gcc-g-shared-wl,-soname, liberr. So-O liberr. so.1.0.0 liberr. O-lC
Create a symbolic connection:
Ln-s liberr. so.1.0.0 liberr. so.1;
Ln-s liberr. so.1.0.0 liberr. So;
In makefile:
$ @
Indicates the target file set in the rule. In a pattern rule, if there are multiple targets, "$ @" is the set that matches the schema definition in the target.
$ %
Only when the target is in the function library file, it indicates the target member name in the rule. For example, if a target is "foo. A (Bar. o) ", then," $ % "is" bar. O "," $ @ "is" foo. A ". If the target file is not a function library file ([. A] in UNIX and [. Lib] in Windows), the value is null.
$ <
The first target name in the dependency target. If the dependency target is defined in the mode ("%"), "$ <" is a series of file sets that conform to the mode. Note that it is obtained one by one.
$?
A set of new dependent targets. Separated by spaces.
$ ^
A set of all dependent targets. Separated by spaces. If there are multiple duplicate dependency targets, the variable will remove the duplicate dependency targets and retain only one copy.
**************************************** ********************************** *************************************
Test: Test. O libfunc
$ (GCC)-O test. O-L.-lfunc
%. O: %. c
$ (GCC)-C $ (cflags)-o $ @ $ <
Clean:
Rm-Fr *. o
Rm-Fr *. So *
Rm-fr Test
To generate. so file, CC must contain the-shared parameter; to call. so files, such as libfunc. so, you can add-lfunc at the end of the CC command, and add-L/usr/xxx to indicate libfunc. so path. In this way, you can call libfunc in the source file to be compiled. so the function of this library file.
I mentioned above, but I 'd like to remind you that it is best to provide an interface header file.
Dynamic Loading, with dlopen, dlclose, dlsym