C+
Systems · View as Markdown

Targets & cross-compilation

The same C+ source builds for more than the machine you are on. A --target flag selects where the code is headed, and the compiler emits that target's ABI and intrinsics, not the host's.

cpc build --target ios-arm64
cpc check --target android-arm64
cpc --emit-asm --target esp32-xtensa src/pid.cplus

--target works on build, check, and the --emit-ll / --emit-ll-opt / --emit-asm / --emit-obj emitters. Omitting it builds for host and reproduces the previous behavior byte for byte.

The named targets

Target What it is
host The machine you are building on (the default).
ios-arm64 iOS device (arm64).
ios-arm64-simulator iOS Simulator (arm64).
android-arm64 Android (aarch64-linux-android24), via the NDK.
esp32-xtensa Espressif ESP32 (32-bit Xtensa). See Embedded & ESP32.

An unknown name fails with the supported list. A TargetSpec (triple, pointer width, endianness, object format, ABI and intrinsic selectors) drives codegen's per-target choices, so HFA classification, struct size buckets, register pairs, byval, spin-loop hints, and NEON selection all resolve against the selected target. Host output is unchanged.

Who runs the final link

C+ emits objects; for the platforms with their own build systems, that build system owns the final link. cpc build of a [lib] staticlib writes the object, the archive, and a C header into target/<target-name>/<mode>/.

  • iOS stops at object emission — Xcode owns the link. clang gets -target <triple> and an -isysroot from xcrun when available. [[bin]] binaries, cdylib, cpc test, and single-file builds are rejected for the iOS targets, with the supported staticlib flow named in the message.
  • Android emits aarch64-linux-android24 ELF objects and staticlibs through the NDK's clang (resolved from $CPC_NDK_CLANG, $ANDROID_NDK_HOME and friends, or the SDK's newest ndk/), archived with the NDK's llvm-ar (the host ar cannot index ELF members). The NDK's clang must report LLVM 19+ (NDK r28.2+).

A cross build pins target triple = "<triple>" into the emitted IR, so a handed-off .ll carries its target. Bundled vendor artifacts resolve by the target's stable artifact triple (vendor/<dep>/src/lib/arm64-apple-ios/...); only host consults clang -print-target-triple. A host-vs-target triple mismatch is reported as E0862.

Verified end to end

These are not paper targets. A C+ staticlib linked by NDK clang has run on a Pixel 9 Pro XL emulator; a C+-driven UIKit screen renders on an iPhone 16 Pro simulator; and a #[realtime] controller runs closed-loop on a physical ESP32. The platform UI bindings are uikit for iOS, appkit for macOS, and espidf for the ESP32.