Detailed examples of Linux dynamic library generation

  
                

Dynamic library can save disk and memory space, make program upgrade easier, increase CPU cache hit rate, etc. The following small series shows you how to generate Linux dynamic library through examples. Let's take a look.

What is the dynamic library?

Dynamic library, also known as dynamic link library, is a short form of Dynamic Link Library. DLL is a library containing code and data that can be used by multiple programs at the same time. DLL is not an executable file. Dynamic linking provides a way for a process to call functions that are not part of its executable code. The function's executable code is located in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLLs also help to share data and resources. Multiple applications can simultaneously access the contents of a single DLL copy in memory. A DLL is a library of code and data that can be used simultaneously by multiple programs.

I. Brief description

The extension of the dynamic library file under Linux is “.so”(Shared Object). By convention, all dynamic library file names are of the form libname.so (possibly adding a version number to the name). Thus, the thread function library is called libthread.so. The file name of the static library is libname.a. The file name form of the shared archive is libname.sa. Shared archive is just a form of transition that helps people move from static to dynamic.

Xiaobian integrates his own learning experience and better content on the network, and introduces the method of generating and linking dynamic library files with simple examples.

Operating System: Ubuntu 14.0.4

GCC Version: 4.1.3

Second, Library File and Test File Code

Library File and Test File Directory: /home/test/program/

1. Library file name: myLibSrc.c

The code is as follows:

/*

filename : myLibSrc.c

*/

#include"stdio.h"

#include“myLibInclude.h”

int

myLibSrcFun(){

printf(“There is myLibSrcFun()\ ”);

return 0;

}

2 File: main.c

The code is as follows:

/*

filename: main.c

*/

#include Stdio.h"

#include“myLibInclude.h”

int main(intargc, char** argv){

printf(“Main function !\ ” );

//Call the function in the loaded dynamic library

myLibSrcFun();

Return 0;

}

"strong" 3, header file: myLibInclude.h "/strong"

/*

filename: myLibInclude.h

*/

intmyLibSrcFun();//Declare the function

Third, the dynamic library compilation method

Compile the library file myLibSrc.c command as follows :

The code is as follows:

$ gcc myLibSrc.c -shared -o libmyLib.so

or: $ gcc myLibSrc.c -fpic-shared -g -DDEBUG -o libmyLib.so

If the compilation is successful, the dynamic library file will be generated in the directory /home/test/program/: libmyLib.so

There are two points to add:

A. For Linux operations, it is generally recommended to use the normal user mode. If you need superuser privileges, you can use sudo or su root to enter the root user password switch. In view of the individual learning and use, many operations need to use the root user, so compile directly under the root user.

B, the meaning of the parameters when compiling the dynamic library

-fpic: The output object module can be relocated by address method.

-shared: Specifies to generate the corresponding dynamic link library file for the corresponding source file.

Fourth, the dynamic library test method

Compile the test file: main.c

The code is as follows:

$gcc–o app main.c /Home/test/program/libmyLib.so

Run after successful compilation. /app :

The code is as follows:

Main function !

There is myLibSrcFun()

Note:

1. The above compiled command $gcc –o app main.c /home/test/program/The last parameter of libmyLib.so

is the absolute path that specifies the particular connected library file. The absolute path of the library file in this example is /home/test/program/libmyLib.so

Of course, if you want to access the library file path from the system (usually the system function library is located in the /usr/lib directory) If you link the dynamic library, you can copy the generated library file to /usr/lib and then link:

The code is as follows:

$cp libmyLib.so /usr/lib/libmyLib.so

$gcc –o app main.c -lmyLib

Here, a simple explanation of the link method: for $gcc –o app main.c -lmyLib The last parameter in the –lmyLib, visible command line arguments passed to C compiler does not mention the full path of the function library, not even mention the full name of the file in the function library directory! In fact, the compiler is told to link to the corresponding function library (under /usr/lib) according to the option -lmyLib. The name of the function library is libmyLib.so, that is, the extensions of the <quo;lib” sections and files are Omitted, but added a ‘l’ in front.

The above is an example of the dynamic library generation under Linux. Through these examples, you can learn more about how the Linux dynamic library is generated, and hope to help beginners.

Copyright © Windows knowledge All Rights Reserved