Register a global C symbol so the compiled object exposes getter/setter
functions global_<name>_get() and global_<name>_set().
Details
Globals are limited to scalar FFI types. Array types are rejected. Scalar conversions follow the same rules as wrapper arguments:
Integer inputs (
i8,i16,i32,u8,u16) must be finite and within range;NAvalues error.Large integer types (
i64,u32,u64) are mediated through R numeric (double). Values must be integer-valued and within range; fori64/u64only exact integers up to $2^53$ are accepted.Getter wrappers for
i64/u64warn when the stored value exceeds R's exact integer range for numeric vectors.boolrejectsNAlogicals.
Ownership notes:
ptrglobals store the raw address from an external pointer. If the external pointer owns memory, keep it alive; otherwise the pointer may be freed while the global still points to it.cstringglobals are rejected because a global would outlive the borrowed R string view. Declare the global asptrand manage its storage explicitly with an owned object such astcc_cstring().
Note
Global helpers are generated inside the compiled TCC unit. Recompiling creates a new instance of the global variable; existing compiled objects continue to refer to their own copy.
Examples
ffi <- tcc_ffi() |>
tcc_source("int global_counter = 7;") |>
tcc_global("global_counter", "i32") |>
tcc_compile()
ffi$global_global_counter_get()
#> [1] 7