Ch1.8: fast_io

中文(简体)

Using fast_io (Header-Only)

In Ch1.5, we cloned the fast_io repository. fast_io is a header-only library, so you do not need to build anything. You simply add the include directory to your compiler flags.

Suppose you cloned fast_io into:

$HOME/libraries/fast_io

Then you can include it in your program:


#include <fast_io.h>

int main() {
    print("Hello from fast_io!\n");
}

And compile it by adding:

-I$HOME/libraries/fast_io/include

This tells the compiler where to find fast_io.h.

Two Required Flags: -fherbceptions -lherbceptions

fast_io reports every error as a herbception, and herbceptions are a compiler feature. So the include directory is not enough: you also need the herbceptions runtime, and a compiler that implements the language feature.

Only our GCC and LLVM builds support herbceptions. Stock compilers and MSVC do not — there is no way to compile fast_io with cl.exe. The full explanation, and every error-reporting rule, is in Ch3.16: Herbceptions.

Nothing else is needed: the herbceptions headers and library are part of the toolchain you installed, so -lherbceptions is the whole addition. Assume fast_io is cloned into $HOME/libraries/fast_io:


FASTIO=$HOME/libraries/fast_io

Compile with Clang

Example: building a Windows MSVC binary using Clang and the MSVC sysroot:


clang++ --config=$HOME/cfgs/x86_64-windows-msvc.cfg \
  -O3 -flto=thin -fherbceptions \
  -I$FASTIO/include \
  hello.cpp -lherbceptions -o hello.exe

The herbceptions runtime lives in the sysroot: lib/<triple>/herbceptions.lib when linking and bin/<triple>/herbceptions.dll when running (Wine finds it through WINEPATH). Our MinGW g++ reuses the same DLL with its own import library, and needs -lntdll because fast_io talks to NT directly:


x86_64-w64-mingw32-g++ -o hello.exe hello.cpp \
  -std=c++26 -O3 -s \
  -fherbceptions \
  -I$FASTIO/include \
  -lherbceptions -lntdll

wine ./hello.exe

To see which DLLs a Windows binary needs, use win-ldd:


wine $HOME/softwares/win-ldd/x86_64-windows-msvc/win-ldd.exe ./hello.exe
Notes on the flags: the MSVC sysroot already carries the Windows herbceptions runtime (herbceptions.lib to link, herbceptions.dll at run time), so --sysroot finds it and no -L is ever written. GCC/GNU-target builds additionally pass -s to strip the binary.

Compile with GCC

GCC works the same way, with our herbceptions GCC build:


g++ -o hello hello.cpp \
  -std=c++26 -O3 -s -fuse-ld=lld \
  -fherbceptions \
  -I$FASTIO/include \
  -lherbceptions

MSVC (cl.exe) Is Not Supported

MSVC is not a supported compiler for fast_io. It has no -fherbceptions, and fast_io reports every error through the herbception channel, so the tutorial's error contract cannot be expressed with it. Use our GCC or LLVM forks.

On Windows, use our Clang targeting x86_64-windows-msvc (exactly the command above — it produces a normal Windows binary, linked with lld) or our MinGW GCC. Neither cl.exe nor Visual Studio’s normal C++ toolset is involved.

Using fast_io Modules

fast_io also provides C++20 module files. They are located in:

{fast_io_cloned_dir}/share/fast_io

Example directory:


/Users/username/libraries/fast_io/share/fast_io

Contents:


fast_io.cppm
fast_io_crypto.cppm
fast_io_legacy.cppm
fast_io_inc/

How to import the module

Create a file hello.cpp:


import fast_io;

int main() {
    using namespace ::fast_io::io;
    print("Hello from fast_io module!\n");
}

Compile fast_io module with Clang

First, compile the module interface:


clang++ --config=$HOME/cfgs/x86_64-linux-gnu-libcxx.cfg \
  -O2 -fherbceptions -I$FASTIO/include \
  -x c++-module $FASTIO/share/fast_io/fast_io_json.cppm \
  -o fast_io.json.pcm --precompile

Then compile your program using the PCM:


clang++ --config=$HOME/cfgs/x86_64-linux-gnu-libcxx.cfg \
  -O2 -fherbceptions -I$FASTIO/include \
  -fmodule-file=fast_io.json=fast_io.json.pcm \
  hello.cpp fast_io.json.pcm -lherbceptions \
  -o hello

The module is precompiled once and imported afterwards; nothing from the headers is needed at the import site beyond #include <herbceptions/error> when you want to write a catch throws handler yourself.

Status of the module file names: fast_io_json.cppm precompiles and imports cleanly today, and is the one used in this example. The umbrella fast_io.cppm is still catching up with the headers, so if the precompile fails on it, use the header interface (#include <fast_io.h>) — that is what the rest of this tutorial uses.

Compile fast_io module with GCC

GCC’s module support is still experimental, and our herbceptions GCC build is known to crash (ICE) on some fast_io module units — compile the cluster with Clang, or use the header interface with GCC.


g++ -std=c++26 -fmodules -fherbceptions -O3 -s -fuse-ld=lld \
  -c $FASTIO/share/fast_io/fast_io_json.cppm \
  -I$FASTIO/include \
  -o fast_io.json.gcm

g++ -std=c++26 -fmodules -fherbceptions -O3 -s -fuse-ld=lld hello.cpp \
  fast_io.json.gcm \
  -I$FASTIO/include \
  -lherbceptions \
  -o hello

MSVC Cannot Compile the Module

MSVC is not a supported compiler for fast_io: it has no herbceptions, and every error in the library is reported as one. Our GCC and LLVM forks are the only compilers this tutorial supports. Windows binaries are built with our Clang targeting the MSVC ABI (or our MinGW GCC) — the resulting executables are ordinary Windows binaries, they just are not produced by cl.exe.

Key takeaways