How to create and use Linux dynamic and static libraries

Source: Internet
Author: User
Tags define function

Like windows, Linux also has a static/dynamic link library. The following describes how to create and use it:
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: implement the strnlen function to obtain the length of a given string. If the length of the input string is greater than the maximum length, the maximum length is returned. Otherwise, the actual length of the string is returned. 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 two target files, strlen. O and strnlen. O (equivalent to the OBJ file in Windows ). Then, use AR to create a library file named libstr. A and insert the contents of strlen. O and strnlen. O into the corresponding library file ., The related commands are as follows:
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.
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 specify the path of the corresponding header file and library file through-I and-l. Libstr. A is the name of the corresponding static library. In this way, the corresponding static library has been compiled into the corresponding executable program. Execute the corresponding executable file to 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 of the dynamic link library. so. conf 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 configuration file. Otherwise, the dynamic link library fails to be loaded when the executable files are executed. When compiling the referenced dynamic library, you can use the-l or-L option in GCC or directly reference the required dynamic link library for compilation. In Linux, you can use the LDD command to check the program dependency 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 that the generated object module must use a shared library. 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 a function is loaded when dlsym () is called.

(2) dlsym ()
When you call dlsym, use the phandle and function name of the Shared Library returned by dlopen () as parameters 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 calling the shared library. In this way, we use a simple example to illustrate the creation and use of static/dynamic libraries in Linux.

Related Article

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.