| tcc_link | R Documentation |
Link a system library (like libsqlite3) and generate type-safe wrappers automatically using TinyCC JIT compilation (API mode). Unlike dlopen(), this uses TinyCC to compile bindings that handle type conversion between R and C automatically.
tcc_link(
path,
symbols,
headers = NULL,
libs = character(0),
lib_paths = character(0),
include_paths = character(0),
user_code = NULL,
verbose = FALSE
)
path |
Library short name (e.g., |
symbols |
Named list of symbol definitions with:
|
headers |
Optional C headers to include |
libs |
Library names to link (e.g., "sqlite3") |
lib_paths |
Additional library search paths |
include_paths |
Additional include search paths |
user_code |
Optional custom C code to include in the compilation |
verbose |
Print debug information |
A tcc_compiled object with callable functions
## Not run:
# Link SQLite with type-safe bindings
sqlite <- tcc_link(
"libsqlite3.so",
symbols = list(
sqlite3_libversion = list(args = list(), returns = "cstring"),
sqlite3_open = list(args = list("cstring", "ptr"), returns = "i32")
),
libs = "sqlite3"
)
# Call directly - type conversion happens automatically
sqlite$sqlite3_libversion()
# Example with custom user code for helper functions
math_with_helpers <- tcc_link(
"m",
symbols = list(
sqrt = list(args = list("f64"), returns = "f64"),
safe_sqrt = list(args = list("f64"), returns = "f64")
),
user_code = "
#include <math.h>
// Helper function that validates input before calling sqrt
double safe_sqrt(double x) {
if (x < 0) {
return NAN;
}
return sqrt(x);
}
",
libs = "m"
)
math_with_helpers$safe_sqrt(16.0)
math_with_helpers$safe_sqrt(-4.0) # Returns NaN for negative input
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.