$plugindir/nbdkit-cc-plugin.so
The plugin.
Use "nbdkit --dump-config" to find the location of $plugindir.
Write small nbdkit plugins in inline c (and other languages)
nbdkit cc /path/to/plugin.c [CC=<CC>] [CFLAGS=<CFLAGS>][EXTRA_CFLAGS=<EXTRA_CFLAGS>]
nbdkit cc - <<'EOF'... C code ...EOF
This plugin allows you to write small nbdkit(1) plugins in C (and some other languages). When you use this plugin it compiles your source code to a temporary plugin and then jumps into your compiled plugin. It is somewhat similar to nbdkit-sh-plugin(3), except for C source code. This can also be used to write plugins which are "C scripts".
Note this is not the way you normally write nbdkit plugins in C. To understand how to write plugins in C normally, read nbdkit-plugin(3).
Simple plugins from the nbdkit source tree can be compiled and run directly using commands such as:
$ nbdkit cc plugins/example1/example1.c EXTRA_CFLAGS="-I. -Iinclude"
You can also read the source from stdin using "-":
$ nbdkit cc - EXTRA_CFLAGS="-I. -Iinclude" \< plugins/example1/example1.c
To replace the compiler flags:
$ nbdkit cc plugins/example1/example1.c \CFLAGS="-O3 -mavx2 -fPIC -shared"
The plugin parameters "CC", "CFLAGS" and "EXTRA_CFLAGS" (written in uppercase) can be used to control which C compiler and C compiler flags are used. If not set, the default compiler and flags from when nbdkit was itself compiled from source are used. To see what those were you can do:
$ nbdkit cc --dump-plugin...CC=gccCFLAGS=-g -O2 -fPIC -shared
The "CFLAGS" parameter overrides the built-in flags completely. The "EXTRA_CFLAGS" parameter adds extra flags to the built-in flags.
Plugins compiled this way must use the same API version as the cc plugin itself uses. Currently this is "NBDKIT_API_VERSION=2".
You can create a C plugin which is a self-contained script by adding the following lines at the top and ensuring the C source is executable ("chmod +x plugin.c"):
#if 0exec nbdkit cc "$0" "$@"#endif
The script can be run as a command with additional nbdkit flags and plugin parameters, eg:
./plugin.c -f -v./plugin.c -p 10000 --filter=cow./plugin.c param=1
nbdkit cc CC=g++ source.cpp
C++ plugin scripts can be created similarly to C, but you must add "CC=g++" as a parameter to exec nbdkit.
nbdkit cc CC=ocamlopt \CFLAGS="-output-obj -runtime-variant _pic NBDKit.cmx -cclib -lnbdkitocaml" \source.ml
OCaml plugin scripts can be created using this trick:
(*/.)>/dev/null 2>&1exec nbdkit cc "$0" \CC=ocamlopt \CFLAGS="-output-obj -runtime-variant _pic NBDKit.cmx -cclib -lnbdkitocaml" \"$@"*)(* followed by OCaml code for the plugin here *)
As with C plugin scripts, the file must be executable. See also nbdkit-ocaml-plugin(3).
This plugin can be used with most ahead-of-time compiled programming languages if they can create shared objects (.so files). The only requirement is that the compiler ("CC") supports an -o option to write a shared object.
The script name, or "-", must appear as the first parameter.
EXTRA_CFLAGS="EXTRA_CFLAGS"Override the compiler and flags. See "Compiler name and flags" above.
All other parameters on the command line are passed to the plugin.
$ nbdkit cc - <<'EOF'#include <stdint.h>#include <string.h>#define NBDKIT_API_VERSION 2#include <nbdkit-plugin.h>char data[10*1024*1024];#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLELstatic void *my_open (int readonly){return NBDKIT_HANDLE_NOT_NEEDED;}static int64_tmy_get_size (void *handle){return (int64_t) sizeof (data);}static intmy_pread (void *handle, void *buf,uint32_t count, uint64_t offset,uint32_t flags){memcpy (buf, data+offset, count);return 0;}static intmy_pwrite (void *handle, const void *buf,uint32_t count, uint64_t offset,uint32_t flags){memcpy (data+offset, buf, count);return 0;}static struct nbdkit_plugin plugin = {.name = "myplugin",.open = my_open,.get_size = my_get_size,.pread = my_pread,.pwrite = my_pwrite,};NBDKIT_REGISTER_PLUGIN(plugin)EOF
$plugindir/nbdkit-cc-plugin.so
The plugin.
Use "nbdkit --dump-config" to find the location of $plugindir.
"nbdkit-cc-plugin" first appeared in nbdkit 1.22.
3 releases since Debian 11 (Bullseye); the text changed 2 times.