Executable Files

So I recently installed Ubuntu, and started to teach myself the gcc toolchain, which greatly increased my knowledge of how all the .cpp and .h files I write actually fit together, but there is still one thing I don't understand:
When the object files have all been linked together to form the executable, it is in machine code, right? So how come executable files are portable across different computers? (For example, when I first started c++ with a friend of mine, we would often trade .exe files to see each other's programs) Were we just lucky enough to be using the same processor (Which I don't know if we did)
Or is there an intermediate step between clicking on the application, and it running, where the computer "decodes" instructions so they match its own processor? I've read that there is some sort of pre-execution step where relative addresses are resolved to absolute addresses?
Last edited on
IIRC it's not exactly machine code, as in there are other things in the exe that allow the OS to handle it. But, it is what we call "natively compiled" and would need to be recompiled for different processor architectures. Most Processor manufacturers (and any you're likely to come in contact with) follow a standard when creating their processor that keeps it compatible with others of the same architecture. Chances are both you and your friend were running a processor based on the x86 or x86_64 standard (any retail desktop computer with an Intel or AMD processor over the last 20 years).

note that x86_64 is essentially an extension to the x86 standard and is written for 64bit processors (while x86 is for 32bit processors), and programs compiled for an x86_64 processor cannot run on an x86 processor.
Oh, okay, make sense!
Thanks!
closed account (S6k9GNh0)
The main difference between an executable on Linux and Windows (and any other OS) is layout. Machine code for the i386 architecture looks no different on Windows or Linux.

If you compile an executable into a given layout and move into a different computer, as long as the hardware recognizes the machine code and the OS recognizes the layout, there shouldn't be a problem.

A project distinctly comes to mind where a native implementation of the WinAPI into Linux was made and you could directly execute PE based executables directly without a 3rd party application.
Last edited on
I tried doing that the other way around but I found out that it's apparently impossible to write custom executable loaders in Windows drivers :(
Topic archived. No new replies allowed.