This guide explains how to set up, build, and run a basic C++ project on macOS, Windows, and Linux.
Ensure you have a C++ compiler installed:
- macOS:
clang(via Xcode Command Line Tools) - Linux:
g++orclang++ - Windows: MinGW (GCC) or Microsoft Visual Studio (MSVC)
Install Xcode Command Line Tools:
xcode-select --installVerify installation:
clang++ --versionUpdate packages and install build tools:
sudo apt update
sudo apt install build-essentialVerify installation:
g++ --version- Download and install MinGW
- Add
bindirectory to your systemPATH - Verify installation:
g++ --version- Install Visual Studio
- Select Desktop development with C++ workload
- Use the Developer Command Prompt to compile code
A simple C++ project may look like this:
project-root/
│
├── src/
│ └── main.cpp
├── include/
├── build/ (optional)
└── README.md
Create a file at src/main.cpp:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Navigate to the project root:
cd /path/to/project-rootg++ -std=c++17 $(find src -name "*.cpp") -o maing++ -std=c++11 -o main.exe src/main.cppcl src\main.cpp./mainmain.exeRemove compiled binaries:
rm -f main main.exe- Use
-std=c++11or higher (e.g.,c++17,c++20) depending on your needs - Keep source files in
src/and headers ininclude/for organization - For larger projects, consider using a build system like CMake
g++ tests/unit/math_test.cpp -o test && ./testcd build
cmake ..
makeThis project is licensed under the MIT License. See the LICENSE file for details.