I recently read "C expert programming" and saw Chapter 2-Thinking about the link, which serves as a reference to summarize the dynamic library generation and link methods in Linux.
The file extension of the dynamic library in Linux is ". So" (shared object ). According to the Conventions, the file names of all dynamic libraries are in the form of libname. So (versions may be added to the name ). In this way, the thread function library is called libthread. So. The static library file name is in the form of libname.. The file name of the shared archive is in the form of libname. SA. Shared archive is just a transitional form that helps people change from a static library to a dynamic library.
This document describes how to generate and link dynamic library files with simple examples.
Operating System: Debian/GNU Linux 2.6.21-2-686;
GCC version: 4.1.3
I. library files and Test File Code
Directory of the library file and test file:/home/Program /.
1. Library File Name: myfunction. c
/*Author: Godbach E-mail: nylzhaowei@163.com */
#include <stdio.h> int my_lib_function (void) { printf ("Library routine called from libmyfunction.so!\n"); return 0; }
|
2. Test File Name: Test. c
#include <stdio.h> int main(void) { my_lib_function(); return 0; }
|
Ii. Dynamic library Compilation Method
Compile the library file myfunction. C:
Debian:/home/Program # gcc-shared-O libmyfunction. So myfunction. c
|
If the compilation is successful, the dynamic library file libmyfunction. So will be generated under/home/Program.
There are two points to note:
1. Linux operations are generally recommended in normal user mode. If you need Super User Permissions, you can use su root to enter the root user password to switch. I personally learn to use it. At the same time, many operations need to use the root user, so I will compile it directly under the root user.
2. Compile the command to generate a dynamic library: GCC (-FPIC)-shared-O libmyfunction. So myfunction. c
-FPIC allows the output object module to be generated in the form of relocated addresses.
-Shared specifies to generate the corresponding dynamic link library file for the corresponding source file.
Iii. Dynamic library Testing Method
Compile the test. c file:
Debian:/home/Program # gcc-O test. c/home/Program/libmyfunction. So |
After compilation, generate the test file and run test:
Debian:/home/Program #./test Library routine called from libmyfunction. So! |
The last parameter of GCC-O test. c/home/Program/libmyfunction. So specifies the absolute path of the connected library file. In this example, the absolute path of the library file is/home/Program/libmyfunction. So.
Of course, if you want to link the dynamic library from the library file path of the system (usually under/usr/lib of the system function library, you can first copy the generated library file to/usr/lib/And then link it:
Debian:/home/Program # cp libmyfunction. So/usr/lib/ Debian:/home/Program # gcc-O test. C-lmyfunction Debian:/home/Program #./test Library routine called from libmyfunction. So! |
Here, I will explain the link method. For GCC-O test. the last parameter-lmyfunction in C-lmyfunction. It can be seen that the command line parameters passed to the C compiler do not mention the full path name of the function library, or even the full name of the file in the function library directory! In fact, the compiler is told to link to the corresponding function library (under/usr/lib) based on the option-lmyfunction. The function library name is libmyfunction. so, that is, the "lib" part and the file extension are omitted, but an L is added before.
From: http://blog.chinaunix.net/space.php? Uid = 10167808 & Do = Blog & id = 25921