Linux mobile phone DIY. Library File topic. Knowledge preparation

Source: Internet
Author: User
Tags cursor library define function

Linux mobile phone DIY. Library File topic. Knowledge preparation

Papayas are copied from 2006-11-9.

I. Sequencing

During software migration, the Linux operating system library files are really troublesome.
It is also relatively small. After a period of search and query, write a summary. However
Most of the content here is copied. I would like to express my deep respect for the original author ~

Ii. Important Notes

To facilitate a better understanding of this article, we provide the following link.
Full range of articles address, mobile phone application development column: http://blog.csdn.net/liwei_cmg
Related important outcome: http://play.younet.com/view.php? Tid = 24045

Iii. Linux Library File Format

[The following content is from the document "create and use a Database", which is not detailed by the author]

Some functions in C language do not need to be compiled or can be used in multiple files.
Generally, this function executes certain standard tasks, such as database input/output operations or
Screen Control. You can compile these functions in advance and place them in some special projects.
In the standard code file, these target code files are called libraries.
Functions in the library file can be connected to applications through the Connection Program. In this way
These general functions are compiled during the next development.

Different types of applications use different function libraries. For example, the libdbm library contains
Database file to access the DBM function, the program that needs to operate on the database will
Line connection. The mathematical application uses the mathematical library libm, and the X-Windows application uses the xlib library,
Libx11. In addition, all programs will use the standard C function library. Libc, which contains
Basic functions for storage management or input/output operations. These libraries are stored in the public systems of/usr/lib.
Any user in the system can use these libraries. You can also create your own
Dedicated library functions for you or other specified personnel.

The library can be used in three forms: static, shared, and dynamic. Static library code is compiled
The shared library is loaded only when the program starts running,
During compilation, only the library functions to be used are specified. The dynamic library is another change of the shared library.
Form. The dynamic library is also loaded when the program is running, but unlike the shared library, the library function used
It is not loaded at the beginning of the program running, but when the statements in the program need to use this function. Dynamic library
You can release the memory occupied by the dynamic library during the running of the program to free up space for other programs. By
The shared library and dynamic library do not include the library function content in the program, but only contain the Library Function
Therefore, the code size is relatively small.

Most of the databases that have been developed adopt shared libraries. Share executable files in ELF format
The library can be easily implemented. Of course, the old A. out mode can also be used to share the library. Linux
Currently, the standard format of executable files in the system is elf.

The use of the GNU Library must comply with the library GNU Public License (lgpl license ). The
The agreement is slightly different from the GNU license agreement. developers can use the GNU Library for software development for free,
The source code of the library function must be provided to the user.

The available databases in the system are stored in the/usr/lib and/lib directories. The library file names are prefixed with lib and
The Database Name and suffix. Different database types have different suffix names. Extension of the Shared Library
It consists of. So and version number. The suffix of the static library is.. Shared library in the old A. out Format
The suffix is. SA.

Using the GCC compiler, you can connect the library with your own program, for example, libc. so.5.
Contains the standard input and output functions. When the connection program connects to the target code, it will automatically search for
Program and connect it to the generated executable file. The standard input/output library contains many basic
Such as printf. You can also connect to other system function libraries, such
Library, but unlike libc. so.5, most other system libraries need to explicitly specify
The name of the database used.

The vast majority of shared libraries can be found in the/usr/lib and/lib directories. Search first during connection
These two directories. Some libraries may also be stored in a specific directory, in the/etc/lD. conf configuration file
List of these directories. The Connection Program will also search for the listed directories. In the default
In this case, Linux will first search for the shared version of the specified library. If it cannot be found, it will search for static
Version. After updating the shared library or installing a new library, you must run the ldconfig command to update
Items in the/etc/lD. conf file (if rpm is used for installation, it is generally updated automatically,
However, this cannot be guaranteed ).

When referencing the library files in a searchable directory in the GCC compiler, use the-L option and Library
Name. Enter-lm on the GCC command line to connect to the Standard Arithmetic library in the program.-l will first use
Libname. So. Here is libm. So. The following example uses the arithmetic library to create bookrecs
Program, pay attention to the-LM option here.
$ GCC main. c Io. C-o bookrecs-LM

There are other available libraries in the system, commonly used libncurses. A library, including some
A single mouse movement routine. Use the-lncurses option in the command line to reference the libncurses. So library. Lower
The above example calls both the math and cursor library.
$ GCC mian. c Io. C-o bookrecs-lm-lncurses

When referencing libraries in other directories, you must use the-ldir option to specify the directory. This option specifies
Other paths when searching for library functions. In the following example, the mydir directory is used for connection.
Myio. So library file in.
$ GCC main. C-o bookrecs-lmydir-lmyio

In Linux, the file type does not depend on the suffix, but generally speaking:
. O is the target file, similar to the. OBJ file in windows.
. So is a shared library file, used for dynamic connection, similar to DLL files
. A is a static database and multiple. O files are combined for static connection.
. LA is a shared library automatically generated by libtool and can be edited and viewed by VI. It records
Configuration information.

4. Create and use Linux library files

[The following content is written in "creating and using Linux static/Dynamic Link Libraries,
Http://blog.csdn.net/hcj2002/]

Like windows, Linux also has a static/dynamic link library. The following describes how to create and use a static/dynamic link library.
Method:

Suppose there are the following files:
The header file string. h declares the prototype of the relevant function. The content is as follows:
Strlen. C: The implementation of the strlen function to obtain the length of a given string. The content is as follows:
Strlnen. C: Implementation of the strnlen function to obtain the length of a given string.
If the length of a string is greater than the specified maximum length, the maximum length is returned. Otherwise, the actual length of the string is returned.
Length, the content is as follows:
Generate static Library:

Use GCC to generate the corresponding target file:
Gcc-C strlen. c strnlen. c
If the corresponding file is correct, GCC will compile the file to generate strlen. O and strnlen. o
Two target files (equivalent to OBJ files in Windows ). Then, use AR to create a file named libstr..
And insert the contents of strlen. O and strnlen. O into the corresponding library file. Related life
Command:
Ar-RC libstr. A strlen. O strnlen. o
After the command is successfully executed, the corresponding static library libstr. A is successfully generated.

/***********************************
Filename: String. h
Description:
Author: HCJ
Date: 2006-5-7
************************************/


Int strlen (char * pstr );
Int strnlen (char * pstr, unsigned long ulmaxlen );



/**************************************
Filename: get String Length
Description:
Author: HCJ
Date: 2006/5/7
**************************************/
# Include <stdio. h>
# Include <assert. h>

Int strlen (char * pstr)
{
Unsigned long ullength;
Assert (null! = Pstr );

Ullength = 0;
While (* pstr ++)
{
Ullength ++;
}

Return ullength;
}


**************************************** ******
Fileneme: mystrnlen. c
Description: Get input string length, if string large
Max length input return max length,
Else real length
Author: HCJ
Date: 2006-5-7
**************************************** ******/

# Include <stdio. h>
# Include <assert. h>

Int strnlen (char * pstr, unsigned long ulmaxlen)
{
Unsigned long ullength;

Assert (null! = Pstr );

If (ulmaxlen <= 0)
{
Printf ("wrong max length! /N ");
Return-1;
}

Ullength = 0;
While (* pstr ++ & ullength <ulmaxlen)
{
Ullength ++;
}

Return ullength;
}


Generate Dynamic Link Library:
Gcc-FPIC-shared-O libstr. So strlen. c strnlen. c
-FPIC allows the output object module to be generated in the form of relocated addresses.
-Shared specifies to generate the corresponding dynamic link library file libstr. So for the corresponding source file
File.

The corresponding link library has been generated. The following describes how to use the corresponding link library.
Static library usage:
Assume that the following files use the corresponding static Library:
Compile and generate the corresponding target file:
Gcc-c-I/home/HCJ/XXXXXXXX main. c
Generate an executable file:
Gcc-O main1-L/home/HCJ/XXXXXXXX main. O libstr.
-I/home/HCJ/XXXXXXXX and-L/home/HCJ/XXXXXXXX indicate
Specifies the path of the corresponding header file and library file. Libstr. A is the name of the corresponding static library. This way
The corresponding static library has been compiled into the corresponding executable program. Execute the corresponding Executable File
You can obtain the function call result.

/*************************************** **
Filename: Main. c
Description: Test static/dynamic library
Author: HCJ
Date: 2005-5-7
**************************************** **/
# Include <stdio. h>
# Include <string. h> // header file of the function corresponding to the static library

Int main (INT argc, char * argv [])
{
Char STR [] = {"Hello World "};
Unsigned long ullength = 0;

Printf ("the string is: % s/n", STR );
Ullength = strlen (STR );
Printf ("the string length is: % d (use strlen)/n", ullength );
Ullength = strnlen (STR, 10 );
Printf ("the string length is: % d (use strnlen)/n", ullength );

Return 0;
}

The dynamic library can be divided into two call Methods: implicit call and explicit call:
The usage of implicit calling is similar to that of static library calling. The specific method is as follows:
Gcc-c-I/home/HCJ/XXXXXXXX main. c
Gcc-O main1-L/home/HCJ/XXXXXXXX main. O libstr. So // here is *. So

In this call method, you need to maintain the configuration file/etc/lD. So. conf of the dynamic link library.
To make the dynamic link library used by the system. Usually, the directory name of the dynamic link library is appended to the dynamic link library.
In the library configuration file. Otherwise, the dynamic link will be loaded when the executable files are executed.
Failed to connect to the database. When compiling the referenced dynamic library, you can use the-l or-L option or
Directly reference the required dynamic link library for compilation. In Linux, you can use the LDD command
To check whether the program depends on the shared library.

Explicit call:

/*************************************** **
Filename: main2.c
Description: Test static/dynamic library
Author: HCJ
Date: 2005-5-7
**************************************** **/
# Include <stdio. h>
# Include <dlfcn. h>

Int main (INT argc, char * argv [])
{
// Define function pointor
INT (* pstrlenfun) (char * pstr); // declare the function pointer of the corresponding function
INT (* pstrnlenfun) (char * pstr, int ulmaxlen );

Char STR [] = {"Hello World "};
Unsigned long ullength = 0;

Void * pdlhandle;
Char * pszerr;

Pdlhandle = dlopen ("./libstr. So", rtld_lazy); // load the Link Library/libstr. So
If (! Pdlhandle)
{
Printf ("failed Load Library/N ");
}
Pszerr = dlerror ();
If (pszerr! = NULL)
{
Printf ("% s/n", pszerr );
Return 0;
}

// Get function from LIB
Pstrlenfun = dlsym (pdlhandle, "strlen"); // obtain the function address.
Pszerr = dlerror ();
If (pszerr! = NULL)
{
Printf ("% s/n", pszerr );
Return 0;
}

Pstrnlenfun = dlsym (pdlhandle, "strnlen ");
Pszerr = dlerror ();
If (pszerr! = NULL)
{
Printf ("% s/n", pszerr );
Return 0;
}

Printf ("the string is: % s/n", STR );
Ullength = pstrlenfun (STR); // call the relevant function
Printf ("the string length is: % d (use strlen)/n", ullength );
Ullength = pstrnlenfun (STR, 10 );
Printf ("the string length is: % d (use strnlen)/n", ullength );
Dlclose (pdlhandle );
Return 0;
}

Gcc-O mian2-LDL main2.c
Use GCC to compile the corresponding source file to generate an executable file. The-LDL option indicates the generated object model.
The shared library must be used for the block. Run the corresponding file to get the correct result.
Related functions are described as follows:
(1) dlopen ()
The first parameter: Specifies the name of the shared library, and searches for the specified shared library in the following position.
-The environment variable LD_LIBRARY_PATH lists all directories separated by semicolons.
-Use ldconfig to maintain the list of databases found in the/etc/lD. So. cache file.
-Directory usr/lib.
-Directory/lib.
-Current directory.
Second parameter: specify how to open the shared library.
-Rtld_now: load all functions in the shared library to the memory.
-Rtld_lazy: pushes back the function loading operation in the shared library until dlsym () is called.
Party loads a function
(2) dlsym ()
When you call dlsym, use the phandle and function name of the Shared Library returned by dlopen ()
Parameter to return the entry address of the function to be loaded.
(3) dlerror ()
This function is used to check the errors related to the function that calls the shared library.
In this way, we will use a simple example to illustrate how to create and use static/dynamic libraries in Linux.

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.