Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If both Mac OS and Windows use the x86 instruction set, why do we have to recompile for each platform?

Tags:

c++11

If both Mac OS and Windows, running on Intel processors, use the x86 instruction set, why can't a program written using only C++11 (no OS Specific libraries, frameworks or API's), run on both without having to recompile for that platform ?

Ultimately the program gets compiled to machine code, so if the instruction set is the same, whats the difference ? What's really going on ?

EDIT: I'm really just talking about a simple "Hello world" program compiled with something like gcc. Not Apps!

EDIT: For example:

    #include<iostream>
    using namespace std;

    int main()
    {
        cout << "Hello World!";
        return 0;
    }

EDIT: An even simpler program:

int main(){
    int j = 2;
    j = j + 3;
}
like image 712
Rahul Iyer Avatar asked Nov 30 '25 15:11

Rahul Iyer


2 Answers

Because a "program" nowadays consists of more than just a blob of binary code. Their file formats are not cross-compatible (PE/COFF vs. ELF vs. Mach-O). It's kind of silly when you think about it, yes, but that's the reality. It wouldn't have to be this way if you could start history over again.

Edit:

You may also want to see my longer answer on SoftwareEngineering.StackExchange (and others').

like image 109
user541686 Avatar answered Dec 02 '25 05:12

user541686


Even "Hello, world" needs to generate output. That will either be OS calls, BIOS calls at a somewhat lower level, or, as was common in DOS days for performance reasons, direct output to video via I/O calls or memory mapped video. Any of those methods will be highly specific to the operating system and other design issues. In the example you've listed, iostream hides those details and will be different for each target system.

like image 30
manassehkatz-Moving 2 Codidact Avatar answered Dec 02 '25 05:12

manassehkatz-Moving 2 Codidact



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!