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.

Compile with Clang

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


clang++ -o hello.exe hello.cpp \
  -O3 \
  --sysroot=$HOME/toolchains/windows-msvc-sysroot \
  -fuse-ld=lld \
  -flto=thin \
  --target=x86_64-windows-msvc \
  -I$HOME/libraries/fast_io/include

Compile with GCC

GCC works the same way. Just add the include directory:


g++ -o hello hello.cpp \
  -O3 \
  -I$HOME/libraries/fast_io/include

Compile with MSVC (cl.exe)

In the Visual Studio Developer Command Prompt:


cl /EHsc /Ox hello.cpp ^
  /I %USERPROFILE%\\libraries\\fast_io\\include

This produces hello.exe using MSVC.

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++ -std=c++26 --precompile \
  $HOME/libraries/fast_io/share/fast_io/fast_io.cppm \
  -O3 \
  --sysroot=$HOME/toolchains/windows-msvc-sysroot \
  -fuse-ld=lld \
  -flto=thin \
  --target=x86_64-windows-msvc \
  -I$HOME/libraries/fast_io/include \
  -o fast_io.pcm

Then compile your program using the PCM:


clang++ -std=c++26 hello.cpp \
  -O3 \
  --sysroot=$HOME/toolchains/windows-msvc-sysroot \
  -fuse-ld=lld \
  -flto=thin \
  --target=x86_64-windows-msvc \
  -fmodule-file=fast_io=fast_io.pcm \
  -o hello.exe

Clang will now import the module instead of using headers.

Compile fast_io module with GCC

GCC’s module support is still experimental, but the process is similar:


g++ -std=c++26 -fmodules -c \
  $HOME/libraries/fast_io/share/fast_io/fast_io.cppm \
  -I$HOME/libraries/fast_io/include \
  -o fast_io.gcm -O3

g++ -std=c++26 -fmodules hello.cpp \
  fast_io.gcm \
  -I$HOME/libraries/fast_io/include \
  -o hello -s -flto -O3

Compile fast_io module with MSVC

MSVC supports modules via /interface and /module flags:


cl /std:c++latest /EHsc /GL /MD /Ox /interface \
  %USERPROFILE%\\libraries\\fast_io\\share\\fast_io\\fast_io.cppm \
  /I %USERPROFILE%\\libraries\\fast_io\\include \
  /Fo:fast_io.ifc

cl /std:c++latest /EHsc /GL /MD /Ox hello.cpp \
  /module:reference fast_io.ifc \
  /I %USERPROFILE%\\libraries\\fast_io\\include \
  /Fe:hello.exe

This builds and imports the fast_io module using MSVC.

Key takeaways