Skip to content

C++

C++ uses clang++, which also links libc++ and libc++abi:

clang++ --target=i8086-pc-msdos --sysroot=<sysroot> -Os prog.cpp -o prog.exe

C++ programs build with -fno-exceptions -fno-rtti by default. libc++ has iostreams, string and file streams, the "C" locale and its facets, wide characters and <filesystem>, but no threads. The linker drops the functions, vtables and other COMDATs that nothing uses (clang passes option eliminate), so a program only pays for the parts of libc++ it uses: in the small model at -Os, the C++ test is about 32 KB of code, and a hello world that writes to std::cout about 27 KB (a 36 KB .COM in the tiny model). For that, on i8086 the "C" locale makes each facet the first time a program asks for it, and the wide streams (std::wcout and the others) are linked only when a program uses one. Programs that use most of iostreams (file and string streams, and reading and writing numbers of each type, floating point among them), <filesystem> or the demangler are more than the 64 KiB of code that the tiny, small and compact models have, and need a model with far code. Reading numbers from std::cin and string streams fits in the small model with optimization; tiny has 64 KiB for code and data together, and far pointers make code in the compact model about a third larger.

<filesystem> takes DOS paths: C:\DIR\FILE.TXT, with a drive letter as the root name and \ as the preferred separator; / works too, and generic_string() uses it. There are no links or POSIX permissions: permissions sets or clears the read-only attribute from the owner write bit, and last_write_time has the 2-second resolution of DOS.

libc++ is built for every model. With -frtti, dynamic_cast and typeid work too. In the models with far code or far data, a vtable entry is 32 bits, and a member function pointer holds a far code pointer where code is far. In the far-data models, vtables and type_info objects are far data too, outside DGROUP.

C++ exceptions are off by default. -fexceptions turns them on, and clang then links the libc++, libc++abi and unwinder built with them, from lib/<model>/eh in the sysroot. They work in every model. Exceptions unwind with setjmp/longjmp-style (SjLj) tables: each function with a try or with destructors to run registers a context on entry and unregisters it on return, so code that throws nothing still pays a few instructions per call, but there are no unwind tables to keep in memory. A throw needs RTTI, which the eh libraries have; your own code needs -frtti only for dynamic_cast and typeid. There is no forced unwind (_Unwind_ForcedUnwind), and an exception cannot leave a Windows DLL: each DLL keeps its own list of contexts.

abi::__cxa_demangle is in the medium, large, huge and huge16 builds of libc++abi. Its code is over 64 KiB with far data pointers, and close to it with near ones; with far code the compiler spreads a module's code over several code segments (<module>_TEXT, <module>_TEXT_1, ...), so it fits. The tiny, small and compact models have one code segment for the whole program and go without it. Its parser takes about 5 KiB of stack, more than the 4 KiB default: link such a program with -Wl,option,stack=8192.