Search code examples
c++binarycross-platform

Cross-platform binary in C++


I have read that a binary is the same for Windows and Linux (I am not sure if it has a different format).

What are the differences between binaries for Linux and binaries for Windows (when speaking about format)?

And if there are none, what stops us from making a single binary for both operating systems (make sure that I mean the last file where you can run and not the source code)?

Since there are differences in the binary format, how are the operating systems themselves compiled?

If I'm not mistaken Microsoft uses Windows to compile the next Windows version/update.

How are these binaries executable by the machine (even the kernel is one of those)?

Aren't they in the same format? (For such a low-level program.)


Solution

  • I have read that a binary is the same for Windows and Linux (not sure if it has a different format)

    Then you have read wrong. They are completely different formats.

    What are the differences between binaries for Linux and binaries for Windows (when speaking about format)?

    Windows: Portable Executable (PE)

    Linux: Executable and Linkable Format (ELF)

    And if there are none

    There are many differences.

    What stops us from making a single binary for both operating systems?

    The fact that different OSes require different binary formats for their respective executable files.

    Since there are differences in the binary format, how are the operating systems themselves compiled?

    Ask the OS manufacturers for details. But basically, they are compiled like any other program (just very complex programs). Like any other program, their source code is compiled into appropriately-formatted executable files for each platform they are targeting, and even for each target CPU. For instance, Windows uses the PE format for all of its executables, but the underlying machine code inside each executable is different whether the executable is running on an x86 CPU vs an x64 CPU vs an ARM CPU.

    If I'm not mistaken Microsoft uses Windows to compile the next Windows version/update

    Yes. That is commonly known as "dog-fooding" or "bootstrapping". For instance, VC++ running on Windows is used to develop new versions of VC++, Windows, etc.

    How are these binaries executable by the machine (even the kernel is one of those)?

    When an executable file is run (by the user, by a program, etc.), a request goes to the OS, and the OS's executable loader then parses the file's format as needed to locate the file's machine code, and then runs that code on the target CPU(s) as needed.

    As for running the user's OS itself, there is an even more fundamental OS running on the machine (i.e., the BIOS) which (amongst other things) loads the user's OS at machine startup and starts it running on the available CPU(s). See Booting an Operating System for more details about that.

    Aren't they in the same format? (For such a low-level program.)

    No.