0

Is it possible to write a program (make executable) that runs on windows and linux without any interpreters?
Will it be able to take input and print output to console?
A program that runs directly on hardware, pure machine code as this should be possible in theory

edit:
Ok, file formats are different, system calls are different
But how hard or is it possible for kernel developers to introduce another executable format called raw for fun and science? Maybe raw program wont be able to report back but it should be able to inflict heavy load on cpu and raise its temperature as evidence of running for example

  • 1
    Does this answer your question? [Why do we need to compile for different platforms (e.g. Windows/Linux)?](https://stackoverflow.com/questions/48235579/why-do-we-need-to-compile-for-different-platforms-e-g-windows-linux) – Mefitico Jul 27 '21 at 04:41
  • 2
    **Why do you ask?** - what is your motivation? What kind of software do you have in mind? – Basile Starynkevitch Jul 27 '21 at 05:21
  • @basile-starynkevitch this is a theory question for deeper understanding of hardware, operating systems and compilation – Timur Nurmagambetov Jul 27 '21 at 05:29
  • 1
    I’m voting to close this question because it encourages discussion. The topic is also too broad. – Modus Tollens Jul 27 '21 at 06:08

2 Answers2

1

No, mainly because executable formats are different, but...

With some care, you can use mostly the same code to create different executables, one for Linux and another one for windows. Depending on what you consider an interpreter Java also runs on both Windows and Linux (in a Java Virtual Machine though).

Also, it is possible to create scripts that can be interpreted both by PowerShell and by the Bash shell, such that running one of these scripts could launch a proper application compiled for the OS of the user.

You might require the windows user to run on WSL, which is maybe an ugly workaround but allows you to have the same executable for both Windows and Linux users.

Mefitico
  • 816
  • 1
  • 12
  • 41
1

Is it possible to write a program (make executable) that runs on windows and linux without any interpreters?

in practice, no !

Levine's book Linkers and loaders explain why it is not possible in practice.

On recent Linux, an executable has the elf(5) format.

On Windows, it has some PE format.

The very first bytes of executables are different. And these two OSes have different system calls. The Linux ones are listed in syscalls(2).

And even on Linux, in practice, an executable is usually dynamically linked and depends on shared objects (and they are different from one distribution to the next one, so it is likely that an executable built for Debian/Testing won't run on Redhat). You could use the objdump(1), readelf(1), ldd(1) commands to inspect it, and strace(1) with gdb(1) to observe its runtime behavior.

Portability of software is often achieved by publishing it (in source form) with some open source license. The burden of recompilation is then on the shoulders of users.

In practice, real software (in particular those with a graphical user interface) depends on lots of OS specific and computer specific resources (e.g. fonts, screen size, colors) and user preferences.

A possible approach could be to have a small OS specific software base which generate machine code at runtime, like e.g. SBCL or LuaJit does. You could also consider using asmjit. Another approach is to generate opaque or obfuscated C or C++ code at runtime, compile it (with the system compiler), and load it -at runtime- as a plugin. On Linux, use dlopen(3) with dlsym(3).

Pitrat's book: Artificial Beings, the conscience of a conscious machine describes a software system (some artificial mathematician) which generates all of its C source code (half a million lines). Contact me by email to basile@starynkevitch.net for more.

The Wine emulator allows you to run some (but not all) simple Windows executables on Linux. The WSL layer is rumored to enable you to run some Linux executable on Windows.

PS. Even open source projects like RefPerSys or GCC or Qt may be (and often are) difficult to build.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547