Shouldn't binaries be cross-platform?

Sorry for the newbiness of this question. I am planning to go back to school and get a computer science degree, as I have come to realize that many of my interests and skills are in this area. But for now the very low level operations of a computer are a complete mystery to me.

I began teaching myself c++ yesterday, and wrote some very basic stuff, like what I included below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;
int main() 
{

int a, b;
float result, c;

cout << "setting variable a..." << endl;
a = 5;

cout << "a = "  << a << endl;
cout << "setting variable b..." << endl;

b = 2;

cout << "b = " << b << endl;
cout << "setting variable c..." << endl;

c = 1.618;

cout << "c = " << c << endl;

a = a - b;

cout << "a minus b = " << a << "..." << endl;

result = a / c;

cout << a << " divided by c = " << result << endl;
cout << "K shoots!" << endl;

return 0;

}


I then compiled it on my linux machine using the g++ command where it launches from the shell flawlessly. I run alot of different operating systems on my machines, and would love to be able to write stand alone software that I could run on any of my devices. So as an experiment I took the compiled binary and ran it from .cmd on an older xp machine. It relays the following error:

'16 bit MS-DOS subsystem
C:\\WINDOWS\system32\cmd.exe -nameofmyprogram
The NTVDM CPU has encountered an illegal instruction.
CS:0530 IP:0537 OP:63 2b 2b 2e 73 Choose 'Close' to terminate program'

(I'm assuming that if I compiled the source on the xp machine that it would run just fine, but I have not gone through the trouble of getting a c++ compiler onto it.)

Again, I really don't understand how the CPU operates, but I was under the impression that binary was a univeral language, and that compiled c++ programs should be cross platform.
I guess my question boils down to this:

1)Is this a CPU architecture incompatibility, or an OS issue?

2)Is there a valid option for creating a c++ binary that works on a foriegn OS or CPU besides compiling it on the destination machine?


I'm trying to learn all this stuff with the intention of (hopefully) making it my career one day, so by all means get as detailed as you want. Thanks!
closed account (S6k9GNh0)
Binaries aren't cross platform because each platform has its own format for how an executable is organized. Some hold extra meta data, etc.

However, bits of the binary are the exact same across platforms, specifically ones that are just pure logic and don't interact with the OS handling the executable. For instance, a function that calculates Pi might have the exact same assembly across platforms as long as those platforms are run on the CPU architecture and are compiled in the same way by the same compiler.

EDIT: most *nix systems still use ELF. Windows uses PE. MacOSX uses Mach-O (GET IT?).
EDIT2: Actually, I'm slightly off. While those platforms do use those formats, they use other formats as well such as COFF. You can look at others here: http://en.wikipedia.org/wiki/Comparison_of_executable_file_formats
Last edited on
However, bits of the binary are the exact same across platforms

No, they are not. Even on the same platform (and OP appears to be targeting x86) the available instructions may be different depending on specific modal factors.

Every type of machine has a specific instruction set -- machine code -- that the processor understands. A SunSPARC's architecture and instruction set is significantly different than a Pentium V's, for example.

The point of a high-level computer language is cross-platform design, whereas the point of a compiler is to turn that high-level, cross-platform code into something specific to the targeted system.


Next, as was already mentioned, the operating system makes a difference, as does the way that the compiled code is packed into the binary (executable) file.


There is hope, however. On Linux systems, you can create a "Fat ELF".
http://icculus.org/fatelf/

Similar systems exist for other architectures. Windows/PC systems have worked something like that for years -- but only because the single executable works with backwards-compatible systems.

Google, BTW, is attempting to do this kind of thing for all systems:
http://www.google.com/patents/about?id=b3wiAAAAEBAJ


Hope this helps.
duoas how are you so knowledgeable 0.0
closed account (S6k9GNh0)
However, bits of the binary are the exact same across platforms, specifically ones that are just pure logic and don't interact with the OS handling the executable.For instance, a function that calculates Pi might have the exact same assembly across platforms as long as those platforms are run on the CPU architecture and are compiled in the same way by the same compiler.


Hey but you know... I'm getting used to people not even reading the first 3 sentences of my post.
Last edited on
You prefix it with `for instance'
You can't put constraints in an example and then expect to be understood that they apply to the general case.
Right on, thanks for the info. I'm a big linux user so fatELF should do the vast majority of what I want, and doing some additional compiling in windows and macOS won't be a big deal.

So if you wanted to emulate the way a foreign OS handles an executable on an identical architecture you would end up with something like WINE?

duoas how are you so knowledgeable 0.0

Lol for real, I've only been on this site for a couple days and honestly most of the posts that really helped me have been yours.

Thanks again!
Last edited on
Topic archived. No new replies allowed.