We need to turn the code we write into instructions the computer can understand and run.
0
0
Compilation and execution process in C++
Introduction
When you write a C++ program and want to run it on your computer.
When you want to check if your code has mistakes before running it.
When you want to make your program faster by turning it into machine code.
When you want to share your program with others without sharing the source code.
Syntax
C++
g++ filename.cpp -o outputname ./outputname
g++ is the compiler that turns C++ code into a program.
The -o option names the output program.
Examples
Compile
hello.cpp into a program called hello and run it.C++
g++ hello.cpp -o hello ./hello
Compile with warnings enabled to catch possible mistakes.
C++
g++ -Wall program.cpp -o program ./program
Sample Program
This simple program prints "Hello, world!" to the screen.
First, you compile it with g++ filename.cpp -o filename, then run the program with ./filename.
C++
#include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
OutputSuccess
Important Notes
Compilation turns your code into machine language the computer understands.
Execution runs the machine code to perform the program's tasks.
If there are errors in your code, the compiler will show messages to help you fix them before running.
Summary
Compilation changes your C++ code into a program the computer can run.
Execution runs the compiled program to do what you wrote.
You must compile before you can execute your C++ program.