问题描述
去年,我在一所研究型大学工作,开始使用 Fortran 进行编程.我以前的大部分经验都是使用 PHP 或旧 ASP 等网络语言,所以我是编译语句的新手.
In the last year I've started programming in Fortran working at a research university. Most of my prior experience is in web languages like PHP or old ASP, so I'm a newbie to compile statements.
我正在修改两个不同的代码.
I have two different code I'm modifying.
一个在创建可执行文件之前有一个从模块创建 .o 文件的显式语句(例如 gfortran -c filea.f90).
One has an explicit statement creating .o files from modules (e.g. gfortran -c filea.f90) before creating the executable.
另一个是直接创建可执行文件(有时创建.mod文件,但没有.o文件,例如gfortran -o executable filea.f90 fileb.f90 mainfile.f90).
Another are creating the executable file directly (sometimes creating .mod files, but no .o files, e.g. gfortran -o executable filea.f90 fileb.f90 mainfile.f90).
- 有没有理由(除了 Makefile 之外)一种方法比另一种方法更受欢迎?
推荐答案
首先编译为目标文件称为单独编译.有很多优点和一些缺点.
Compiling to object files first is called separate compilation. There are many advantages and a few drawbacks.
优点:
- 易于将目标文件 (.o) 转换为库并稍后链接到它们
- 多人可以同时处理不同的源文件
- 更快的编译(当源代码没有改变时,你不会一次又一次地编译相同的文件)
- 目标文件可以由不同的语言来源制成,并在以后链接在一起.为此,目标文件只需使用相同的格式和兼容的调用约定.
- 单独编译允许分发静态或共享的系统范围的库(操作系统库、语言标准库或第三方库).
缺点:
- 有些优化(比如优化函数)是编译器无法执行的,链接器也不关心;然而,许多编译器现在包括执行链接时间优化"的选项,这在很大程度上消除了这个缺点.但这对于系统库和第三方库来说仍然是一个问题,尤其是对于共享库(无法优化掉每次运行时可能发生变化的组件部分,但是其他技术,如 JIT 编译可能会缓解这种情况).
- 在某些语言中,程序员必须提供某种类型的标头以供其他将与此对象链接的人使用.例如,在 C 中,您必须提供
.h
文件以与您的目标文件一起使用.但无论如何,这是一种很好的做法. - 在像 C 或 C++ 这样的基于文本的包含的语言中,如果你改变一个函数原型,你必须在两个地方改变它.一次在头文件中,一次在实现文件中.
- There are some optimizations (like optimizing functions away) that the compiler cannot perform, and the linker does not care about; however, many compilers now include the option to perform "link time optimization", which largely negates this drawback. But this is still an issue for system libraries and third party libraries, especially for shared libraries (impossible to optimize away parts of a component that may change at each run, however other techniques like JIT compilation may mitigate this).
- in some languages, the programmer has to provide some kind of header for the use of others that will link with this object. For example in C you have to provide
.h
files to go with your object files. But it is good practice anyway. - in languages with text based includes like C or C++, if you change a function prototype, you have to change it in two places. Once in header file, once in the implementation file.
这篇关于为什么要先编译成目标文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!