Can C++ code be compiled only for specific CPU architectures. And is there any way to compile it to all architectures

Viewed 2204

C++ compiler compiles your code into executable file(.exe) that contains CPU-readable machine code consisting of CPU commands. Different CPUs have different architectures and probably different commands. If CPUs have different commands then executable file compiled for my CPU may not run on another computer with another CPU with another architecture and another commands. For example will program compiled on SkyLake core run on KabyLake core and will program compiled on Intel CPU run on AMD CPU. Am I right? If so, is there any way to install right executable file with right commands on computer? Please also answer questions in example.

2 Answers

You are correct. The output of a compiler is machine code, and different CPU architectures have different instruction sets that require different machine code. So x86 machine code won't run on an ARM-based CPU.

It is possible for a compiler to output machine code for a CPU other than the one the compiler is running on. This is generally referred to as "cross-compilation", and all of the major C++ compilers have this capability to some extent.

Also note that an executable file is just a file that contains machine code. The operating system's program loader is responsible for loading that machine code from the file into memory and actually beginning execution of it. For that reason, its entirely possible to have machine code for multiple CPU architectures in one executable file. The MacOS executable file format, for example, supports this with its "universal binary" format, that lets a single executable file contain machine code for both PowerPC and x86 CPUs.

Different operating systems also use different executable file formats and have different support routines. That means that a program compiled for Windows won't run on Linux even if they're both running on CPUs with the same architecture. This is where things like the Windows Subsystem for Linux and WINE come in, allowing one OS to load and execute a program compiled for a different OS.

To compile for "all architectures" is not really possible because some of the are very different from what you expect.

The best we can do is to compile to a virtual machine byte code such as Java or .NET. Then the compiled code can run on any system that has the virtual machine installed.

Java is pretty universal so it is a good choice. However, because of its focus on object orientation and garbage collection, it is a little tricky to get C++ compiled for it.

Another virtual machine that is becoming very popular is JavaScript WASM (Web Assembly) This used to be ASM.js but it's now more standardized and cross-browser. It takes advantage of the fact that all current browsers compile JS into machine code, and so WASM is a special form of JS that compiles almost one-to-one into machine code.

See https://medium.com/@tdeniffel/pragmatic-compiling-from-c-to-webassembly-a-guide-a496cc5954b8

Related