Linux Create dynamic libraries

Source: Internet
Author: User

  

[1] New source program SHARELIB.C

1 /*************************************************************************2 > File name:sharelib.c3 > Author:copener4 > Mail: [email protected]5 > Created time:2015 May 14 Thursday 09:03 18 seconds6  ************************************************************************/7 8#include <stdio.h>9 /*Ten * Insert method sorting function One * Input Parameters: *array is the array that will be sorted, length is the size of the element A * */ - voidInsert_sort (int*array,intlength) { -     inti,j; the      for(i=1; i<length; i++){ -         inttemp; -j=i; -temp =Array[j]; +  -          while(j>0){ +             if(Temp < array[j-1]){ Aarray[j]=array[j-1]; atj--; -}Else{ -                  Break; -             } -  -ARRAY[J] =temp; in         } -     } to     return; + } -  the /* * * Binary Lookup function $ * Input Parameters: *array is the array to be looked up, item is the element to find, length is the elementPanax Notoginseng * */ - intBinary_search (int*array,intItemintlength) { the     intHigh , Low, mid; +High =length; ALow =0; theMid = (high + low)/2; +      -      while(Low <High ) { $printf"high:%d low:%d mid:%d\r\n", high,low,mid); $         if(Array[mid] >Item) { -             if(low== (high-1) || High = =mid) { -                 return-1; the             } -printf"array[%d]>%d\r\n", Mid,item);WuyiHigh =mid; theMid = (high + low)/2; -  Wu}Else if(array[mid]<Item) { -             if(Low==mid | | high = = (low+1)){ About                 return-1; $             } -printf"array[%d]<%d\r\n", Mid,item); -Low =mid; -Mid = (high + low)/2; A  +}Else{ the             returnmid; -         } $     } the     return-1; the }

[2] Generating a sharelib.so dynamic library file

1 gcc -shared-fpic-o sharelib.so sharelib.c2ls3 SHARELIB.C  

1 //about the-shared parameter in gcc2-shared3Produce a sharedObject whichCan ThenBe linked with other objects to form an executable.  Not all systems support this option. for4Predictable results, you must also specify the same set of options used forCompilation (-fpic,-FPIC, or model Suboptions) when you5Specify this linker option. [1]6 7 8 //-shared will be used with the-fpic parameter to generate a dynamic library file

[3] Adding a sharelib.h header file reference

1 /*************************************************************************2 > File name:sharelib.h3 > Author:copener4 > Mail: [email protected]5 > Created time:2015 May 14 Thursday 11:23 18 seconds6  ************************************************************************/7 8 extern voidInsert_sort (int*array,intlength);9 extern intBinary_search (int*array,intItemintlength);Ten extern voidBubble_sort (int* Array,intlength);

[4] New program TESTAPP.C call dynamic library function test

1 /*************************************************************************2 > File name:testapp.c3 > Author:copener4 > Mail: [email protected]5 > Created time:2015 May 14 Thursday 11:25 46 seconds6  ************************************************************************/7 8#include <stdio.h>9#include"Sharelib.h"Ten  One intMainvoid){ A     intitem; -     intPos; -     inti; the     intarray[5] ={5,9,2,3,1};  -  -     //Sort -Insert_sort (Array,5); +      for(i=0; i<5; i++){ -printf"%d", Array[i]); +     } A  at     //Binary Method Search -printf"\r\nplease input a number:\r\n"); -scanf"%d", &item); -  -pos = Binary_search (array, item,5); -     if(pos = =-1){ inprintf"can ' t find or data not sort!\r\n"); -     } to     Else{ +printf"done! The position is%d\r\n", (pos+1)); -     } the  *     return 0; $}

[5] Compile test Source link Dynamic Library sharelib.so, generate TestApp executable program

1 gcc -o testapp testapp.c./2ls3 sharelib.c  sharelib.h  sharelib.so  TestApp  testapp.c

[6] Test run TestApp

1[Email protected]:~/workspace/test/sharelib$./TestApp2 1 2 3 5 9 3 Please input a number:4 25 input end, processing ...6High5Low0Mid27array[2]>28High2Low0Mid19  Done! The position is2

[7] Summary: After the library is written, just provide sharelib.so library and sharelib.h function header file to users. The link library should be placed after the compiled source file when compiling the program, otherwise the link will fail.

Linux Create dynamic libraries

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.