Generation and use of dynamic function library and static function library in C language (under linux environment)

Software development is often a very huge project. It requires a lot of brainpower. With the help of libraries that others have developed, efficiency can often be improved. The following will introduce how to develop and use shared library files. To use libraries that others have developed, just For example, it is very difficult for us to build a car, but if the major parts of the car already exist and are available, the work we have to do is to assemble, and the assembly process must be easier than designing these parts.

 

The function library is divided into two kinds of static (static) function library and dynamic (shared) function library. Both are collections of functions. The difference: the content of the static function library will be added to the target program during compilation, and the target program has The code of the function library; the dynamic function library only adds the functions in the function library to the target when it is executed, and there is no code of the function library in the target program.

 

Libraries under Linux

Dynamic: xxxx.so

static: xxxx.a

Libraries under Windows

Dynamic: xxxx.dll

static: xxxx.lib

 

Common C language library

Libpng

Libjpeg

Libmysql etc.

 

1. Use gcc to compile a dynamic library file and use a dynamic library

Gcc -shared source file -o object file

Where -shared indicates that the compiled result is a dynamic library file, and the object file is often named libxxx.so.

 

For example, create a file test.c and test.h.

Test.c:

#include <stdio.h>

int print()

{

printf("hello this is lib");

}

 

Test.h

int print();

 

 

Use gcc -shared test.c -o libtest.so (the compiled result is best to start with lib, which can be omitted when calling)

The dynamic function library of the file libtest.so is generated (-shared means the dynamic function library is generated, -o means the generated object file).

 

Write a program (in call.c) to call print() in this library

#include "libtest.h"

intmain ()

{

print();

return 0;

}

 

 

command when compiling

gcc call.c -ltest -L. -o c

 

Where -lxx means to call the dynamic function library, libtest.so, if the format of the library file is not libxx.so, you can also use -l xxx.so

-L. Indicates that the function library is under the current folder

If you copy libtest.so to /usr/lib or /usr/local/lib, you can use the command gcc call.c -ltest -o c without the -L. parameter.

 

If you run ./c directly, you will get an error

#./c
: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

 

Reason: Although the function library has been placed in the system library directory or notified that the function library is current, the system will automatically go to /etc/ld.so.cache to find the recorded function library instead of searching under the folder. So If you want to use the function library, you also need to use the command ldconfig to search for the function library and add it to the system ld.so.cache.

If you are in the current path, you can add the current path to /etc/ld.so.conf, and then run ldconfig to add the current file to ld.so.ccache.

It is also possible to modify the temporary system library variable LD_LIBRARY_PATH. Use the command in the terminal

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../  

(shell command, you can use echo $LD_LIBRARY_PATH to view variables, close the terminal and need to reconfigure it after opening)

After that, it can be run.

#./c

#hello this is lib

 

ldconfig is a dynamic link library management command. If you want to know more, you can check the details of the ldconfig command carefully.

There is also a command ldd, which is to see what kind of dynamic library the program needs.

#ldd c

linux-gate.so.1 =>  (0xb77bd000)

libtest.so => not found

libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75fc000)

/lib/ld-linux.so.2 (0xb77c0000)

 

2. Compile into a static library file and use

 

We use the examples used above libtest.c and libtest.h, call.c.

First compile libtest.c into a static function library.

Gcc -c libtest.c   

The difference between compiling libtest.c into libtest.o and not adding -c is that -c does not link

ar crv libtest.a libtest.o 

Ar is to generate a static library from the file. For details, refer to man ar

Compile the target program

Gcc call.c libtest.a -o c

Then you can use ./c to run.

Because the static library is added to the program when it is compiled, after the target is compiled, the xxx.a file is not needed.

Although the static library is relatively simple in comparison, the code using the static library is often very large.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324761789&siteId=291194637