An Introduction to GCC读书笔记.docx

上传人:b****4 文档编号:3966489 上传时间:2023-05-06 格式:DOCX 页数:17 大小:23.55KB
下载 相关 举报
An Introduction to GCC读书笔记.docx_第1页
第1页 / 共17页
An Introduction to GCC读书笔记.docx_第2页
第2页 / 共17页
An Introduction to GCC读书笔记.docx_第3页
第3页 / 共17页
An Introduction to GCC读书笔记.docx_第4页
第4页 / 共17页
An Introduction to GCC读书笔记.docx_第5页
第5页 / 共17页
An Introduction to GCC读书笔记.docx_第6页
第6页 / 共17页
An Introduction to GCC读书笔记.docx_第7页
第7页 / 共17页
An Introduction to GCC读书笔记.docx_第8页
第8页 / 共17页
An Introduction to GCC读书笔记.docx_第9页
第9页 / 共17页
An Introduction to GCC读书笔记.docx_第10页
第10页 / 共17页
An Introduction to GCC读书笔记.docx_第11页
第11页 / 共17页
An Introduction to GCC读书笔记.docx_第12页
第12页 / 共17页
An Introduction to GCC读书笔记.docx_第13页
第13页 / 共17页
An Introduction to GCC读书笔记.docx_第14页
第14页 / 共17页
An Introduction to GCC读书笔记.docx_第15页
第15页 / 共17页
An Introduction to GCC读书笔记.docx_第16页
第16页 / 共17页
An Introduction to GCC读书笔记.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

An Introduction to GCC读书笔记.docx

《An Introduction to GCC读书笔记.docx》由会员分享,可在线阅读,更多相关《An Introduction to GCC读书笔记.docx(17页珍藏版)》请在冰点文库上搜索。

An Introduction to GCC读书笔记.docx

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.#includeonlysearchesinthesystemheaderfiledirectory.

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2