May 2, 2025

C++ (some programs that might be interesting)

DownloadOpen

Throughout the free stuff section you will see topics with (C++). These topics include a code block that you can run locally on your terminal on your PC or Mac. They would all run equally well on python but as you get to very large numbers (and you might if you are interested; then there is no competition and you need C).

All the programs were constructed on a 2019 MacBook (2.3 GHz 8-Core Intel Core i9; 16 GB 2667 MHz DDR4), they all run fine.

As a general guide you will need a few things on your machine which will help you run the programs depending on your machine type follow the relevant instructions below.

### Installing and Running C++ on macOS, Windows, and Linux

#### macOS
##### Installation
1. Install Xcode Command Line Tools (includes g++ compiler):
   - Open Terminal and run:
     xcode-select --install
   - Follow the prompts to install.
2. Verify installation:
     g++ --version
3. (Optional) Install Homebrew for additional tools:
     /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
   - Then install GMP (for programs using <gmp.h> like the example):
     brew install gmp

##### Compilation
- Compile a C++ file (e.g., `program.cpp`):
    g++ -o program program.cpp
- With GMP library:
    g++ -o program program.cpp -lgmp -lgmpxx
- Run the program:
    ./program

##### Guidance
- Use a text editor like Visual Studio Code, Sublime Text, or Xcode for writing code.
- If GMP is needed, link it explicitly with `-lgmp -lgmpxx` during compilation.
- Update Xcode tools if you encounter version issues:
    softwareupdate --all --install

#### Windows
##### Installation
1. Install MinGW (Minimalist GNU for Windows):
   - Download from: http://www.mingw.org/ or use MSYS2 (recommended) at https://www.msys2.org/
   - MSYS2 steps:
     pacman -Syu
     pacman -S mingw-w64-x86_64-gcc
     pacman -S mingw-w64-x86_64-gmp
2. Add MinGW to PATH:
   - Search "Environment Variables" in Windows, edit PATH, add: `C:\msys64\mingw64\bin` (adjust path as needed).
3. Verify:
     g++ --version

##### Compilation
- Compile a C++ file (e.g., `program.cpp`):
    g++ -o program program.cpp
- With GMP library:
    g++ -o program program.cpp -lgmp -lgmpxx
- Run the program:
    program.exe

##### Guidance
- Use Command Prompt, PowerShell, or an IDE like Visual Studio Community (which includes its own compiler).
- For GMP, ensure it’s installed via MSYS2 or manually download and link libraries.
- If using Visual Studio, create a new C++ project instead of command-line compilation.

#### Linux
##### Installation
1. Install GCC (includes g++):
   - On Ubuntu/Debian:
     sudo apt update
     sudo apt install build-essential
   - On Fedora:
     sudo dnf groupinstall "Development Tools"
   - On Arch:
     sudo pacman -S base-devel
2. Install GMP (if needed):
     sudo apt install libgmp-dev  # Ubuntu/Debian
     sudo dnf install gmp-devel   # Fedora
     sudo pacman -S gmp           # Arch
3. Verify:
     g++ --version

##### Compilation
- Compile a C++ file (e.g., `program.cpp`):
    g++ -o program program.cpp
- With GMP library:
    g++ -o program program.cpp -lgmp -lgmpxx
- Run the program:
    ./program

##### Guidance
- Use terminal-based editors (nano, vim) or GUI editors (VS Code, Geany).
- Ensure you have permissions to execute the compiled file (`chmod +x program` if needed).
- Update packages regularly:
    sudo apt update && sudo apt upgrade  # Ubuntu/Debian

#### General Tips for All Platforms
- **Text Editors/IDEs**: Use Visual Studio Code, CLion, or a simple editor like Notepad++ to write code.
- **File Extensions**: Save C++ files with `.cpp` or `.cxx`.
- **Debugging**: Add `-g` flag to compile with debug info:
    g++ -g -o program program.cpp
- **Optimization**: Use `-O2` for optimized code:
    g++ -O2 -o program program.cpp
- **Errors**: Read compiler output carefully—it often points to the line and type of error.
- **Libraries**: If using external libraries (like GMP), ensure they’re installed and linked (e.g., `-l<library>`).
- **Learning**: Practice with small programs, use `std::cout` for output, and explore online resources like cplusplus.com.