Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between code object and executable file

Tags:

c++

linker

I'm a C++ beginner and I'm studying the basics of the language. There is a topic in my book about the compiler and my problem is that I can not understand what the text wants to say:

C++ is a compiled language so you need to translate the source code in a file that the computer can execute. This file is generated by the compiler and is called the object code ( .obj ), but a program like the "hello world" program is composed by a part that we wrote and a part of the C++ library. The linker links these two parts of a program and produces an executable file ( .exe ).

Why does my book tell that the file that is executed by the computer is the one with the obj suffix (the object code) and then say that it is the one with the exe suffix?

like image 979
Piero Borrelli Avatar asked Sep 17 '14 16:09

Piero Borrelli


People also ask

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.

What is executable object?

Executable Object Code means the computer programming code in any other form than Source Code that is not readily perceivable by humans and suitable for machine execution without the intervening steps of interpretation or compilation.

What is a executable code file?

An executable file (EXE file) is a computer file that contains an encoded sequence of instructions that the system can execute directly when the user clicks the file icon. Executable files commonly have an EXE file extension, but there are hundreds of other executable file formats.


3 Answers

Object files are source compiled into binary machine language, but they contain unresolved external references (such as printf,for instance). They may need to be linked against other object files, third party libraries and almost always against C/C++ runtime library.

In Unix, both object and exe files are the same COFF format. The only difference is that object files have unresolved external references, while a.out files don't.

like image 111
ArunasR Avatar answered Oct 04 '22 04:10

ArunasR


The C++ specification is a technical document in English. For C++11 have a look inside n3337 (or spend a lot of money to buy the paperback ISO standard). In theory you don't need a computer to run a C++ program (you could use a bunch of human slaves, but that would be unethical, inefficient, and unreliable).

You could have a C++ implementation which is an interpreter, not a compiler (e.g. Ch by SoftIntegration)

If you install Linux on your laptop (which I recommend doing to every student) then you could have several free software C++ compilers, in particular GCC and Clang/LLVM (using g++ and clang commands respectively). Source files are suffixed .cc, or .cxx, or .cpp, or even .C (I prefer .cc), and you could ask the compiler to handle a file of some other suffix as a C++ source file (but that is not conventional). Then, both object files (suffixed .o) and executables share the same ELF format. Conventionally, executables don't have any suffix (e.g. g++ is a binary executable, not doing much except starting other processes like cc1plus -the compiler proper-, as -the assembler-, ld -the linker- etc...)

In all cases I strongly recommend:

  • to enable all warnings and debug info during compilation (e.g. use g++ -Wall -g ....)
  • to improve your source code till you got no warnings
  • to learn how to use the debugger (gdb)
  • to be able to build your program on the command line
  • to use a version control system like git
  • to use a good editor like emacs, gedit, geany, or gvim
  • once you are writing programs in several source files, learn how to use a builder like make
  • to learn C++11 (or even perhaps C++14) rather than older C++ standards
  • to also learn other programming languages (Ocaml, Scheme, Haskell, Prolog, Scala, ....) since they would improve your thinking and your way of coding in C++
  • to study the source code of several free software coded in C++
  • to read the documentation of every function that you are using, e.g. on cppreference or in man pages (for Linux)
  • to understand what is undefined behavior (the fact that your program sometimes work does not make it correct).

Concretely, on Linux you could edit your Hello World program (file hello.cc) with gedit or emacs (with a command like gedit hello.cc) etc..., compile it using g++ -Wall -g hello.cc -o hello command, debug it using gdb ./hello, and repeat (don't forget to use git commands for version control).

Sometimes it makes sense to generate some C++ code, e.g. by some shell, Python, or awk script (or even by your own program coded in C++ which generates C++ code!).

Also, understand that an IDE is not a compiler (but runs the compiler for you).

like image 26
Basile Starynkevitch Avatar answered Oct 04 '22 04:10

Basile Starynkevitch


The basic steps for creating an application from a C or C++ source file are as follows: (1) the source files are created (by a person or generated by a program), (2) the source files are compiled (which is really two steps, Preprocessor and compilation) into object code, (3) the object files that are created by the C/C++ compiler are linked to create the .exe

So you have these steps of transforming one version of the computer program, the source files, to another, the executable. The C++ source is compiled to produce the object files. The object files are then linked to produce the executable file.

In most cases there are several different programs involved in the compile and link process with C and C++. Each program takes in certain files and creates new files.

  • C/C++ Preprocessor takes in source code files and generates source code files
  • C/C++ Compiler takes in source code files and generates object code files
  • the linker takes in object code files and libraries and generates executable files

See What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?

Most compiler installations have a program that runs these various applications for you. So if you are using gcc then gcc program will run first the C++ Preprocessor then then C++ compiler and then the linker. However you can modify what gcc does with command line options to tell it to only run the C++ Preprocessor or to only compile the source files but not to link them or to only link the object code files.

A brief history of computer languages and programming

The languages used for programming computers along with the various software development tools have evolved over the years.

The first computers were programmed with numbers entered by switches on a console.

Then people started developing languages and software that could be used to create software more easily and quicker. The first major development was creating assembler language where each line of source was converted by a computer program into a machine code instruction. Along with this came the development of linkers (which link pieces of machine code together into larger pieces). Assemblers were improved by adding a macro or preprocessor facility somewhat like the C/C++ Preprocessor though designed for assembly language.

Then people created programming languages that looked more like people written languages rather than assembler (FORTRAN and COBOL and ALGOL for instance). These languages were easier to read and a single line of source might be converted into several machine instructions so it was more productive to write computer programs in these languages rather than assembler.

The C programming language was a later refinement using lessons learned from the early programming languages such as FORTRAN. And C used some of the same software development tools that already existed such as linkers which already existed. Still later C++ was invented, starting off as a refinement of C introducing object oriented facilities. In fact the first C++ compiler was really a C++ translator which translated C++ source code to C source code which was then compiled with a C compiler. However modern C++ is compiled straight to machine code in order to provide the full functionality of the C++ standard with templates, lambdas, and all the other things with C++11 and later.

linkers and loaders

When you run a program you run the executable file. The executable file contains several kinds of information. The first is the machine instructions that are the result of compiling the C++ source code. The other is information that the loader uses in order to know how to load the executable into memory.

In the old days, long long ago all libraries and object files were linked together into an executable file and the executable file was loaded by the loader and the loader was pretty simple.

Then people invented shared libraries and dynamic link libraries and this required the linker to be more complex and the loader to be more complex.

The linker became more complex because it had to be able to recognize the difference between using a shared library and a static library and be able to generate an executable file that not only contains the linked object code but also information for the loader about any dynamic libraries.

The loader became more complex because not only does the loader have to load the executable file into memory so that it can start running, the loader must also find any shared libraries or dynamic link libraries that are also needed and load those too. And the loader also has to do a certain amount of linking of the additional components, the shared libraries, so the loader does a lot more than it used to do.

See also

  • Difference between shared objects (.so), static libraries (.a), and DLL's (.so)?

  • What is an application binary interface (ABI)?

  • How to make a SIMPLE C++ Makefile

like image 27
Richard Chambers Avatar answered Oct 04 '22 04:10

Richard Chambers