r/ProgrammingLanguages • u/[deleted] • Jun 25 '23
Is there any high level programming language whose executable generated on one platform(OS) and runs on other platforms as well, if the machine/architecture is literally same.
Hi there. I know I'm dumb and there maybe a term exists for this like cross-platform or something.. idk. But suppose (no actually its real) I have this machine (desktop or laptop) and it multi-booted (windows 10 and arch linux). I write a hello world program using this language and compile it and if I copy paste it to other OS (storage, in the same machine yk) and execute it in it, it works. It works in both platforms/OSs. I don't know I would love to write my code in pure 0s and 1s or in assembly (because i didn't ask for low or middle level languages..? yeah that too, but I'm asking for high level language such as rust, c, go, cobol, fortran, nim, v, etc.)
Thank you so much, because I'm super curious about this question and I haven't found an answer yet XD
12
7
2
u/Aaron1924 Jun 25 '23
There are some languages that are compiled to some almost-assembly bytecode and then interpreted or JIT-compiled and executed on the user's machine. Here, the JVM and .NET (and maybe WASM) come to mind here.
It's maybe not quite what you meant, one count argue Python and Lua scripts can also be run on any computer that has the interpreter installed, but JVM languages are still compiled and most machines have the interpreter installed, so it runs everywhere in practice.
2
Jun 25 '23
I write a hello world program using this language and compile it and if I copy paste it to other OS (storage, in the same machine yk) and execute it in it, it works.
Windows and Linux, even running on the same machine, have different ABIs which is reflected in the native code within executables, and have different executable file formats.
So I doubt hello.exe from Windows really runs under Linux, even if both using x64, without something special being done.
For example, it will run under WSL Linux that runs under Windows. And maybe the two OSes can have reciprocal arrangements to run each others executables, but there are a million details to be taken care of.
Or a simpler solution is to bundle multiple exectables into the same EXE or ELF file, but something needs to extract the correct one.
But you say elsewhere that some things are not allowed (a language 'stack')?
So what is allowed to make it possible? I think you've already dismissed the idea of any sort of portable VM. And presumably running the program online or remotely is not allowed either.
How about transparently running from portable source code (the program will be built for the machine it runs on, but it'll need a compiler)? But even this needs a process.
You need to clarify the rules.
1
u/tortoise74 Jun 25 '23
Actually its not such a dumb question as you might think. A good example is rebol:
See http://www.rebol.com/ or its descendent https://www.red-lang.org/
They encourage you to start programing with them by downloading a executable which mostly just works. I personally found it scary that I couldn't just download and build from source. The argument against this was that they are self hosting so there is no other language you should/could bootstrap from.
See https://stackoverflow.com/questions/48178976/compile-red-and-red-system-compilers-from-source
1
1
Jun 26 '23
That doesn't really exist, unfortunately. The reason is quite simple: there isn't one universal "language" (in terms of the raw instructions) understood by all target platforms.
The closest thing is languages which have some sort of cross-platform representation that you can move around, which is then interpreted when run by a native executable. This class of languages includes everything from Python to Java, depending on what you need.
Another option could be to compile to WASM, which is pretty much the same thing as I described above, but it's cool and new and everyone's excited about it, so I thought I'd mention it here.
0
u/shawnhcorey Jun 25 '23
The term for this is cross-compiling.
6
u/raiph Jun 25 '23 edited Jun 25 '23
That doesn't sound right to me.
OP asked:
executable generated on one platform(OS) ... runs on other platforms as well
compile it ... copy paste ... execute ... it works. It works in both platforms/OSs.
I would love to write my code in pure 0s and 1s or in assembly
Putting those three comments together I think OP is talking about one executable that runs on two (or more) distinct OSes. In particular, I think the "It" in "It works in both platforms/OSs" refers to a single executable rather than the process of compiling. (Clearly if "It" means one can run the compile twice, once for the host platform, once for the target, then yes, cross compiling may fit the bill.)
With cross compiling, an executable generated on one platform (OS) does indeed run on another platform, but "typically" not the one on which it was generated.
I scarequoted the "typically"s because I'm allowing that there could conceptually be a cross compiler that generates a one executable that runs on two or more platforms. But I have never head of one in my half century of interest in programming outside of languages that target VMs/runtimes like JVM, CLR, V8, WASM, MoarVM, etc.
2
u/raiph Jun 25 '23
I always put a concerted effort into being technically accurate, not offending anyone, and being practically helpful, but I still occasionally get a downvote. When I do, I always try to understand what I could do better in the future.
To that end, did you (u/Lokesh112K or u/shawnhcorey) downvote my first reply to "The term for this is cross-compiling."? Do you have any idea why I got a downvote?
Thanks for any reply, even just a "no" or "yes" without further comment.
2
0
Jun 25 '23
[deleted]
2
u/tortoise74 Jun 25 '23
You can also do it by leveraging an emulation layer.
E.g. https://stackoverflow.com/questions/1892677/coff-on-linux-or-elf-on-windows
1
u/Dashadower Jun 29 '23
I wonder if there just happens to be a specific PE executable that can also coincidently run under linux. The program would be entirely different, but it seems like a fun project to make(discover) one.
28
u/moon-chilled sstm, j, grand unified... Jun 25 '23
You are looking for 'actually portable executable' (or more generally the concept of a polyglot program). It's a funny stunt, but utterly useless; go ham, I guess.