Why does a 64-bit executable built on 64 bit OS not run on a second 64-bit CPU having a 32-bit OS

StackOverflow https://stackoverflow.com/questions/20925988

Question

I have two computers both with 64 bit Intel processors . One computer(A) has Ubuntu 10.04 64-bit and the second(B) has Ubuntu 11.04 32-bit operating systems, (verified using the commands on this site)

My default compiler is gcc on both the machines. From what I know, during compilation gcc detects what OS type (32bit/64bit) it compiles on and accordingly makes a 32-bit or a 64-bit executable.

Now I wrote a simple Hello World Program in C++ on A, and then tried to execute it on B.

The message I got was

zsh: exec format error: ./hello

Why is this the case? Being a 64 bit CPU it should execute 64-bit executable irrespective of what the operating system type is.

However a hello world executable built on B runs on A. This is expected, since 64 bit CPU's are supposed to run 32-bit executable.

Is there some conceptual error I am making here?

Était-ce utile?

La solution

It is the operating system's job to run the program and the 32 bit OS doesn't recognise the 64 bit executable. Also the program will call some system libraries and be looking for the 64 bit libraries. But in this case it is a 64 bit (ELF?) binary and the 32 bit kernel will have no clue what to do with it.

A 32 bit OS could be written that could run 64 bit programs. They are not.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top