Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Compile to an Object File First?

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.

One has an explicit statement creating .o files from modules (e.g. gfortran -c filea.f90) before creating the executable.

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).

  • Is there a reason (other than, maybe, Makefiles) that one method is preferred over the other?
like image 976
tomshafer Avatar asked Mar 12 '11 16:03

tomshafer


People also ask

What is the purpose of an object file?

An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable. There are various formats for object files, and the same machine code can be packaged in different object file formats.

What is the difference between object file and executable?

The main difference between object file and executable file is that an object file is a file generated after compiling the source code while an executable file is a file generated after linking a set of object files together using a linker. C is a general-purpose, high-level programming language.

Why is it called an object file?

When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product. The term object stands here for sequences of unlinked machine code (basically). An object file contains objects.

Is object file created in compiler?

Object file(.o): These files are produced as the output of the compiler. They consist of function definitions in binary form, but they are not executable by themselves and by convention their names end with .o. Binary executables file(.exe): These files are produced as the output of a program called a “linker“.


1 Answers

Compiling to object files first is called separate compilation. There are many advantages and a few drawbacks.

Advantages:

  • easy to transform object files (.o) to libraries and link to them later
  • many people can work on different source files at the same time
  • faster compiling (you don't compile the same files again and again when the source hasn't changed)
  • object files can be made from different language sources and linked together at some later time. To do that, the object files just have to use the same format and compatible calling conventions.
  • separate compilation enables distribution of system wide libraries (either OS libraries, language standard libraries or third party libraries) either static or shared.

Drawbacks:

  • 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.
like image 83
kriss Avatar answered Sep 25 '22 10:09

kriss