Static libraries and dynamic libraries for C

Source: Internet
Author: User

1 (1) library files: Static library files and shared library files
(2) comparison
A. Static library file:
Copy code/instructions directly to the target file when using a static library file
The target file will appear large, modification and maintenance are inconvenient
Can be separated from the static library file, high efficiency

B. Shared library files:
When using a shared library, copy the address of the code/Directive to the destination file
The target file will be small, modification and maintenance is more convenient
cannot be detached from shared library files, less efficient
(3) Basic commands
LDD a.out = View the library file that the file a.out depends on
Gcc/cc-static xxx.c = requires a static library to compile the link, the resulting executable file will be larger

2 generation and use of static library files
(1) The process of generating static library files
A. Write source code xxx.c, such as: VI adder.c
B. target files that only compile without chaining to. O
Gcc/cc-c adder.c
C. Insert the */LIB library name using the command ar-r/*. A xxx.o generate a static library
Ar-r LIBADDER.A ADDER.O
Attention:
A.lib Library name. A is called a static library file name, and unlike the library name, note the distinction between
B. The naming rules for static library files generally start with Lib,. A is a suffix, and the middle library name can be customized

(2) Procedures for using static library files
A. Writing source program xxx.c, such as: VI MAIN.C
B. Compile the non-link only, generate the target file. O
such as: Cc-c MAIN.C
C. Connecting the destination file and the static library file
1) Direct connection
CC MAIN.O LIBADDER.A
2) Connect via option (Master)
The path of the CC MAIN.O-L Library name-L library file
CC Main.o-l adder-l.
3) Configure the environment variable Libarary_path
Export library_path= $LIBRARY _path:.
CC Main.o-l Adder

3.3 Steps for building and using shared libraries
(1) Steps to build a shared library
A. Writing source program xxx.c, such as VI adder.c
B. Compile-only non-chained births to. O Target files
GCC/CC-C-fpic/* Small mode */ADDER.C
C. Generating a shared library file using compilation options
gcc/cc-shared/* Share */adder.o-o Lib library name. So
(2) Steps to use shared libraries
A. Writing test procedures xxx.c, such as VI main.c
B. Compile-only non-chained births to. O Target files
such as: Cc-c MAIN.C
C. Connecting test files and shared library files
1) Direct connection
CC MAIN.O libadder.so
2) Connect using the Compile option (master)
CC Main.o-l adder-l.
3) Configure the environment variable Library_path
Export library_path= $LIBRARY _path:.
CC Main.o-l Adder
Attention:
For the use of shared library files, the configuration Ld_library_path is required
Export ld_library_path= $LD _library_path:.

Dynamic loading of shared libraries
(1) Dlopen function
#include <dlfcn.h>
void *dlopen (const char *filename, int flag);
First parameter: file name as a string
Second parameter: Load flag
Rtld_lazy-Lazy Loading
Rtld_now-Load Now
Return value: Returns the handle/address of the shared library file
function function: Open and load shared library files, according to the return value can be
Manipulate the shared library file

(2) Dlerror function
Char *dlerror (void);
function function:
Used primarily to get the most recent error message that occurs during the Dlopen ()/dlsym ()/dlclose ()/function call.
Return by return value, return null without error

(3) Dlsym function
void *dlsym (void *handle, const char *symbol);
First parameter: The return value of the Dlopen function
Second parameter: identification, typically used to find a specific function name
Return value: Returns the address identified in memory
function function:
Find the address named symbol in the corresponding shared library according to handle

(4) Dlclose function
int dlclose (void *handle);
function function:
Indicates that the shared library file corresponding to the handle is closed

Attention:
You need to specify an option when connecting:-LDL

Examples of dynamic library use:

// header file for addition calculation #ifndef Adder_h #define Adder_h// computes the int add_int (int ia,int  IB) of two int type parameters  ; // calculates the and of two double type parameters double add_double (double da,double  db); #endif
//source file for addition calculation#include"Adder.h"//#include ". /adder.h "//calculates the and of two int type parametersintAdd_int (intIaintIB) {    returnIA +IB;}//calculates the and of two double type parametersDoubleAdd_double (DoubleDaDoubledb) {    returnDa +DB;}

//dynamic loading of shared libraries#include <stdio.h>#include<dlfcn.h>intMainvoid){    //1. Open and load shared library files    void* Handler = Dlopen ("./shared/libadder.so", Rtld_now); //2. Determine if there is an error    Char* ERROR =Dlerror (); if(Error! =NULL) {printf ("failed to load shared library file \ n"); return-1; }    //3. Find the identity specified in the shared library    int(*add_pint) (intIaintIB); Add_pint= Dlsym (Handler,"Add_int"); //4. Determine if there is an errorError =Dlerror (); if(Error! =NULL) {printf ("failed to get identity \ n"); return-1; }    //5. Handling with the logoprintf"The result of the calculation is:%d\n", Add_pint (Ten, -));// -//6. Close the shared library fileDlclose (handler); return 0;}

Static libraries and dynamic libraries for C

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.