使用R代码注意C函数名称

Ale*_*lex 4 c r

所以这里有点疯狂.

如果您有一些由R函数调用的C代码(作为共享对象),请尝试将其添加到代码中

void warn() {

 int i; // just so the function has some work, but you could make it empty to, or do other stuff

}
Run Code Online (Sandbox Code Playgroud)

如果您随后调用warn()R函数调用的C代码中的任何位置,则会出现段错误;

  *** caught segfault ***
 address 0xa, cause 'memory not mapped'
Traceback:
  1: .C("C_function_called_by_R", as.double(L), as.double(G), as.double(T), as.integer(nrow),     as.integer(ncolL), as.integer(ncolG), as.integer(ncolT),     as.integer(trios), as.integer(seed), as.double(pval), as.double(pval1),     as.double(pval2), as.double(pval3), as.double(pval4), as.integer(ntest),     as.integer(maxit), as.integer(threads), as.integer(quietly))
  2: package_name::R_function(L, G, T, trios)
  3: func()
  4: system.time(func())
  5: doTryCatch(return(expr), name, parentenv, handler)
  6: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  7: tryCatchList(expr, classes, parentenv, handlers)
  8: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        msg <- conditionMessage(e)        sm <- strsplit(msg, "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste(prefix, "\n  ", sep = "")    }    else prefix <- "Error : "    msg <- paste(prefix, conditionMessage(e), "\n", sep = "")    .Internal(seterrmessage(msg[1L]))    if (!silent && identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = stderr())        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
  9: try(system.time(func()))
 10: .executeTestCase(funcName, envir = sandbox, setUpFunc = .setUp,     tearDownFunc = .tearDown)
 11: .sourceTestFile(testFile, testSuite$testFuncRegexp)
 12: runTestSuite(testSuite)
 aborting ...
 Segmentation fault (core dumped)
 (END)
Run Code Online (Sandbox Code Playgroud)

不用说,如果从C或C++包装器而不是从R函数调用相同的函数,代码运行正常.如果你重命名warn()它也可以正常工作.

有任何想法吗?这是受保护的名称/符号吗?有这样的名字列表吗?我在Ubuntu 12.01(i686-pc-linux-gnu(32位))上使用R版本2.14.1.C代码使用GNU GCC 4.6.3编译.

Mar*_*gan 5

这似乎是一个非常有趣的问题.这是我的最小例子,在test.c我的文件中

void warn() {}
void my_fun() { warn(); }
Run Code Online (Sandbox Code Playgroud)

我编译然后运行

$ R CMD SHLIB test.c
$ R -e "dyn.load('test.so'); .C('my_fun')"
Run Code Online (Sandbox Code Playgroud)

使用我的linux gcc版本4.6.3.,R输出是

> dyn.load('test.so'); .C('my_fun')
R: Success
list()
Run Code Online (Sandbox Code Playgroud)

使用"R:Success"来自warnlibc中定义的函数(参见man warn,在err.h中定义).当然,R会加载几个动态库,然后按照指示加载test.so.当my_fun被调用,动态链接器解析warn,但分辨率的规则是搜索全球warn象征,而不仅仅是test.so. 我真的不知道全局搜索规则是什么,也许是按照.so打开的顺序,但无论如何分辨率都不是我期待的.

什么是要做?指定

static void warn() {}
Run Code Online (Sandbox Code Playgroud)

在编译时强制解析,当创建.o时,从而避免问题.例如,如果warn在一个文件(utilities.c)和my_fun另一个文件中定义,则这不起作用.在Linux上dlopen(用于加载共享对象的函数)可以提供一个标志RTLD_DEEPBIND,该标志在全局之前在本地进行符号解析,但是(a)R不使用dlopen那种方式,(b)有几个(参见第9页)保留用这种方法.因此,据我所知,最佳做法是尽可能使用static,并仔细命名函数以避免名称冲突.后者并不像看起来那么糟糕,因为R加载包共享对象,使得包符号本身不会被添加到全局名称空间(请参阅?dyn.loadlocal参数,并注意特定于操作系统的警告).

我有兴趣听到更强大的"最佳实践".