Why do we need executable files in a machine code operating system?

On Windows, when I compile a simple "C" program, I get the final executable machine code .exe code. Using gcc on unix does the same thing with a .out machine code .out .

What is the difference between the two?

My main question is: .exe and .out are machine codes, why do they depend on the operating system?

Like on Unix, I cannot execute .exe directly, and on Windows I cannot execute .out Unix. Why is this so?

+6
c operating-system
source share
3 answers

All this is related to how the program loads.

Windows and Linux have different formats for how they want the program to define itself.

Linux usually uses the PE format.

These formats define different program data needed to execute machine instructions.

In addition, the operating system interfaces are different, so you need to use different libraries and make different system calls.

For a simple program, you can usually just recompile another operating system so that it works on both, but you cannot use one file on both.

+12
source share

The operating system abstracts access to basic equipment and makes it accessible to programmers through system calls. On Windows, this is done through the Windows API (which, as a rule, is further abstracted by libraries that make programming easier, such as MFC, etc.). On UNIX, this is often done with interrupts, which System C simplifies a bit by following the POSIX api (often with several system-dependent additions).

For example, on Linux, system calls are made through int 0x80 , with several registers loaded with function arguments, and the C library simplifies them, allowing you to call, for example. read , with the expected arguments ( int fd, void *buf, size_t count ) . This translates to an interrupt call to which the kernel responds.

These two ways of making requests to the operating system are incompatible, and therefore you (as a rule) cannot run the Windows executable on UNIX systems and vice versa, without using any additional system that acts as a translation level, for example, WINE, VMWare, etc. d. (Although the two works are very different).

(By the way, a.out says nothing about the contents of the executable file, this is the traditional file name assigned to executable files compiled on UNIX systems, and is not suitable for โ€œassembler output.โ€ GCC allows cross-compilation, so you can even compile Win32 -compatible .EXE files with it. You can use the -o flag for gcc to specify the name of the output file, which indicates that it does not affect the actual format of the output file.)

+1
source share

In a unix environment, any file with the bit set + x is considered executable. Remember that not even binary files can be executable (shell scripts, batch files, etc.). Windows relies on the concept of file extension, on Unix we just install chmod +x filename .

You can always use the -o file flag to force gcc to create any file name you like.

+1
source share

All Articles