Search code examples
compilationcross-platformprogramming-languagesbytecodemachine-code

Do I need to recompile for another processor arc?


I try to understand this whole "compiling" topic in a way more detailed than all those "what is a compiler (doing)?" articles out there.

One big question to me is processor- and os-platform dependency when compiling directly to machine code (e.g. C). I try to formulate concrete questions that needs to be resolved in order to get my picture clearer:

I compile my C code via gcc on a Linux distribution... :

  • Can I run the resulting executable on any other Linux Distribution?
  • Is that executable bound the processor platform compiled on? Do I need to search for another e.g. power-pc gcc when I am running a x86 distro?
  • Can I somehow execute this on windows? I know executables differs but the binary code is the same, isn't it?

So in the end my questions aims on: Is compiling about targeting a specifiy OS paltform, processor platform or both?

Thanks!


Solution

  • Compiling targets both, OS, and Architecture.

    The OS needs to be targeted because:

    • The format of what is an "executable" file is different among operating systems.
    • Programs call the operating system even for common tasks like writing to the console, reading from a file, or terminating cleanly (standards like POSIX mitigate OS dependencies by defining a common layer between the program and the OS).

    The CPU architecture must be targeted because the CPU instructions are different, even among different generations of the "same architecture".

    • Can I run the resulting executable on any other Linux Distribution?

    In general, Yes, but on specific cases it may depend on the type of program (f.i. GUI) and the services assumed available on the OS.

    • Is that executable bound the the processor platform compiled on? Do I need to search for another e.g. power-pc gcc when I am running a x86 distro?

    I don't understand what you mean by "search", but, Yes, you can cross-compile from, say, x86 targeting PPC.

    • Can I somehow execute this on Windows? I know executables differ but the binary code is the same, isn't it?

    These days Windows has Ubuntu integration, and that allows for some kind of exceptions, but the general answer is No, because of the above.