SrcForge

Lesson 4

Compiling and Executing

Covers compilation stages, GCC usage, errors, and best practices.

Lesson content

Compiling and executing a C++ program

C++ must be compiled before it runs.

Compilation finds errors. It builds a machine file.

What is compilation?

Compilation turns .cpp code into machine code.

Compiler checks syntax and types first.

Compilation and execution process

             C++ Source Code (.cpp)


            Preprocessor Processes
              (#include, #define)


                 Compiler
          (Checks Syntax & Errors)


                 Object File (.o)


                    Linker
     (Links Standard Library & Other Files)


           Executable File (.exe/.out)


              Program Execution


              Output on the Screen

Stages of compilation

1. Preprocessing

Preprocessor handles include, define, and conditionals.

#include <iostream>

It replaces this with iostream's contents first.

2. Compilation

Compiler turns code into object code.

  • Syntax errors
  • Type errors
  • Invalid declarations
  • Missing semicolons
  • Incorrect function calls

Errors stop compiling. Compiler shows messages.

3. Linking

Linker combines object files and libraries.

It resolves references. Builds final executable.

4. Execution

OS loads executable. Starts at main().

Writing a simple C++ program

#include <iostream>          // Include the input/output stream library

using namespace std;         // Use the standard namespace

int main()                   // Program execution starts here
{
    cout << "Hello, World!"; // Display a message on the screen

    return 0;                // Indicate successful execution
}

Compiling and running using GCC

Suppose your file is hello.cpp.

Step 1: Compile the program

g++ hello.cpp -o hello
  • g++: GNU C++ compiler
  • hello.cpp: Source file
  • -o hello: Creates executable named hello

Step 2: Run the program

:: Windows
hello.exe
.\hello.exe

# Linux / macOS
./hello

Compiling with different compilers

Compiler   | Platform              | Command
---------- | --------------------- | ------------------------------
GCC (g++)  | Windows, Linux, macOS | g++ file.cpp -o program
Clang      | Windows, Linux, macOS | clang++ file.cpp -o program
MSVC       | Windows               | cl file.cpp

Compiling in popular IDEs

  • Visual Studio: Build Solution, then Ctrl+F5
  • VS Code: Install C++ extension, use Run
  • Code::Blocks: Click Build and Run (F9)
  • CLion: Click Run, or Shift+F10

Example: compilation error

// Incorrect
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!"   // Missing semicolon

    return 0;
}
error: expected ';' before 'return'
// Correct
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!";  // Semicolon added

    return 0;
}

Common compilation errors

  • Missing semicolon: add a semicolon
  • Undefined reference: define or link function
  • cout not declared: include iostream, use std::
  • No main(): define int main()
  • File not found: check filename and path

Best practices

  • Save files with .cpp extension
  • Compile often to catch errors early
  • Read compiler messages carefully
  • Use meaningful file names
  • Enable warnings, like -Wall -Wextra
  • Use the latest C++ standard
g++ -std=c++23 hello.cpp -o hello

Summary

  • C++ needs compiling before running
  • Stages: preprocessing, compiling, linking, execution
  • Compilers turn code into machine code
  • GCC, Clang, MSVC are common compilers
  • Reading errors is a key skill

Frequently asked questions

Why does C++ need to be compiled?

Machine code runs faster. Compiling gives better performance.

What is the difference between compiling and executing?

Compiling builds the program. Executing runs it.

What is the purpose of the linker?

Linker joins files and libraries. Builds executable.

Which compiler should beginners use?

GCC is free and cross-platform. MSVC suits Windows users.

What should I do if my program does not compile?

Read errors. Fix issues one by one. Recompile.