Modern C++ 開発。
production 向けの C++26-first 環境です。プロジェクト所有 modules、明示的な error contract、RAII、target-based CMake、Ninja、プラットフォームネイティブ toolchain を使います。必要な compiler または standard library が選択した C++26 機能に未対応の場合は C++23 を互換パスとして使います。
エンジニアリングベースライン。
| Area | Default | Rule |
|---|---|---|
| Language | C++26 | 現在の言語パスを優先し、実際の toolchain 対応が必要とする場合のみ C++23 を使います。 |
| Project boundaries | C++ modules | New project-owned production boundaries use .cppm modules by default. |
| Standard library | Minimal headers | Use normal standard headers in the global module fragment; do not depend on experimental import std;. |
| エラー | std::expected | 回復可能な失敗は sentinel value に隠さず API contract の一部にします。 |
| Console output | std::print / std::println | 新しい iostream 挿入チェーンより、モダンな formatted output を優先します。 |
| Build model | Target-based CMake | compile features、sources、definitions、dependencies は所有する target に紐付けます。 |
| Generator | Ninja | compiler、configuration、target ごとに別の build tree を使います。 |
headers ではなく modules から始めます。
プロジェクト所有の境界は責務を直接表現します。declarations は module interface から export し、非自明な implementation は implementation unit に置き、platform/third-party code は明示的な adapter 境界に隔離します。
src/greeting/greeting.cppmmodule;
#include <expected>
#include <string>
#include <string_view>
export module genyleap.greeting;
export namespace genyleap::greeting {
enum class GreetingError {
EmptyName
};
[[nodiscard]] auto makeGreeting(std::string_view name)
-> std::expected<std::string, GreetingError>;
}
src/greeting/greeting.cppmodule;
#include <expected>
#include <format>
#include <string>
#include <string_view>
module genyleap.greeting;
namespace genyleap::greeting {
auto makeGreeting(std::string_view name)
-> std::expected<std::string, GreetingError>
{
if (name.empty()) {
return std::unexpected {GreetingError::EmptyName};
}
return std::format("Hello, {}.", name);
}
}
src/main.cpp#include <print>
import genyleap.greeting;
int main()
{
const auto greeting = genyleap::greeting::makeGreeting("Genyleap");
if (!greeting) {
std::println("Unable to create greeting.");
return 1;
}
std::println("{}", *greeting);
return 0;
}
module scanning を使う target-based CMake。
言語標準、module file set、dependency graph は target に保持します。従来型 header architecture を再現するために global compiler flags や project-wide include directories へ戻さないでください。
CMakeLists.txtcmake_minimum_required(VERSION 3.30)
project(GenyleapHello
VERSION 0.1.0
LANGUAGES CXX
)
add_library(genyleap_greeting)
target_compile_features(genyleap_greeting
PUBLIC
cxx_std_26
)
target_sources(genyleap_greeting
PUBLIC
FILE_SET CXX_MODULES
FILES
src/greeting/greeting.cppm
PRIVATE
src/greeting/greeting.cpp
)
set_property(
TARGET genyleap_greeting
PROPERTY CXX_SCAN_FOR_MODULES ON
)
add_executable(genyleap_hello
src/main.cpp
)
target_link_libraries(genyleap_hello
PRIVATE
genyleap_greeting
)
target_compile_features(genyleap_hello
PRIVATE
cxx_std_26
)
cmake -S . -B build/dev -G Ninja \
-DCMAKE_BUILD_TYPE=Debug
cmake --build build/dev --parallel
ctest --test-dir build/dev \
--output-on-failure \
--no-tests=error
プラットフォーム toolchain。
C++ アーキテクチャは移植可能に保ち、compiler、SDK、linker、packaging、runtime を明示的なプラットフォーム境界とします。compiler や target SDK を変更した後に設定済み CMake tree を再利用しないでください。
macOS.
Apple SDK とプラットフォームツールのために完全版 Xcode をインストールします。Apple Clang がデフォルトの native compiler で、より新しい compiler feature や diagnostics が必要な場合は upstream LLVM を併設できます。
sudo xcode-select --switch /Applications/Xcode.app
sudo xcodebuild -license accept
brew install cmake ninja llvm
xcrun clang++ --version
cmake --version
ninja --version
xcrun --show-sdk-path
Select upstream LLVM per build when needed; Apple SDK integration and upstream language experimentation are separate concerns.
Linux.
portability が重要なら GCC と Clang の両方を利用可能にします。compiler ごとに別の build directory を configure し、バージョン番号だけでなく実際の CMake/compiler/generator 組み合わせで module support を検証します。
sudo apt update
sudo apt install -y \
build-essential \
clang \
lld \
cmake \
ninja-build \
gdb
cmake -S . -B build/gcc -G Ninja \
-DCMAKE_CXX_COMPILER=g++
cmake -S . -B build/clang -G Ninja \
-DCMAKE_CXX_COMPILER=clang++
Windows.
Use Visual Studio Build Tools with the C++ workload for the Windows SDK and MSVC ABI. Add LLVM when you want clang-cl diagnostics while retaining MSVC-compatible platform integration.
winget install --id Microsoft.VisualStudio.2022.BuildTools `
--override "--wait --passive --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"
winget install --id Kitware.CMake
winget install --id Ninja-build.Ninja
winget install --id LLVM.LLVM
CMake を configure する前に MSVC compiler、linker、Windows SDK 環境を初期化する必要があります。
Android.
Use the Android NDK's Clang toolchain and CMake toolchain file rather than treating Android as a normal Linux desktop build. For projects that also use the current Qt baseline, the compatibility manifest currently resolves NDK r27c (27.2.12479018).
cmake -S . -B build/android-arm64 -G Ninja \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-28
cmake --build build/android-arm64 --parallel
Android 固有の JNI、lifecycle、storage、permission code は adapters の背後に置き、domain/application modules は通常の C++ のままにします。
iOS.
Use Xcode 16 or newer for the current Qt compatibility profile, and treat Apple frameworks, entitlements, signing and bundle resources as platform boundaries. Device and simulator builds are distinct targets even when they share application modules.
xcodebuild -version
xcrun --sdk iphoneos --show-sdk-path
xcrun --sdk iphonesimulator --show-sdk-path
xcrun --sdk iphoneos clang++ --version
WebAssembly.
Use Emscripten when the target runtime is the browser. For the current Qt compatibility profile, Emscripten 5.0.5 is pinned automatically from upstream Qt documentation; a standalone C++/WASM project may intentionally choose a newer SDK after its own verification.
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install 5.0.5./emsdk activate 5.0.5
source ./emsdk_env.sh
em++ --version
git clone https://github.com/emscripten-core/emsdk.git
Set-Location emsdk
.\emsdk.bat install 5.0.5.\emsdk.bat activate 5.0.5
.\emsdk_env.ps1
em++ --version
browser threading、SIMD、filesystem access、network behavior は単なる compiler switch ではなく runtime 制約です。target capabilities としてモデル化し、実際に提供する browser configuration で検証します。
Diagnostics は開発環境の一部です。
compile-time correctness は一層にすぎません。再現しにくい障害が出てから追加するのではなく、sanitizer と static-analysis build を最初から利用可能にします。
| ツール | 用途 | Recommendation |
|---|---|---|
ASan | Memory safety failures | Debug and CI where supported |
UBSan | Undefined behavior | Pair with ASan in a dedicated diagnostic build |
TSan | Data races | Separate build because instrumentation changes runtime behavior |
clang-tidy | Static analysis and modernization | Editor plus CI on changed production surfaces |
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
target_compile_options(genyleap_greeting PRIVATE
-fsanitize=address,undefined
-fno-omit-frame-pointer
)
target_link_options(genyleap_greeting PRIVATE
-fsanitize=address,undefined
)
endif()
Boost is part of the native foundation.
Use the C++ standard library first when it cleanly provides the required facility, then use Boost for capabilities that remain stronger or broader there. Keep Boost dependencies target-local, architecture-specific when compiled, and visible through modern CMake imported targets.
ユーザー向けアプリケーションでは Qt を意図的に選択します。
新しい Genyleap スタイルの GUI アプリでは、module-based C++ application/domain code の上に Qt Quick、QML、Qt Quick Controls を置く構成をデフォルトとします。UI は adapter であり、重複した business logic を持ちません。