Creation and use of static libraries on Linux

Source: Internet
Author: User
Tags array length

The concept of a static library:

We know that program compilation requires preprocessing, compiling, assembling, and linking several steps. In our application, there are some common code that needs to be reused, compile the code into a "library" file, and in the link step, the connector will get the required code from the library file and copy it to the resulting executable . This library, known as a static library, is characterized by a full copy of the library code contained in the executable file, and the drawback is that multiple copies are redundant when used multiple times.

Create a static library:

 For example, I have some of the following functions that manipulate arrays need to be made into a static library, convenient for use without the need for duplicate definitions, the Array.h header file is as follows:

1 #ifndef Array_h_2 #defineArray_h_3 4 /*5 function function: Finding array elements by binary lookup of shaped array6 parameter: ary array name start find where end find ends7 return Value: Successfully returns the subscript of the array, failure returns-18 */9 intBin_search (int*ary,intStartintEnd,intkey);Ten  One /* A functions: Quick sorting of shaped arrays - parameter: ary array name low need to sort the high order - return value: None the */ - voidSort_quick (int* ary,intLowintHigh ); -  - /* + function function: Outputs all elements of a shaped array - parameter: ary array name len array length + return value: None A */ at voidPrint_array (int* ary,intlen); -  - #endif
View Code

  Array.c source files are as follows:

1#include"Array.h"2#include <stdio.h>3 4 /*5 function function: Finding array elements by binary lookup of shaped array6 parameter: ary array name start find where end find ends7 return Value: Successfully returns the subscript of the array, failure returns-18 */9 intBin_search (int*ary,intStartintEnd,intkey)Ten { One     intMid = (start + end)/2; A     if(Start >end) { -         return-1; -     } the     if(Ary[mid] = =key) { -         returnmid; -}Else if(Key >Ary[mid]) { -         returnBin_search (ary, Mid +1, end, key); +}Else{ -         returnBin_search (ary, start, mid-1, key); +     } A } at  - /* - function function: Outputs all elements of a shaped array - parameter: ary array name len array length - return value: None - */ in voidPrint_array (int* ary,intlen) - { to     inti =0; +      for(i =0; i < Len; i + +){ -printf"%d", Ary[i]); the     } *printf"\ n"); $ }Panax Notoginseng  - /* the functions: Quick sorting of shaped arrays + parameter: ary array name low need to sort the high order A return value: None the */ + voidSort_quick (int* ary,intLowintHigh ) - { $     if(Low >High ) $         return ; -     inti =Low ; -     intj =High ; the     intKey =Ary[i]; - Wuyi      while(I <j) { the          while(I < J && Ary[j] >=key) { -j--; Wu         } -Ary[i] =Ary[j]; About  $          while(I < J && Ary[i] <=key) { -i++; -         } -  AARY[J] =Ary[i]; +     } the  -Ary[i] =key; $Sort_quick (ary, Low, I-1); theSort_quick (ary, i +1, high); the}
View Code

I used the engineering directory structure here

Include: Save header file

SRC: Save source file

Obj: Save. o File

LIB: Save library file

Bin: Save executable file

1: Compile our. c file into an. o file
Gcc-c Src/array.c-o obj/array.o-i include
2: Use AR command to generate library file
AR RCS lib/libary.a obj/array.o
3: Testing the use of static libraries

See if a static library file called Libary.a is generated under the Lib directory

Let's simply test if it is available, write the main function, as follows:

1#include <stdio.h>2#include"Array.h"3 4 intMainintargcChar**argv)5 {6     intary[Ten] = {9,8,6,0,2,5,1,7, at, the};7     8Sort_quick (ary,0,9);9Print_array (ary,Ten);Ten     return 0; One}
View Code

Compile run Effect:

  

-L Specify the library file path

-L Specify Library name

4: Adding a static library to the system path
[Email protected]:~/c/algorithm$ sudo cp lib/libary.a/usr/lib/[email protected]:~/c/algorithm$ sudo cp include/array.h /usr/include/
 

Final Test Result:

1#include <stdio.h>2#include <array.h>3 4 intMainintargcChar**argv)5 {6     intary[Ten] = {9,8,6,0,2,5,1,7, at, the};7     8Sort_quick (ary,0,9);9Print_array (ary,Ten);Ten     return 0; One}
View Code

  

Creation and use of static libraries on 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.