UnicMinds

source code vs. object code vs. executable code

Source Code vs. Object Code

A source code is what you and I can read and write in programming languages such as C, C++, and Python. These are human understandable syntax statements. 

When we compile this source code, it gets converted or translated into object code. Object code is usually with .obj or .o  or .oo extensions. This is in binary 1s and 0s and cannot be understandable by humans. Usually it’s either machine instructions for a specific processor or byte code for a VM. This object code is not something that you can run. For example, if you run it or double click it nothing happens. That is because it is not an executable code yet. When we use the term “executable code”, sometimes it is used very loosely to even refer to object code. Object which is not linked is not an executable code, it is just Object code. What is very important to understand is: Object code is specific to the CPU in which the compiler ran it, which mean object code contains instructions that only that particular CPU can understand; for example, x86 or Power-PC.

To make the object file into an executable code, you need to link all the other object files and make it into an executable file using a linker. This will then become an executable that you can run with a double click by using a command line code runner like gcc. The distinction between object code and executable code is that object code often contains placeholders for references to variables and functions defined in other modules. Those references aren’t executable and are resolved by linking object files and libraries. Executable code is the final code that you run or execute (as the name suggests). 

Difference between Source Code and Object Code

Source code is something like:

  • #include <stdio.h> 
  • int main() 
  •    int testInteger; 
  •    printf(“Enter an integer: “); 
  •    scanf(“%d”, &testInteger);   
  •    printf(“Number = %d”,testInteger); 
  •    return 0; 

The “program” that results from compiling that is a bunch of values (numbers) that tell the computer to do exactly what that source code says to do. (Depending on the CPU it was compiled for, the numbers could be anything – each number can be 0–15, and each instruction can compile down to any number of “numbers”.) It’s a lot easier to make sense of the source code than it is to understand “32 42 38 97 98 70 99 87 41 23”. But the CPU understands the numbers, not the source code.

Some languages such as Java and Python have something called Byte Code. Byte code (in Java and Python and perhaps others) is intermediate code between source code and machine code that is executed by the virtual machine.  When we compile a .java file, it gets converted into a .class file and this .class file is given to the JVM. Each OS will have its own JVM but the .class file (with bytecodes) is platform independent. So, the JVM reads the .class file and converts it to machine language code for execution. This makes bytecode portable across multiple platforms (combination of hardware and operating system) as long as a virtual machine is implemented on that platform.

Hope this is useful, thank you.

You may like to read: Top Free Coding Platforms in 2024, Compilers, Assemblers, Linkers, and Loaders – What are they?, & Making a Program Counter using Multiplexers

BOOK A FREE TRIAL