AnIntroductiontoGCC读书笔记Word文档格式.docx
《AnIntroductiontoGCC读书笔记Word文档格式.docx》由会员分享,可在线阅读,更多相关《AnIntroductiontoGCC读书笔记Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
![AnIntroductiontoGCC读书笔记Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-10/25/86fdfec5-37aa-4b5c-b8e1-39cc8fe12a0c/86fdfec5-37aa-4b5c-b8e1-39cc8fe12a0c1.gif)
error.Itisrecommendedthatyoualwaysusethis!
ii.–o:
Thisoptionisusuallygivenasthelastargumentonthecommandline.Ifitisomitted,theoutputiswrittentoadefaultfilecalled‘***.out’.
b)Errordebugging:
i.ThemessagesproducedbyGCCalwayshavetheformfile:
line-number:
message.
2.Compilingmultiplesourcefiles
a)Differencebetween#include"
FILE.h"
and#include<
FILE.h>
i.#include"
Searchesinthecurrentdirectoryfirstthenthesystemheaderfiledirectory.
ii.#include<
onlysearchesinthesystemheaderfiledirectory.
b)gcc-Wallmain.chello_fn.c-onewhello
i.Notethathello.hwillbeincludedautomatically.
3.Compilingfilesindependently
a)Why:
Ifaprogramisstoredinasinglefilethenanychangetoanindividualfunctionrequiresthewholeprogramtoberecompiledtoproduceanewexecutable.
b)Thesourcefilesarecompiledseparatelyandthenlinkedtogether—atwostageprocess.
i.Firststage:
.o,anobjectfile.
ii.Secondstage:
theobjectfilesaremergedtogetherbyaseparateprogramcalledthelinker.
c)Creatingobjectfilesfromsourcefiles
i.gcc-Wall-cmain.c
-c:
compileasourcefileintoanobjectfile
ii.Containingthemachinecodeforthemainfunction,hasanexternalreferencetohello(),buttheexactmemoryaddressisnotdecided.
iii.Noneedtoadd–ooption
iv.Noneedtoputtheheaderfile‘hello.h’onthecommandline,
d)Creatingexecutablesfromobjectfiles
i.gccmain.ohello_fn.o-ohello
ii.Noneedtousethe‘-Wall’warningoption:
filehasbeencompiled.
iii.Failsonlyiftherearereferenceswhichcannotberesolved
e)Linkorderofobjectfiles
ii.Objectfilewhichcontainsthedefinitionofafunctionshouldappearafteranyfileswhichcallthatfunction.
f)Recompilingandrelinking
i.Iftheprototypeofafunctionhaschanged,itisnecessarytomodifyandrecompilealloftheothersourcefileswhichuseit.
ii.Linkingisfasterthancompilation
g)Linkingwithexternallibraries
i.Themostcommonuseoflibrariesistoprovidesystemfunctions.
ii.Librariesaretypicallystoredinspecialarchivefileswiththeextension‘.a’,referredtoasstaticlibraries
iii.Usingarcommandtocreateastaticlibrary
iv.Thedefaultlibraryfileislib.a,whenwereferredotherfunction,suchasmath.h,wewillneedlibm.a
v.gcc-Wallcalc.c-lm-ocalc
vi.Thecompileroption‘-lNAME’willattempttolinkobjectfileswithalibraryfile‘libNAME.a’inthestandardlibrarydirectories.
h)Linkorderoflibraries
i.Containingthedefinitionofafunctionshouldappearafteranysourcefilesorobjectfileswhichuseit.
ii.Alibrarywhichcallsanexternalfunctiondefinedinanotherlibraryshouldappearbeforethelibrarycontainingthefunction.
iii.gcc-Walldata.c-lglpk-lm
iv.libglpk.acallsfunctionsinlibm.a
i)Usinglibraryheaderfile:
Ifyouwronglyomittheincludeheaderfile,youcanonlydetectthiserrorby–Wall
Chapter3:
compilationoptions
1.Settingsearchpaths
a)Bydefault,gccsearches
i.Thefollowingdirectoriesforheaderfiles:
1./usr/local/include/
2./usr/include/
ii.Thefollowingdirectoriesforlibraries:
1./usr/local/lib/
2./usr/lib/
iii.Aheaderfilefoundin‘/usr/local/include’takesprecedenceoverafilewiththesamenamein‘/usr/include’.
iv.Thecompileroptions‘-I’and‘-L’addnewdirectoriestothebeginningoftheincludepathandlibrarysearchpathrespectively.
b)Searchpathexample
i.gcc-Wall-I/opt/gdbm-1.8.3/include-L/opt/gdbm-1.8.3/libdbmain.c–lgdbm
-I:
addnewdirectorytotheheaderfilesearchset.
-L:
addnewdirectorytothelibrarysearchset.
ii.Youshouldneverplacetheabsolutepathsofheaderfilesin#includestatementsinyoursourcecode,asthiswillpreventtheprogramfromcompilingonothersystems
c)Environmentvariables
i.Setin.bash_profile
ii.AdditionaldirectoriescanbeaddedtotheincludepathusingtheenvironmentvariableC_INCLUDE_PATH(forCheaderfiles)orCPLUS_INCLUDE_PATH(forC++headerfiles).
iii.C_INCLUDE_PATH=/opt/gdbm-1.8.3/include
C_INCLUDE_PATH=.:
/opt/gdbm-1.8.3/include:
/net/include
exportC_INCLUDE_PATH
LIBRARY_PATH=/opt/gdbm-1.8.3/lib
exportLIBRARY_PATH
d)Extendedsearchpaths
i.gcc-I.-I/opt/gdbm-1.8.3/include-I/net/include-L.-L/opt/gdbm-1.8.3/lib-L/net/lib
Addmultiplesearchpaths
e)Exactsearchorder.
i.Command-lineoptions‘-I’and‘-L’,fromlefttoright
ii.Directoriesspecifiedbyenvironmentvariables,suchasC_INCLUDE_PATHandLIBRARY_PATH
iii.Defaultsystemdirectories
2.Sharedlibrariesandstaticlibraries
a)Staticlibraries
i.‘.a’files
ii.Whenaprogrami