Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does C/C++ have to be compiled? [closed]

This question assumes that the C/C++ code that is written is portable and does not use platform specific code.

I am beginning to learn C++ and how to compile code. I know that C was created to avoid assembly languages because programs would have to be rewritten for different hardware configurations. I also know that C/C++ has to be recompiled for different computer configurations.

That made me wonder what constitutes a new configuration. I can download an executable file from the internet that works just fine, and the internet obviously does not know my hardware configuration. Does code have to be compiled for each different CPU? Does code compiled for one OS version work for another one? (Does Windows 7 code work for Windows 8 without a recompilation?)

In a nutshell, what conditions define a new configuration that has to be compiled for?

like image 679
Nathan Kinsey Avatar asked Dec 23 '13 15:12

Nathan Kinsey


People also ask

Does C Code need to be compiled?

C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.

Why does C++ need to be compiled?

Because computer architecture is made up of electronic switches and cables that can only work with binary 1s and 0s, you need a compiler to translate your code from high level C++ to machine language that the CPU can understand.

Is C an interpreted or compiled?

Example of compiled language – C, C++, C#, CLEO, COBOL, etc. Example of Interpreted language – JavaScript, Perl, Python, BASIC, etc.

Does C get compiled to assembly?

Languages like Pascal, FORTRAN, C, C++, Java, and C# are usually compiled. Compiling is another word for "translating". Computers runs a language called assembly language (more accurately, machine code).


1 Answers

C and C++ are compiled programming languages. A compiled language is a programming language whose implementations are typically compilers - translators which generate machine code from source code -, and not interpreters. Machine code is a set of instructions executed directly by a computer's processor. There is two things make programs incompatible with hardware/software combinations. Most important one in The Machine (Hardware) you are trying to run your binary on. For example you need to know that a program compiled on a 64-bit CPU, can not be run on a 32-bit or ARM processor.

There are dozens of computer architectures and sub-architectures. Major architectures are x86, x86_64 (amd64) and probably ARM. If you are using a language that it's code is going to be compiled to machine code, you will be able to distribute binary artifacts only on computers that have a CPU compatible with yours (Unless you are cross-compiling).

Hardware is not the only difference between platforms. Usually your code is meant to be handled by Operating System before meeting CPU. The part of operating system that do this job is called application Binary Interface. You can't run an ELF binary compiled on Linux with Windows even if on same machine, because Win32 executables are different.

like image 162
sorush-r Avatar answered Oct 04 '22 03:10

sorush-r