Under Linux production and use of static and dynamic libraries

Outline

      Linux operating system support library is divided into static and dynamic libraries, dynamic libraries known as shared libraries. There are several important linux system directory for storing respective libraries, such as / lib / usr / lib.

Static library:

  The names of these libraries are generally libxxx.a; use of static libraries compiled into the files are large because all the data for the entire library will be integrated into the object code in, his advantage becomes apparent that the implementation of the compiled program the need for external library support, because the use of all functions have been compiled into executable files. Of course, this will be his disadvantage, because if the static library changes, then your program must be recompiled, and the volume is large.

Dynamic libraries:

  The names of these libraries are generally libxxx.so, dynamic libraries known as shared libraries; relative to a static library, dynamic library at compile time and has not been compiled into object code, when you call the program execution to the correlation function the function corresponding to the function library, and therefore the dynamic library executable file generated is relatively small. Since the library is not integrated into your application, but dynamic application and call the program is running, so the operating environment of the program must provide the appropriate library. Change the dynamic library does not affect your programs, so upgrading dynamic libraries more convenient. And if a plurality of applications have to use the same library, it is very suitable for a dynamic library, can reduce the size of the application.

Create and use Linux static library

例程add.h add.c sub.h sub.c main.c:

add.h

#ifndef ADD_H
#define ADD_H
int add(int x,int y); 
#endif
add.c
#include <stdio.h>
#include "add.h"
int add(int x, int y)
{
    return (x + y);
}
sub.h
#ifndef _SUB_H_ 
#define _SUB_H_  
int sub(int x, int y);
#endif
sub.c
#include <stdio.h>
#include "sub.h"
int sub(int x, int y)
{
    return (x - y);
}
main.c
#include <stdio.h>
#include "sub.h"
#include "add.h"
 
int main()
{
    int a, b;
    a = add(1, 2);
    b = sub(10, 5);
    printf("a = % d, b = % d\n", a, b);
    return 0;
}

Whether it is static or dynamic library library, are generated by the * .o object files.

So first

gcc -c add.c 
gcc -c sub.c

Generated add.o sub.o

Static library created by the ar command

This Example:

ar -cr libaddsub.a add.o sub.o

parameter:

-c the Create meaning

-R & lt Replace meaning, represents when inserted module name already exists in the library, the replacement modules with the same name. If there are several modules in a module is not present in the library, ar displays an error message, is not replaced by another module of the same name. By default, new members increase at the end of the library, you can use any of the other options to change the location of the increase.

This static library is created.

Instructions:

by

gcc -o main main.c -L. -laddsub

Compiling main.c will put static library integrated into the main.

parameter:

-L static library specified location in the lookup, and there note L '' represents a static library catalog look at.

-l specifies a static library name, due to the naming of a static library lib ***. a, where the lib and .a ignored.

According to the characteristics of static libraries, delete libaddsub.a main can still run here, because the contents of the static library has been integrated into it.

Creating and using Linux dynamic library

gcc -shared -fpic -o libaddsub.soadd.c sub.c

parameter

-fpic: generate position independent code

-shared: building a shared library

Libaddsub.so dynamic libraries generated using the above command.

gcc -o out main.c -L. -laddsub

This time can not ./out immediately, because in the dynamic library use, looks for / usr / lib / dynamic libraries in the lib directory, but this time we are not generated inside the library.

This time there are good several ways to allow him to run successfully:

1. The most direct and simplest way is to pull libaddsub.so / usr / lib or / lib go.

2. Another method, assuming libaddsub.so at / home / linux / addsub

export LD_LIBRARY_PATH=/home/linux/addsub:$LD_LIBRARY_PATH

3. It also can join our catalog generated in the /etc/ld.so.conf file, and / sbin / ldconfig.

/etc/ld.so.conf is very important to a directory, which is stored in the linker and loader search for shared libraries to check the directory, the default is read from / usr / lib / lib in, so I want to running smoothly, we can also put our library catalog is added to the file and execute / sbin / ldconfig.

Linux hierarchy of dynamic and static link libraries and dynamically linked distinguish Figure

Library hierarchy in Figure 1. Linux
clip_image0025d48b87e-0ec4-493c-a4f7-6303189d5571

Figure 2. Static and dynamic links link
clip_image00423005275-10e9-48d3-a341-f6f0b6ab8f8e[5]

Below a simple example to explain the dynamic link library dynamic and dynamic loading methods:
dynamic library Example:

add.c

int add(int a,int b){ 
        return (a+b);
}
Compile a dynamic library:
gcc -shared -o libadd.so add.c

Libadd.so generate
dynamic linking method : when compiling the program, specify the library file you want to link to, at this time only you need to call the shared library files to their head.
Example:

test.c

#include <stdio.h>
#include <dlfcn.h>
int add(int,int);
int main(int argc, char *argv[])
{
        sum=add(10,11);
        printf("sum=%d",sum);
}

Compiler:

gcc -l add -o test test.c

parameter:

-l parameter indicates the dynamic link library to link, if the path is not available standard library file path comprising -L.

The method of dynamic loading : complete API by the following
Table 1. Dl API

image

#include<stdio.h>
#include<dlfcn.h>
#include"add.h"
int main()
{
       void *dl_handler = NULL;
       int(*func)(int,int);
       char *error;
       int sum;
       dl_handler = dlopen("libadd.so",RTLD_LAZY);
       if(!dl_handler)
       {
              printf("open:%s\n",dlerror());
              return 0;
       }
       func=dlsym(dl_handler,"add");
       error = dlerror();
       if(error != NULL)
       {
              printf("find:%s\n",error);
              return 0;
       }
       sum = (*func)(10,11);
       printf("sum = %d\n",sum);
       dlclose(dl_handler);
       return 0;
}
编译程序:
gcc -o test test.c -ldl

参数:

-ldl 表明将 dllib 链接于该程序,即可调用DL的API

Shared library path: the system can be placed in a shared library directory: under / usr / lib, you can also set the environment variable LD_LIBRARY_PATH

Guess you like

Origin www.cnblogs.com/WindSun/p/11287927.html