Example: dental hygienist

Creating and Using are w Soft Libraries 1

Outcreatingandusinglibrary les,IstronglyrecommendthatyoureadDavidWh eeler' eginbyexplainingabitab outsoftwarelibrariesingeneral,thenpro ceedtodescrib ethedi erencesb ,theydescrib ethehow-to'sab outcreatingandusingb othtyp esoflibrariesusingtheto (ELF), eimp ortant,thatis ne; ,youcanjust cuttothechase andjumpdirectlytotheappropriatesectionb elow,either 5and 6forstaticlibrariesor 7and outLibrariesAsoftwarelibrary,alsocalleda programlibrary,isa lecontainingcompiledco deandp ossiblydatathatcanb youcannot run ,typ ede nitions, eenusingsoftwarelibrariessinceyourvery rst HelloWorld program, ,mostlikelyeithertheCstandardI/Olibrary( ifyouusedprintf,forinstance),ortheC++ios treamslibrary(ifyouusedtheinsertionop eratorofthecoutostreamob ject.)

e Softwar Design e ctur e L Notes ating e Cr and Using e Softwar aries Libr of. Pr Stewart eiss W Creating and Using are w Soft Libraries 1 tro Induction These notes

Tags:

  Using, Creating, Soft, Libraries, Creating and using are w soft libraries

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Creating and Using are w Soft Libraries 1

1 Outcreatingandusinglibrary les,IstronglyrecommendthatyoureadDavidWh eeler' eginbyexplainingabitab outsoftwarelibrariesingeneral,thenpro ceedtodescrib ethedi erencesb ,theydescrib ethehow-to'sab outcreatingandusingb othtyp esoflibrariesusingtheto (ELF), eimp ortant,thatis ne; ,youcanjust cuttothechase andjumpdirectlytotheappropriatesectionb elow,either 5and 6forstaticlibrariesor 7and outLibrariesAsoftwarelibrary,alsocalleda programlibrary,isa lecontainingcompiledco deandp ossiblydatathatcanb youcannot run ,typ ede nitions, eenusingsoftwarelibrariessinceyourvery rst HelloWorld program, ,mostlikelyeithertheCstandardI/Olibrary( ifyouusedprintf,forinstance),ortheC++ios treamslibrary(ifyouusedtheinsertionop eratorofthecoutostreamob ject.)

2 Perhapsyoumighthavereachedthep ointwhereyourealizethatyouarewritingusef ulco de,co dethatyoumightwanttouseinmorethanonepro ject,andthatwhileyoucouldcontinuetocopyt hosefunctionsintoeachnewpro ject,p erhapsyouwouldliketoreusethatco deinamoree cientwaybycreatingalibrary , ,therearetwokindsoflibrary les, staticlibrary isshortfor staticallylinkedlibrary. Astaticlibraryisalibrarythatcanb elinkedtotheprogramstatically,afterthepr ogramiscompiled,aspartoftheprogramexecut able ,itisincorp oratedintotheprogramexecutable ,eitheratloadtimeoratruntime,dep ,thenasymb olsuchasafunctioninthelibraryislinkedtot heprogramonlywhentheprogramcallsthatfunc tionorotherwisereferencesthatsymb DLL isshortfor dynamicallylinkedlibrary ,DLLsaredi , ,whichwastheoriginalformoflinking,resolv esreferencestoexternally-de nedsymb olssuchasfunctions,bycopyingthelibraryco dedirectlyintotheexecutable lewhentheexecutable( le)

3 ,alsocalledthelinkeditor,orjustthelinker ,p linker isabitambiguous, ,p erhapsnowtheonlyadvantage, ,aprogrammightuseagraphicalto olkitsuchasGTKthatmaynotb olkit'slibrariesstaticallylinkedintothee xecutable,theexecutablecanrunonothersyst ems(withthesamemachinearchitecture)witho utrequiringtheusersonthosesystemstoinsta llthoselibrary onatime,staticlinkingresultedinfasterco deaswell,butthegainisnegligibleto edoneeitherwhentheprogramisloadedintomem ory,orwhileitisrunningandreferencesanunr esolvedsymb ,thestart-uptimeoftheprogramisslightlylo ngerthanifithadb eenstaticallylinked,sincethelibrarieshav etob elo catedinmemory(andp ossiblyloadedintomemoryiftheywerenotalre adythere)

4 Andthenlinkedtotheprogramb eforeitcanactuallyb ,theprogramwillexp erienceslightlylongerrunningtime,b ecausewheneveranunresolvedsymb olisfoundandmustb eresolved,thereisabitofoverheadinlo ecauseitonlylinkssymb ,ifafunctionfromasharedlibraryisnotcalle dduringexecution,itwillnotb elinkedtothelibraryatall, ,b ecausetheexe-cutableprogram ledo esnotcontaintheco deofthelibrariesthatmustb elinkedtoit,theexecutable eingphysicallyincorp oratedintomultipleprograms,asinglememory -residentcopyofthelibrarycanb elinkedtoeachprogram, di edwhenapro ,".so" shortforsharedob ossibletoup datethelibrarieswithoutrecompilingthepro gramswhichusethem, xedintheselibraries,allthatisnecessaryis toobtainthemo di ,thenallprogramsthatusethemwouldhavetob eci caddressesintheexecutableco dsofdeterringsuchtyp esofattacksinvolveran-domizingthelo cationsofvariousrelo catablesegmentsintheco ,onlythestackandheapaddresscanb erandomized:allinstructionshavea xedaddressinallinvo ,thekernelhastheabilitytoloadthelibrarie satarbitraryaddresses,indep erecognizedbytheirending:theyendin".

5 A".Sharedlibrarieshavea".so"extension,p ossiblywithaversionnumb erfollowing, esoflibrariesstartwiththepre x"lib"andthenhaveauniquenamethatidenti ,forexample,thestandardC++staticlibraryi slibstdc++.a, "rt" osethatyouhaveoneormoresourceco de lescontainingusefulfunctionsorp erhapsde nitionsofusefultyp ,supp lethatcontainstheprototyp esofthefunctionsde osethat lesintoob ject lesusingthecommandgcc -c gcc -c les, ,ar,tocreateanewarchiveandinsertthetwoob ject lesintoit:ar rcs rcs followingthecommandnameconsistsofaone-le tterop erationco defollowedbytwomo di "r"istheop erationco dethattellsartoinserttheob ject c and s aremo di ers.

6 Cmeans createthearchiveifitdidnotexist andsmeans createanindex, likeatableofcontents,inthearchive eforethelistof ,ourlibrarywillb eusedtoaddnewob ject lestothelibrary,soifyoulaterdecidetoaddt he ,youwouldusethecommandar rcs ,andputtheheader "most-closelyenclosingancestraldirectory " ,alibrarythatwillb eusedonlyforprogramsthatIwriteformyUNIXS ystemProgrammingclasswillb einadirectoryunderthedirectorycontaining allofthoseprograms,suchas:~/unix_demos/ e~/unix_demos/ , ,thatisgenerallyusefultomeforanyprogramm ingtask,Iwillputitinmy~/libdirectory:~/ ~/includedirectory:~/ leshouldhavelinesoftheform1:LIBRARY_PATH ="$LIBRARY_PATH:~/lib:"export LIBRARY_PATH sothatgccwillknow"wheretolo ok" esearchedb eforethestandardones,thenreversetheorder :LIBRARY_PATH="~/lib.

7 $LIBRARY_PATH"export LIBRARY_PATH1 Thisisnottheb ()formo (orifusingC++,yourCPLUS_INCLUDE_PATH)con tainsthepathtothedirectoryinwhichyouputt heheader lehasthelinesCPATH="~/include"export o dreasonforthis,whichwillb (Linkingto)aStaticLibraryTousethelibrary inaprogram,(1)youhavetotellthecompilerto includeitsinterface, ,itsheader le,and(2) erthatthenameiseverythingb etween"lib"andthe".".The-loptionmustfol lowthelistof , leforthelibrary:#include " " ,youwouldissuethecommandgcc -o myprogram -lutilsorthefollowingifyoudidnotmo difyyourCPATH:gcc -o myprogram -lutils -I~/unix_demos/includebutineithercase,on lyifyouarecertainthatthereisnotasharedli brarywiththesamenameinadirectorythatwill b cated, ecausegcc,bydefault,willalwayscho : cannot find -lutilscollect2.

8 Ld returned 1 exit statusitmeansthatyoudidnotsetuptheLIBRAR Y_PATH prop erly.(Didyouexp ortit?Didyoutyp eitcorrectly?)Ifyouwanttob esafe, esearchedwhenlo okingforlibrariessp eci edwiththe-loption,asingcc -o myprogram -L~/unix_demos/lib -lutilsDirectoriessp eci edwith-Lwillb esearchedb ,youmayseeinstructionsforbuildingyourpro gramoftheformgcc -static -o myprogram : cannot find -lccollect2.

9 Ld returned 1 exit statusb , eratingsystems,thelinkeditor,ld,willnot ,youneedtounderstandafewthingsab outsharedlibrariesinUNIX tomakesenseoutoftheoptionstob ecialnamecalledits soname .Thesonameisconstructedfromthepre x lib ,followedbythenameofthelibrary,thenthest ring".so",and nally,ap erio dandaversionnumb ,forexample,thesonameofthemathlibrary,m, mightb realname ,whichisthenameoftheactual ;itmustb eformedbyapp endingtothesonameap erio d,andaminornumb er,andoptionally,anotherp erio dandareleasenumb erandreleasenumb erareusedforcon ,thelibraryhasanamethatisusedbythecompil er,whichisthesonamewithouttheversionnumb Thiswillb Thiswillb ethenameofthe erof0andareleasenumb Thisisthenamethecompilerwilluse, okinthe/libdirectory,youwillseethatlinks arecreatedinasp eci cway.

10 Foreachsharedlibrarythereareoftenatleast threeentries,suchaslrwxrwxrwx 1 root root 11 Aug 12 18:52 -> 1 root root 15 Aug 12 18:51 -> 1 root root 31380 Aug 3 18:42 'sname(withouttheversionnumb er)isasoftlinktothesoname,whichisasoftli nktotheactuallibrary , lesinthedirectorywhereitisplaced:thesona mewillb easoftlinktotheactuallibrary le,andasoftlinktothesoname de lethatyouintendtoputintoasharedlibrary, ,compileitwithpositionindependentcodeusi ngthefollowingcommand:gcc -fPIC -g -Wall -c duceanob ject le, ,withdebugginginformationincluded(the-go ption),withallwarningsenabled(the-Wallop tion), "-fPIC"optioniswhatgeneratesthepositioni ndependentcode(hencePIC).


Related search queries