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.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 store a borrowed pointer to R's string data (UTF-8 translation). Do not free it; for C-owned strings prefer aptrglobal and manage lifetime explicitly (e.g., withtcc_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