C/C++ Compilation Basics
Key:
:large_orange_diamond: - Code Example
:large_blue_diamond: - Code Exercise
:red_circle: - Code Warning
Previous: C++ Track Overview
Next: Source & Header Files
Compilation Basics
(We will have more on this topic later in the semester.)
Within this track, we will need to compile our codes.
C and C++ are compiled programming languages contrasting Python which is an interpreted language.
Compiled languages are usually much faster than interpreted languages.
- Compiled languages pass the source code to a compiler, which is translated directly to machine code.
- Interpreted languages pass the source code to a separate program called an interpreter, which reads each line of code and then executes it on the processor.
Compiling Hello, World!
Let's take a look at the canonical Hello, World! to learn the basics of compiling programs from source files.
:large_orange_diamond: hello.c
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Compiling on the command line:
:large_orange_diamond: Compiling with gcc
gcc -o hello hello.c
gcc
: the GNU C compiler-o hello
: output a program namedhello
hello.c
: source file to compile
We can also compile the single file using make.
:large_orange_diamond: Compiling with make (simple version)
make hello
Executing on the command line:
:large_orange_diamond: hello Output
./hello
>>> Hello, World!
:large_orange_diamond: Sandbox Example: Hello World!