C++ is a compiled language: you don't run a source file directly. A compiler translates it into object code, a linker combines the pieces into an executable, and then you run that executable. This chapter walks through installing a toolchain, building your first program, picking a language standard, turning on warnings, and setting up a minimal CMake project.
Installing a compiler
There are three major compilers, and which one you reach for usually depends on your platform.
On Linux and macOS, one command is usually enough:
# Debian / Ubuntu: g++, make and friends
sudo apt install build-essential
# Fedora
sudo dnf install gcc-c++
# macOS: clang++ plus the basic developer tools
xcode-select --install
# Check what you got
g++ --version
clang++ --version
For MSVC, install the C++ workload from the Visual Studio Installer, then open Developer Command Prompt or Developer PowerShell from the Start menu. Those shells are preconfigured so that cl and its libraries are on the path; an ordinary terminal won't find them.
Your first program
Save the following as hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
return 0;
}
The #include <iostream> line pulls in the declarations for the standard I/O streams. Execution begins in main, and returning 0 tells the operating system that everything went fine. std::cout is the standard output stream; the << operator pushes values into it.
Compiling and running
# GCC
g++ -std=c++20 -Wall -Wextra -o hello hello.cpp
./hello
# Clang
clang++ -std=c++20 -Wall -Wextra -o hello hello.cpp
./hello
MSVC spells its options differently:
cl /std:c++20 /EHsc /W4 hello.cpp
hello.exe
/EHsc selects standard C++ exception handling. You will want it on practically any program that uses the standard library.
Picking a language standard
A new ISO C++ standard ships roughly every three years. C++11 was the big modernization, followed by C++14, C++17, C++20 and C++23. Compilers don't all default to the same one, so it pays to state the version explicitly.
-std=c++17 gives you structured bindings, std::optional, std::filesystem and more.-std=c++20 adds concepts, ranges, std::format and modules. It is a sensible baseline for new code.-std=c++23 brings std::expected, std::print and other additions. Check how far your compiler and standard library have implemented it before relying on it.
GCC and Clang also accept -std=gnu++20, which enables GNU extensions on top. If portability matters, stick with -std=c++20.
Turn the warnings on
Out of the box, C++ compilers stay quiet about a lot of suspicious code. With warnings enabled they will point out uninitialized variables, comparisons between signed and unsigned integers, unused variables and plenty more.
- GCC and Clang: use
-Wall -Wextra -Wpedantic as a baseline, and add -Werror if you want warnings to fail the build. - MSVC: use
/W4, plus /WX to treat warnings as errors. - Add
-g (or /Zi on MSVC) for debug information, and -O2 (or /O2) for optimized release builds.
A minimal CMake project
Typing compiler commands by hand stops being fun after a couple of files. CMake is the de facto standard build configuration tool for C++: it reads a CMakeLists.txt and generates project files for a real build system such as Ninja, Make or Visual Studio.
cmake_minimum_required(VERSION 3.20)
project(hello LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(hello hello.cpp)
if(MSVC)
target_compile_options(hello PRIVATE /W4)
else()
target_compile_options(hello PRIVATE -Wall -Wextra -Wpedantic)
endif()
Put this file next to hello.cpp and build out of source, so generated files never mix with your code:
cmake -S . -B build
cmake --build build
./build/hello
With the Visual Studio generator the executable lands in a per-configuration folder such as build/Debug/hello.exe. Visual Studio, VS Code with the CMake Tools extension, and CLion can all open a CMakeLists.txt directly as a project.
Summary
- C++ programs are built with a compiler (GCC, Clang or MSVC) before they run.
- Always pick a standard explicitly, for example
-std=c++20, and enable warnings with -Wall -Wextra or /W4. - One-line compiler commands are fine for experiments; use CMake for anything real.
- Next up: variables, functions and control flow.
0 条评论
登录 · 登录后即可发表评论。