DLL and static library for Linux

2019-09-25

Keywords: build the library, static library references, dynamic library reference


 

In C development, "library" is a term often heard.

 

The so-called library is actually a binary file. The contents of the binary file is a function that can be performed by other C program calls. In other words, the library is packaged as a set of C code only, the package refers to the file compiled source code in the form of libraries generated.

 

But even if it's just packaged source code, it still is compiled by the proprietary nature of the system, that is, different systems are not compatible with each other and library use.

 

C library can be divided into dynamic libraries and static libraries two kinds. Referenced application outside the library at compile time will deal with the relationship between the library and the program source code in the "link period."

 

1, static library

 

Static library name suggests, it is not "dynamic sharing", a program every reference to a static library, the compiler at compile time will go in with a static library, static library is about to be packaged into direct reference to its program.

 

Static library file suffix is ​​.a, that is often said A file.

 

Static library has the following characteristics:

1, the library comprises a direct source, the source library directly packed into the executable program at compile time;

2, at runtime without loading the library file, which is faster;

3, reference the static libraries need to take up more disk space;

4, static database upgrade need to recompile all references to its program;

 

Create a static library:

1, prototype design library;

2, the code base;

3, the compiler generates O library source code file: gcc -c xxx.c -Wall;

4, O by ar command file to generate static library files: ar crs libxxx.a xxx.o;

5, static library information can be viewed through the nm command: nm libxxx.a

Name of a static library in Linux is the rule, it must begin with lib, to .a as a suffix.

 

Call the static library:

1, the need to refer to source code static library functions declared in the function prototype, usually static library file provides a header file called specifically to the use of external programs;

2, direct call function static library where needed;

3, the compiler: gcc -o xxx2 xxx2.c -L -lxxx;

-L is to set the library search path. gcc will default at compile time to / lib, / usr / lib and other official repository directory to find, but we do not necessarily static library compiled on those directories, so to explicitly tell the compiler where to go should look. Behind the dots it means to find a library in the current directory.

-lxxx is specified to link the name of the library. Here we must note, is connected behind the -l library name rather than the name of the library file, simply understood to be removed after the file name in front of the library lib prefix and .a suffixes, and string on the line.

You can also use another compiler option:. Gcc -o xxx2 xxx2.c -L -static -lxxx

4, the compilation is complete, you can run the program directly.

 

2, dynamic library

 

DLL and static library opposite, it is a "global sharing" of. After a program references a shared library, which will record only the shared library which is used when compiling a symbol, and not as the library code is packaged together into this application as a static library.

 

Therefore, the characteristics of dynamic libraries essentially static library opposite:

1, smaller compile the results;

2, multiple programs can share the same base;

3, you need to load the shared library runtime;

4, the upgrade is a direct replacement for the dynamic library file to the library, generally do not need to recompile the referenced library.

 

Creating a dynamic library:

1, prototype design library and implementation code;

2, compiled shared library source code generation O file: gcc -c fPIC xxx.c -Wall;

3, the O file generated as a shared library file: gcc -shared -o libxxx.so.1 xxx.o;

Shared library file naming convention similar to the static library, have a fixed prefixes and suffixes. Xxx is the middle name of the library. .so indicates that the file is a DLL file. .1 generally represents the version number of the library, the library so that the plurality of versions coexist demand can be achieved.

4, creating a symbolic link to the shared library files: ln -s libxxx.so.1 libxxx.so;

 

References dynamic library:

1, the statement library function interface, consistent with the above static library reference method;

2. Compile: gcc -o xxx2 xxx2.c -L -lxxx;

gcc when referring to the library, the first will look for dynamic libraries, dynamic libraries can not be found if only to find a static library, static library also will not find the error.

3, execution of the program;

Linux default dynamic library only to find a few default directory and loaded at runtime, if we compiled dynamic library is not in the default library directory, an error can not find the dynamic libraries will be reported. The solution are:

1, the extended library loading path: export LD_LIBRARY_PATH = xxx: $ LD_LIBRARY_PATH; xxx path is to have this dynamic library files.

2, this dynamic libraries in the default library directory: / lib, / usr / lib;

3, New /etc/ld.so.conf.d/xxx.conf file, fill in the path of our dynamic library in this file, save it for later re-execute the ldconfig command to refresh the system variables;

 

 


 

Guess you like

Origin www.cnblogs.com/chorm590/p/11588029.html