Boost Libraries for C++: Asio, Filesystem, Regex, DateTime, Program_options, and CMake
이 글의 핵심
Practical Boost: installation paths, header-only vs compiled libs, Asio async echo sample, regex and filesystem examples, CLI parsing, Beast mention, and Boost vs std guidance.
Introduction: when the standard library is not enough
Boost fills gaps: Asio for async I/O, Regex when std::regex performance varies, DateTime for calendar math beyond chrono, Program_options for CLI parsing, and more. Many facilities later became std (optional, filesystem, etc.)—prefer std when it matches your standard level.
flowchart LR
subgraph stdlib["Standard library"]
S1[vector, string]
S2[optional C++17]
S3[filesystem C++17]
end
subgraph boost["Boost"]
B1[Asio]
B2[Beast]
B3[Regex]
B4[DateTime]
B5[Program_options]
end
stdlib -->|gaps| boost
boost -->|standardization source| stdlib
Requirements: C++14+, Boost 1.70+ typical for Asio/Beast samples.
Installation
- apt (
libboost-all-devor component-devpackages): quick but distro-pinned versions. - vcpkg:
./vcpkg install boost-asio boost-filesystem ...+ CMake toolchain—reproducible. - Source
./b2: maximum control, longer builds.
Header-only vs link
Many headers need no .a; Filesystem, Regex, Date_time, Program_options need linking boost_* and usually boost_system for Asio error categories.
Boost.Asio (timer and echo sketch)
io_context::run drives completion handlers; chain async_accept → async_read_until → async_write for TCP echo. Always re-arm async_accept or use work_guard` when you need the loop to stay alive.
Filesystem
boost::filesystem parallels C++17 std::filesystem—prefer std on C++17+ for new code; Boost remains useful for older toolchains.
Regex
Precompile boost::regex for hot paths; watch catastrophic backtracking on nested quantifiers.
DateTime
Gregorian date, date_duration, posix_time for calendar operations chrono does not express as conveniently.
Program_options
Declare options with options_description, parse_command_line, parse_config_file, and variables_map.
CMake
find_package(Boost 1.70 REQUIRED COMPONENTS filesystem regex date_time program_options system)
target_link_libraries(app PRIVATE Boost::filesystem Boost::regex ...)
Asio often still links Boost::system for error_code.
Common errors
- undefined reference to boost::… — add missing libraries; order dependent after dependee for raw
g++lines. - Version mismatch — one install path (apt XOR vcpkg XOR custom prefix).
- pthread on Linux —
-pthreadwith Asio samples. - bad_any_cast in Program_options — check
vm.countbeforeas<T>. - Asio run() exits early — re-register accept or use work guard.
Boost vs std (summary)
| Feature | Prefer std | Boost when |
|---|---|---|
| optional/variant | C++17 | Pre-C++14 code |
| filesystem | C++17 | Legacy |
| regex | Test first | Heavy matching, portable perf |
| Async I/O | N/A | Asio |
| CLI parsing | N/A | Program_options |
Related posts
- JSON #27-2
- Asio intro #29-1
- Chat server #31-1
Next: nlohmann/json #27-2
Previous: Compile-time reflection #26-3