リリース・改善中
C++ ガイド · 1/6
この章は現在、英語でのみ提供しています。
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.
There are three major compilers, and which one you reach for usually depends on your platform.
| Compiler | Typical platform | How to get it |
|---|---|---|
GCC (g++) | Linux, Windows via MinGW-w64 | Your distribution's package manager |
Clang (clang++) | macOS, Linux, Windows | Xcode Command Line Tools or a package manager |
MSVC (cl) | Windows | Visual Studio or Build Tools with the "Desktop development with C++" workload |
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++ --versionFor 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.
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.
# GCC
g++ -std=c++20 -Wall -Wextra -o hello hello.cpp
./hello
# Clang
clang++ -std=c++20 -Wall -Wextra -o hello hello.cpp
./helloMSVC 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.
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.
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.
-Wall -Wextra -Wpedantic as a baseline, and add -Werror if you want warnings to fail the build./W4, plus /WX to treat warnings as errors.-g (or /Zi on MSVC) for debug information, and -O2 (or /O2) for optimized release builds.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/helloWith 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.
-std=c++20, and enable warnings with -Wall -Wextra or /W4.
コメント 0件
ログイン · ログインするとコメントできます。
最初のコメントを書いてみましょう。