IT专家为什么要先编译到对象文件Word格式.docx
- 文档编号:15686152
- 上传时间:2022-11-15
- 格式:DOCX
- 页数:13
- 大小:22.71KB
IT专家为什么要先编译到对象文件Word格式.docx
《IT专家为什么要先编译到对象文件Word格式.docx》由会员分享,可在线阅读,更多相关《IT专家为什么要先编译到对象文件Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
Onehasanexplicitstatementcreating.ofilesfrommodules(e.g.gfortran-cfilea.f90)
beforecreatingtheexecutable.
在创建可执行文件之前,可以使用一个显式语句从模块(例如gfortran-cfile.f90)
创建.o文件。
Anotherarecreatingtheexecutablefiledirectly(sometimescreating.modfiles,but
no.ofiles,e.g.gfortran-oexecutablefilea.f90fileb.f90mainfile.f90).
另一种方法是直接创建可执行文件(有时创建.mod文件,但不是.o文件,例如
gfortran-o可执行文件)。
fileb法郎。
mainfile.f90法郎)。
Isthereareason(otherthan,maybe,Makefiles)thatonemethodispreferredoverthe
other?
有什么原因(可能除了makefile之外)一个方法优于另一个方法?
27
Compilingtoobjectfilesfirstiscalledseparatecompilation.Therearemanyadvantages
andafewdrawbacks.
首先编译对象文件称为单独编译。
有很多优点和缺点。
Advantages:
优点:
easytotransformobjectfiles(.o)tolibrariesandlinktothemlater很容易将对象文
件(.o)转换为库并稍后链接到它们manypeoplecanworkondifferentsourcefilesat
thesametime许多人可以同时处理不同的源文件fastercompiling(youdon’tcompile
thesamefilesagainandagainwhenthesourcehasn’tchanged)更快的编译(当源代码没
有更改时,您不会一次又一次地编译相同的文件)objectfilescanbemadefrom
differentlanguagesourcesandlinkedtogetheratsomelatertime.Todothat,theobject
filesjusthavetousethesameformatandcompatiblecallingconventions.对象文件可以
来自不同的语言源,并在以后的某个时候链接在一起。
为此,对象文件只需使用相
同的格式和兼容的调用约定。
separatecompilationenablesdistributionofsystem
widelibraries(eitherOSlibraries,languagestandardlibrariesorthirdpartylibraries)
eitherstaticorshared.单独的编译允许分发系统范围的库(OS库、语言标准库或第三
方库),可以是静态的,也可以是共享的。
Drawbacks:
缺点:
Therearesomeoptimizations(likeoptimizingfunctionsaway)thatthecompiler
cannotperform,andthelinkerdoesnotcareabout;
however,manycompilersnowinclude
theoptiontoperform“linktimeoptimization”,whichlargelynegatesthisdrawback.But
thisisstillanissueforsystemlibrariesandthirdpartylibraries,especiallyforshared
libraries(impossibletooptimizeawaypartsofacomponentthatmaychangeateachrun,
howeverothertechniqueslikeJITcompilationmaymitigatethis).有一些优化(比如优化
函数)编译器不能执行,链接器不关心;
然而,许多编译器现在包含了执行“链接时间
优化”的选项,这在很大程度上抵消了这个缺点。
但是,这仍然是系统库和第三方库
的问题,特别是对于共享库(不可能优化组件的某些部分,这些部分在每次运行时都
可能发生更改,但是其他技术如JIT编译可能会缓解这一问题)。
insome
languages,theprogrammerhastoprovidesomekindofheaderfortheuseofothersthat
willlinkwiththisobject.ForexampleinCyouhavetoprovide.hfilestogowithyour
objectfiles.Butitisgoodpracticeanyway.在某些语言中,程序员必须提供某种头文
件,以便使用与此对象相关联的其他头文件。
例如,在C语言中,您必须提供.h文
件以配合对象文件。
但无论如何,这是一个很好的实践。
inlanguageswithtext
basedincludeslikeCorC++,ifyouchangeafunctionprototype,youhavetochangeitin
twoplaces.Onceinheaderfile,onceintheimplementationfile.在基于文本的语言中,
比如C或c++,如果你改变一个函数原型,你必须在两个地方改变它。
一次在头文
件,一次在实现文件。
14
Whenyouhaveaprojectwithafew100sourcefiles,youdon’twanttorecompileallof
themeverytimeonechanges.Bycompilingeachsourcefileintoaseparateobjectfileand
onlyrecompilethosesourcefilesthatareaffectedbyachange,youspendtheminimum
amountoftimefromsourcecodechangetonewexecutable.
当您有一个项目有一些100个源文件时,您不希望在每次更改时重新编译它们。
通过将每个源文件编译成一个单独的对象文件,并且只重新编译受更改影响的源文
件,您将从源代码更改花费最少的时间到新的可执行文件。
makeisthecommontoolusedtotracksuchdependenciesandrecreateyourbinary
whensomethingchanges.Typicallyyousetupwhateachsourcefiledependson(these
dependenciescantypicallybegeneratedbyyourcompiler-inaformatsuitableformake),
andletmakehandlethedetailsofcreatinganuptodatebinary.
make是一种常见的工具,用于跟踪这种依赖关系并在发生更改时重新创建二进制
文件。
通常,您可以设置每个源文件所依赖的内容(这些依赖项通常可以由您的编译
器生成——以适合make的格式生成),并让make处理创建最新二进制文件的细
节。
5
The.ofileistheObjectFile.It’sanintermediaterepresentationofthefinalprogram.
.o文件是对象文件。
它是最终程序的中间表示。
Specifically,typically,the.ofilehascompiledcode,butwhatitdoesnothaveisfinal
addressesforallofthedifferentroutinesordata.
特别地,通常.o文件已经编译了代码,但是它没有所有不同的例程或数据的最终
地址。
Oneofthethingsthataprogramneedsbeforeitcanberunissomethingsimilartoa
memoryimage.
程序在运行之前需要做的一件事就是类似于内存映像。
Forexample.
为例。
IfyouhaveyourmainprogramanditcallsaroutineA.(Thisisfauxfortran,Ihaven’t
touchedindecades,soworkwithmehere.)
如果你有你的主程序,并且它调用一个例程a(这是假的fortran,我已经几十年没
碰过了,因此和我一起工作吧。
)
PROGRAMMAININTEGERX,YX=10Y=SQUARE(X)WRITE(*,*)YThenyou
havetheSQUAREfunction.
然后是平方函数。
FUNCTIONSQUARE(N)S
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- IT 专家 为什么 编译 对象 文件