An Introduction to GCC读书笔记.docx
- 文档编号:10286911
- 上传时间:2023-02-09
- 格式:DOCX
- 页数:17
- 大小:23.55KB
An Introduction to GCC读书笔记.docx
《An Introduction to GCC读书笔记.docx》由会员分享,可在线阅读,更多相关《An Introduction to GCC读书笔记.docx(17页珍藏版)》请在冰豆网上搜索。
AnIntroductiontoGCC读书笔记
AnIntroductiontoGCC:
fortheGNUCompilersgccandg++读书笔记
Chapter2:
1.CompileaCprogram
a)gcc-Wallhello.c-ohello
i.–Wall:
showallwarning&error.Itisrecommendedthatyoualwaysusethis!
ii.–o:
Thisoptionisusuallygivenasthelastargumentonthecommandline.Ifitisomitted,theoutputiswrittentoadefaultfilecalled‘***.out’.
b)Errordebugging:
i.ThemessagesproducedbyGCCalwayshavetheformfile:
line-number:
message.
2.Compilingmultiplesourcefiles
a)Differencebetween#include"FILE.h"and#include
i.#include"FILE.h"Searchesinthecurrentdirectoryfirstthenthesystemheaderfiledirectory.
ii.#include
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
i.gccmain.ohello_fn.o-ohello
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-lmlibglpk.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.Whenaprogramislinkedagainstastaticlibrary,themachinecodefromtheobjectfilesforanyexternalfunctionsusedbytheprogramiscopiedfromthelibraryintothefinalexecutable
b)Sharedlibraries
i.extension‘.so’
ii.Anexecutablefilelinkedagainstasharedlibrarycontainsonlyasmalltableofthefunctionsitrequires.
iii.Beforetheexecutablefilestartsrunning,themachinecodefortheexternalfunctionsiscopiedintomemoryfromthesharedlibraryfileondiskbytheoperatingsystem
iv.Dynamiclinkingmakesexecutablefilessmallerandsavesdiskspace,becauseonecopyofalibrarycanbesharedbetweenmultipleprograms.
v.Sharedlibrariesmakeitpossibletoupdatealibrarywithoutrecompilingtheprogramswhichuseit
c)gcccompilesprogramstousesharedlibrariesbydefaultonmostsystems,iftheyareavailable
d)Bydefaulttheloadersearchesforsharedlibrariesonlyinapredefinedsetofsystemdirectories,suchas‘/usr/local/lib’and‘/usr/lib
e)LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib
exportLD_LIBRARY_PATH
f)gcc-Wall-static-I/opt/gdbm-1.8.3/include/-L/opt/gdbm-1.8.3/lib/dbmain.c-lgdbm-static:
forcestaticlinking
g)gcc-Wall-I/opt/gdbm-1.8.3/includedbmain.c/opt/gdbm-1.8.3/lib/libgdbm.so
Linkdirectlywithindividuallibraryfilesbyspecifyingthefullpathtothelibraryonthecommandline
3.Clanguagestandards
a)ANSI/ISO
i.gcc-Wall-ansiansi.c
-ansi:
useansi.ThisallowsprogramswrittenforANSI/ISOCtobecompiledwithoutanyunwantedeffectsfromGNUextensions.
b)StrictANSI/ISO
i.gcc-Wall-ansi-pedanticgnuarray.c
-pedantic:
gcctorejectallGNUCextensions,notjustthosethatareincompatiblewiththeANSI/ISOstandard.
ii.ThishelpsyoutowriteportableprogramswhichfollowtheANSI/ISOstandard.
c)Selectingspecificstandards
i.‘-std=c89’or‘-std=iso9899:
1990’
Std9899
ii.‘-std=iso9899:
199409
Addinternationalizationsupport:
language
iii.‘-std=c99’or‘-std=iso9899:
1999’
Std99
iv.‘-std=gnu89’and‘-std=gnu99’.clanguagestandard+GNUextension
d)Warningoptionsin–Wall
i.‘-Wcomment’(includedin‘-Wall’)
Thisoptionwarnsaboutnestedcomments.
ii.‘-Wformat’(includedin‘-Wall’)
Thisoptionwarnsabouttheincorrectuseofformatstringsinfunctionssuchasprintfandscanf,wheretheformatspecifierdoesnotagreewiththetypeofthecorrespondingfunctionargument.
iii.‘-Wunused’(includedin‘-Wall’)Thisoptionwarnsaboutunusedvariables.
iv.‘-Wimplicit’(includedin‘-Wall’)
Thisoptionwarnsaboutanyfunctionsthatareusedwithoutbeingdeclared.Themostcommonreasonforafunctiontobeusedwithoutbeingdeclaredisforgettingtoincludeaheaderfile.
v.‘-Wreturn-type’(includedin‘-Wall’)
Thisoptionwarnsaboutfunctionsthataredefinedwithoutareturntypebutnotdeclaredvoid.
e)Additionalwarningoptions
i.Theyarenotincludedin‘-Wall’becausetheyonlyindicatepossiblyproblematicor“suspicious”code
ii.Itismoreappropriatetousethemperiodicallyandreviewtheresults,checkingforanythingunexpected,ortoenablethemforsomeprogramsorfiles.
iii.‘-W’
Thisisageneraloptionsimilarto‘-Wall’whichwarnsaboutaselectionofcommonprogrammingerrors
Inpractice,theoptions‘-W’and‘-Wall’arenormallyusedtogether.
iv.‘-Werror’
Changesthedefaultbehaviorbyconvertingwarningsintoerrors,stoppingthecompilationwheneverawarningoccurs.
v.‘-Wconversion’
Thisoptionwarnsaboutimplicittypeconversionsthatcouldcauseunexpectedresults.Forexample:
unsignedintx=-1;
vi.‘-Wshadow’
Thisoptionwarnsabouttheredeclarationofavariablenameinascopewhereithasalreadybeendeclared.
vii.‘-Wcast-qual’
Thisoptionwarnsaboutpointersthatarecasttoremoveatypequalifier,suchasconst.
viii.-Wtraditional’
ThisoptionwarnsaboutpartsofthecodewhichwouldbeinterpreteddifferentlybyanANSI/ISOcompileranda“traditional”pre-ANSIcompiler.
Chapter4:
Usingthepreprocessor
1.Definingmacros
a)gcc-Wall-DTESTdtest.c
-DTEST:
defineamacrocalledTESTandassign1asitsvalue.
Thegccoption‘-DNAME’definesapreprocessormacroNAMEfromthecommandline.
2.Originofthemacros
a)Specifiedonthecommandlinewiththeoption‘-D’,
b)Inasourcefile(orlibraryheaderfile)with#define.
c)Automaticallydefinedbythecompiler—thesetypicallyuseareservednamespacebeginningwithadouble-underscoreprefix‘__’.
3.Macroswithvalues
a)Thisvalueisinsertedintothesourcecodeateachpointwherethemacrooccurs
b)Notethatmacrosarenotexpandedinsidestrings
c)gcc-Wall-DNUM=100dtestval.c
The‘-D’command-lineoptioncanbeusedintheform‘-DNAME=VALUE’.
d)Notethatitisagoodideatosurroundmacrosbyparentheseswhenevertheyarepartofanexpression.
e)Amacrocanbedefinedtoaemptyvalueusingquotesonthecommandline,-DNAME=""
4.Preprocessingsourcefiles
a)gcc-Etest.c-Eoption:
U
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- An Introduction to GCC读书笔记 GCC 读书笔记