shlib: Compile shared library

Description Usage Arguments Value Author(s) Examples

Description

Compile a shared library using R CMD SHLIB. Options exist for converting the compiler output to R messages, warnings and errors.

Usage

1
2
3
4
shlib(filenames, verbose = TRUE, output = NULL, clean = FALSE,
  preclean = FALSE, dry_run = FALSE, debug = FALSE,
  stop_on_error = TRUE, warn_on_warning = TRUE, chdir = FALSE,
  use_colour = NULL)

Arguments

filenames

Vector of filenames

verbose

Be verbose (print compiler output to screen)

output

Name to use for the file output (passed to SHLIB with --output – by default the name of the first file is used with the extension changed to .Platform$dynlib.ext).

clean

Clean intermediate targets (passed to SHLIB as --clean)

preclean

Clean intermediate targets before running (passed to SHLIB as --clean)

dry_run

Don't run anything, but print commands (passed to SHLIB as --dry-run)

debug

Create a debug dll (windows only; passed to SHLIB as --debug)

stop_on_error

If an error in compilation is thrown, throw an error (TRUE by default). If set to FALSE, then the compilation output will be returned (the dll element will be set to NA).

warn_on_warning

If a the compiler returns a warning, issue an R warning (TRUE by default).

chdir

One of FALSE (the default) indicating that the working directory will not be changed, TRUE indicating that the working directory should be set to the directory containing source files, or a character string indicating the directory to set immediately prior to compilation. If TRUE, then all files listed in filenames must be in the same directory.

use_colour

Use ANSI escape colours (via the crayon package) if on a ANSI compatibile terminal (does not support Rstudio, Rgui or Rterm). If NULL (the default) then colour will be used where support is detected by crayon::has_color()

Value

Invisibly, A list with four elements: success - a logical indicating if the command was successful (this can only be FALSE if stop_on_error is FALSE), output - an object of class compiler_output containing compiler output, dll - the path to the created shared library, or NA_character_ if the command failed, and base - the dll name without leading path or extension (suitable to use as the PACKAGE argument to .Call). If the command changed directory then the normalized (and therefore absolute) path to the dll is returned.

Author(s)

Rich FitzJohn

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# A small piece of C code saved as add.c within a temporary directory:
txt <- c("#include <R.h>",
        "#include <Rinternals.h>",
        "SEXP add2(SEXP a, SEXP b) {",
        "  return ScalarReal(REAL(a)[0] + REAL(b)[0]);",
        "}")
path <- tempfile()
dir.create(path)
filename <- file.path(path, "add.c")
writeLines(txt, filename)

# Compile the object:
res <- rcmdshlib::shlib(filename)
res

# Load it into R:
dyn.load(res$dll)

# And call it
.Call("add2", pi, 1, PACKAGE = res$base)
# Cleanup
dyn.unload(res$dll)
unlink(path, recursive = TRUE)

richfitz/rcmdshlib documentation built on May 27, 2019, 8:25 a.m.