How to create and use Linux static/dynamic link libraries

Source: Internet
Author: User
Tags assert function prototype strlen

Linux also has a static/dynamic link library like Windows systems, and here's how to create and use it:

Let's say there are several files:

Header file String.h, declare the correlation function prototype, the contents are as follows:

STRLEN.C: The implementation of the function Strlen, gets the length of the given string, as follows:

STRLNEN.C: The implementation of the function Strnlen, gets the length of the given string, returns the maximum length if the length of the input string is greater than the specified maximum length, or returns the actual length of the string, as follows:

To generate a static library:

Use GCC to generate the corresponding target file:

Gcc–c strlen.c STRNLEN.C

If the corresponding file is not wrong, GCC compiles the file to generate STRLEN.O and STRNLEN.O two target files (equivalent to the obj file under 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 relevant commands are as follows:

AR–RC LIBSTR.A STRLEN.O STRNLEN.O

After the command was successfully executed, the corresponding static library LIBSTR.A has been successfully generated.

<span style= "FONT-SIZE:18PX;" ><strong>/*********************************** filename:string.h Description:Author:HCJ date:200  
6-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; } </strong></span>

This article URL address: http://www.bianceng.cn/OS/Linux/201410/45420.htm

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.