Skip to content
PaskalCPP
Build and run

Building

How pcpp turns a main module into an executable or a library: what each kind builds, what the output folder holds, what each optimize level changes, how a target is built and run, and how libraries and the runtime are linked.

What a build makes

module exe builds an executable, module dll a shared library and module lib a static library. A unit is not built on its own: given to pcpp as the main module it is PR0004.

pcpp writes a C++23 header and source for each module and builds them with the bundled Zig toolchain (clang, -std=gnu++23) through zig build, which caches compiles. The toolchain folder is read from build.toml beside pcpp.exe (toolchain_path); the default is $P:res, the res folder beside pcpp.exe.

build.zig, the generated C++ and the built-in icon entry are written only when their bytes changed, so a program rebuilt with no change is not compiled again. A module renamed in case only (math to Math) gets its generated files renamed to the new case.

A successful build prints Build succeeded. and then Output: <path of the artifact>.

The output folder

The output folder is the -o option, else the @outputpath directive, else project.toml's output folder in a project build (see Projects), else output under the working folder. It holds:

PathWhat it is
build.zigthe build script pcpp writes
pcpp-output.jsonpcpp's record of the build: format, source, output folder, project
pcpp-installed.txtthe record of the files the build put in bin\ and lib\
pcpp-project\<triple>.txta project build's own record, one per target
pack\the packer pcpp pack built (a project's own output only)
generated\<Module>.pcpp.h, .pcpp.cppthe C++ written for each module
pcpp-diag\clang's diagnostics, one file per translation unit; store\ keeps them for cached builds (see Who reports a mistake)
.zig-cache\zig's cache
bin\an exe or a dll
lib\a lib (and a Windows dll's import library)
Kindx86_64-windows-gnux86_64-linux-gnu
exe<name>.exe<name>
dll<name>.dll (import library <name>.lib in lib\)lib<name>.so
lib<name>.liblib<name>.a
  • A Windows debug build writes the program's .pdb beside the exe or dll; a safe, fast or small build writes none, and neither does a Linux build.
  • Every file a build puts in bin\ and lib\ (the program or library, its .pdb, a dll's import library, the @copydll files) is recorded in pcpp-installed.txt in the output folder; each build writes the record afresh, listing only what that build installed. A plain build removes nothing: one output folder holds many programs' files.
  • A project build of its own output folder keeps its own record per target (pcpp-project\<triple>.txt) and removes a file that record lists and this build no longer produces (a .pdb after a switch from debug, the old exe after a rename, a @copydll dropped), with a Removing <name> status line. Another target's files, a name pcpp-installed.txt also lists and a @copydll whose source sits in that folder are never removed. When that cleanup cannot do its part (a file that cannot be removed, a record that cannot be read or written), the build goes on with the warning ZB0034.
  • An output folder longer than 198 characters is ZB0021 and nothing is built: zig launches its configure program from a folder nested under the output folder.

Optimize levels

With no @optimize (and no -opt) the build is debug.

LevelDefinesHeap report
debugPCPP_DEBUGyes
safe__OPTIMIZE__ (_FORTIFY_SOURCE undefined unless the program sets it with @cppdefine)no
fast__OPTIMIZE__, NDEBUGno
small__OPTIMIZE__, __OPTIMIZE_SIZE__, NDEBUGno

On Linux, fast and safe also define __NO_INLINE__ (unless the program sets it with @cppdefine): glibc then leaves out its own inline bodies of C library functions such as getc_unlocked, so a module may define those functions itself (as std HookIO does) at every level.

A debug build prints the runtime's heap report, [Heap] Allocs: <n>, Frees: <n>, Leaked: <n>, when the program ends. The level names are zig's own: build.zig writes the level as zig's optimize mode of the same name (small is std.lang.Optimize = .small).

build_debug.pcpp
module exe build_debug;

// What the C++ compiler sees at this optimize level
static const char* build_debug_flag()
{
#ifdef PCPP_DEBUG
  return "yes";
#else
  return "no";
#endif
}

begin
  println("PCPP_DEBUG: {}", build_debug_flag());
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
PCPP_DEBUG: yes
[Heap] Allocs: 0, Frees: 0, Leaked: 0

Targets and running

The same source builds for x86_64-windows-gnu and x86_64-linux-gnu, from Windows: @target in the source (see Target) or pcpp -t.

  • pcpp <file> -r builds the exe and runs it in its own folder; a Linux exe runs through WSL.
  • A program that exits with a code other than 0 under -r is ZB0031 (Program exited with code <n>.), and pcpp exits with that code.
  • -r on a dll or a lib is the warning CMP0002 (a dll cannot be run: auto-run skipped); the build itself succeeds.

Linking libraries

An exe links a PaskalCPP dll by name (@linklibrary) from the folder @addlibrarypath names (Windows: the import library in the dll's lib\; Linux: the .so in its bin\), declares the dll's routines external, and @copydll puts the library beside the exe so it is found at run time.

  • @copydll files are copied before the build.
  • A routine no linked library defines fails at the link as ZB0016 in the linker's words, reported at the .pcpp line that calls it.

Static libraries and the runtime

  • A lib whose code never calls into the PaskalCPP runtime ships alone, with no startup hook, and a C program links it with a plain C driver.
  • A lib that calls into the runtime (div, mod, a guard, raise, sets, ...) gets a startup hook, and its own build also builds the runtime as a second static library and installs it beside the lib in lib\ (pcpp_runtime.lib; on Linux libpcpp_runtime.a): one build.zig, one zig cache, with the lib's target and optimize level and none of the program's own defines.
  • So the lib cannot be named pcpp_runtime in any case (module lib PCPP_Runtime too): that is ZB0035, and nothing is built.
  • A PaskalCPP program that links such a lib brings its own runtime and never links pcpp_runtime; a raise inside the lib reaches the program's guard.
  • A C program built by another toolchain links the lib, then pcpp_runtime, through a C++ driver; without pcpp_runtime the link fails on pcpp::init.

Resource stamps

@exeicon, and @addverinfo on with the @vi* fields, are stamped on the built Windows binary after the build; where a stamp does not apply it is the warning SM0103 (see Values). A requested stamp that fails is an error (icon ZB0008, version info ZB0009), and nothing is published.

On Linux an exe's @exeicon is built in instead of stamped. At build time the compiler decodes the icon file's largest image (the most pixels, the first of equal ones) and builds it into the exe as the data entry pcpp/exeicon, read with the std unit ResData: width and height as little-endian 32-bit integers, then the pixels as 8-bit RGBA (not premultiplied), rows top to bottom. The icon is checked the same way on every target before anything is built (Windows decodes it before the build and stamps it after the link): an icon file that holds no image the compiler can decode is CMP0008 at the @exeicon path, and nothing is built.

When the build driver fails

The build driver runs zig for pcpp, and for any program that uses it through its API. A mistake in the source never reaches it: the compiler reports those first. What its ZB codes report is about files, processes and its own inputs; each message names the file and the reason.

CodeWhat failed
ZB0004, ZB0032zig.exe is not in the toolchain folder, or will not start
ZB0005a zig run that failed with no report of its own (Zig build failed with exit code <n>.)
ZB0017 (warning)build.toml cannot be read or written, or its toolchain_path is no usable path: the default toolchain path is used
ZB0003, ZB0015build.zig cannot be written, or read back (by --clean's check)
ZB0023 (warning)a line in build.zig that pcpp never writes; it is ignored
ZB0012, ZB0011 (warning)a file in pcpp-diag\ cannot be prepared before the run, or read after it
ZB0024, ZB0025, ZB0026, ZB0014, ZB0010@copydll: an empty name, two files onto one name, a file not found, a copy or publish that fails (every copy is checked before any is made)
ZB0029, ZB0030under -r, the exe is not where the build put it, or will not start
ZB0027, ZB0006 (warning)asked to run a dll or lib, or a target that is neither Windows nor Linux
ZB0013--clean cannot delete one of its targets
ZB0007 (warning)the manifest pcpp adds to every Windows exe cannot be added; the exe is kept
ZB0001, ZB0002, ZB0028, ZB0020, ZB0022, ZB0033, ZB0019 (warning)only a program using the API can cause these, since pcpp always sets them: no output folder, no sources, no project name, a bad name or path, an unknown target, a file the toolchain does not compile, a resource stamp where it does not apply