Static library and shared library

Source: Internet
Author: User

Create and use static databases

1. Create a directory
Mkdir-P test/sub

2. Write hello. C and hello. h under sub /.
/***** Hello. c *****/
# Include <stdio. h>
# Include "Hello. h"
Void Hello ()
{
Printf ("hello! /N ");
}
 
/***** Hello. h *****/
# Include <stdio. h>
Void Hello ();

3. Compile the link/package
Gcc-C hello. C-O hello. o
Ar cqs libhello. A hello. o

4. Write main. c In the main directory test /.
# Include <stdio. h>
# Include "sub/Hello. H"
Int main ()
{
Hello ();
Retrun 0;
}

5. Compile the link
Gcc-C main. C-o main. o
GCC main. O-o main.exe-L "sub/"-lhello # dynamically linked (uses SHARED libs)
Or GCC main. O-o main.exe-static-L "sub/"-lhello # statically linked

Bytes --------------------------------------------------------------------------------------------------

. A (archive) static library
. So (shared object) Shared Library
The static library only works when the program is linked, and the final execution program runs out of the static library.
The shared library works when the program is running.

Bytes --------------------------------------------------------------------------------------------------

Number conventions of shared libraries

Format: library_name.major_num.minor_num.patch_num
For example, libminigui. so.2.0.0
Major_num: When the database changes to Chengdu that is not compatible with the previous version, the main version number will be added.
Minor_num: the version number is changed only when the database is changed and compatible with the previous version.
Patch_num: changes made to fix errors in the library change the patch level number, also known as the release number ).
Database ending with _ g and _ p:
For example, libminigui_g.a and libminigui_p.a
They are special versions of the basic library, which is libminigui.. The library ending with _ g is a debugging library, which is compiled into special symbols and functions, and added debugging functions for applications using this library. The database ending with _ p is a profiling, which contains code and symbols for complex code analysis and performance analysis. If you use these two class libraries, once debugging and profiling are completed, you need to use the normal library to re-compile your program.

Bytes --------------------------------------------------------------------------------------------------

Database operation commands

Nm
Function
List all symbols that are compiled into the target file or binary file. Purpose 1: Check the functions called by the program. Purpose 2: Check whether a given library or target file provides the required functions.

Syntax
NM [Options] File

Common options
-C converts a symbolic name to a user-level name. It is particularly useful in making C ++ function names readable.
-S: when used in the. A file, the output maps the symbol name to the index of the module or member name that defines the symbol.
-U only displays undefined symbols, that is, files externally defined in the file to be checked.
-L use the debug information output to define the line number of each symbol or the important bit of the undefined symbol.

Ar
Function
Combine multiple. O files into a. a file.

Syntax
Ar [Options] lib *. A *. o

Common options
-C if the archive file does not exist, create the file and do not display the AR warning.
-Q: Add *. O to the end of the archive file without checking whether to replace the file.
-R inserts the. o file into the archive file, replaces any existing files with the same name, and adds new members to the end of the file.
-S creates or upgrades the cross-index ing table from the symbol to the. A file and adds it to The. A file.
It is equivalent to ranlib [*. A]. After executing this command, you can use nm-s to view the generated index.

LDD
Function
Displays the shared libraries required for executable programs.

Syntax
LDD [Options] File

Common options
-D. Execute the relocation and report all the missing functions.
-R migrates functions and data objects and reports any missing functions or data objects.

Ldconfig
Function
In the default search directory (/lib and/usr/lib) and dynamic library configuration file/etc/lD. so. search for the shared dynamic link library (lib *. so *) to create a dynamic loader (LD. so) the required connection and cache file. The default cache file is/etc/lD. So. cache. This file stores a list of Dynamic Linked Library names that have been sorted.
This command is run when the system starts. When you install a new dynamic link library, you need to manually run this command.

Syntax
Ldconfig [Options] path
For example, ldconfig/root/lib allows the system to share the dynamic link library under the/root/lib directory, that is, add the shared library under the specified directory to/etc/lD. So. cache. [Note] If the directory is not in/lib,/usr/lib,/etc/lD. in the directory list listed in soconf, when ldconf is run again, the dynamic link library under this directory will not be shared by the system.

Common options
-V updates/etc/lD. So. cache content, version numbers of each database in the column, scanned directories, and all created and updated links.
-P Only displays the content of/etc/lD. So. cache, that is, the current list of shared libraries known to lD. So.
-N ldconf only scans the directory specified by the-n command
-F conf specifies that the configuration file of the dynamic link library is Conf, and the default value is/etc/lD. So. conf.
-C cache specifies that the generated cache file is cache. The default value is/etc/lD. So. cache.
When ldconf does not include any option, only the high-speed buffer file is updated.

Environment Variable
$ Ld_preload: list of shared libraries separated by spaces, which are loaded before other libraries, so that they have the opportunity to overwrite or
Redefines the standard library.
$ LD_LIBRARY_PATH is a list of directories separated by colons, which are accessed during shared library search.

Bytes --------------------------------------------------------------------------------------------------

Create and use a dynamic library

Create a dynamic library
1. GCC-FPIC-C hello. C-O hello. o
2. GCC-shared-wl,-soname, libhello. So-O libhello.1.0.0 hello. O-lC
Parameter description
-FPIC generates code irrelevant to the location and can be loaded to any location. -Similar to FPIC, FPIC will generate smaller and faster
Code, but there are platform restrictions.
-Shared tells the compiler to generate the shared library code.
-Wl tells the compiler to pass the following parameters to the linker lD.
-Soname specifies the short for shared object name for the shared library ).
-L specifies the Library File Name of the link.
-L add a new directory to the GCC library file search path

Use Dynamic library
Method 1
1) copy the generated. So file to the default folder that stores the library file, such as/usr/lib. If you copy
Libhello. so.1.0.0 to/usr/lib, a symbolic link libhello. So is automatically generated.
2) Run ldconfig as root to update the high-speed buffer/etc/lD. So. cache.
3) GCC-C main. C-o main-lhello
Method 2
1) change the environment variable $ LD_LIBRARY_PATH = "your dir"
2) GCC-C main. C-o main-L "sub"-lhello
Method 3
1) Add the path of your shared library to/etc/lD. So. conf.
2) Run ldconfig as the root user
3) GCC-C main. C-o main-L "sub"-lhello

Bytes --------------------------------------------------------------------------------------------------

Share objects Dynamically Loaded

You only need to include <dlfcn. h> in the source code, and then use the-LDL command in the compilation command or makefile to link to the libdl library.
[Note] You do not need to link the library you want to use. Even if you use a standard shared library, you do not have to use it as usual. The linker does not know the shared object. These modules may not even exist when compiling and linking applications.

Dlopen
Load the shared object. If filename is found, a handle is returned. Otherwise, null is returned.
Void * dlopen (const char * filename, int flag );

Dlsym
Use shared objects. Dlsym searches for the symbols or functions named in symbol in the loaded object (shared object pointed by handle. The handle parameter must be the handle returned by dlopen; the symbol parameter is a standard C string. Dlsym
Returns a null pointer to the symbol. If an error occurs, null is returned.
Void * dlsym (void * handle, char * symbol)

Dlerror
Check errors. If any function has an error, dlerror returns a string with an incorrect description, and then sets the error string to null.
Const char * dlerror (void );

Dlclose
Uninstall the shared library. Disable a shared object to save system resources.
Int dlclose (void * handle );

 

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.