R:C符号名称不在加载表中 - 与外部.c文件链接时出错

时间:2015-04-04 07:05:47

标签: c++ c r rcpp ode

我正在尝试使用 Rsundials 包来解决使用SUNDIALS的常微分方程(ODE)系统。首先,我试图在手册中运行该示例。

当在C文件中描述ODE的右侧时,可以使用Rsundials来解决ODE。

首先,在C文件(包中的示例程序)中描述了ODE

#include "include/nvector/nvector_serial.h"
#include "include/sundials/sundials_dense.h"
#define Ith(v,i) ( NV_DATA_S(v)[i - 1] )

int rhs(realtype t, N_Vector y, N_Vector ydot, void *f_data){
       realtype y1, y2, y3;
       y1 = Ith(y,1); y2 = Ith(y,2); y3 = Ith(y,3);
       /* Change values of ydot here using Ith(ydot,i) */

       double L = 49.3;
       double a21 = 0.011; double a12 = 0.012;
       double a31 = 0.0039; double a13 = 0.000035;
       double a01 = 0.021; double a02 = 0.016;
       Ith(ydot, 1) = -(a01 + a21 + a31)*y1 + a12*y2 + a13*y3 + L;
       Ith(ydot, 2) = a21*y1 - (a02 + a12)*y2;
       Ith(ydot, 3) = a31*y1 - a13*y3; 
       return(0);
}

保存在文件test_rsundials.c中。然后使用终端

中的以下命令编译该文件
R CMD SHLIB ./test_rsundials.c

文件编译时没有任何错误消息,我在生成的目录中看到test_rsundials.sotest_rsundials.o

然后我使用dyn.load加载包 -

dyn.load("/ .. path .. /test_rsundials.so")

上面的步骤中也没有错误信息,最后我使用以下命令调用cvode解算器(如包中的手册所示)

library(Rsundials)
vals  <- cvodes(c(0.0,0.0,0.0), seq(0,400,20),"test_rsundials","rhs",rtol=1e-4,atol=c(1e-8,1e-14,1e-6),verbose=T)

这会产生错误 -

Error in .Call("cvodes", PACKAGE = "Rsundials", as.double(y), as.double(times),  : 
  "cvodes" not available for .Call() for package "Rsundials"

我也尝试了以下命令

yvals  <- .Call("cvodes",package="Rsundials",c(0.0,0.0,0.0), seq(0,400,20),"test_rsundials","rhs",rtol=1e-4,atol=c(1e-8,1e-14,1e-6),verbose=T)

给了我错误

Error in .Call("cvodes", package = "Rsundials", c(0, 0, 0), seq(0, 400,  : 
  C symbol name "cvodes" not in load table

我不知道如何绕过它,或者它意味着什么。我将以下cvodes的代码粘贴参考

function (y, times, package, rhs, fndata = NULL, jacfunc = NULL, 
    rootfunc = NULL, numroots = 0, rtol = 1e-06, atol = 1e-06, 
    maxnumsteps = 500, maxstep = 0, verbose = FALSE, lasttime = FALSE) 
{
    if (!is.numeric(y)) 
        stop("Error: 'y' must be numeric")
    if (!is.numeric(times)) 
        stop("Error: 'times' must be numeric")
    if (!is.character(package)) 
        stop("Error: 'package' must be a character vector")
    if (!is.character(rhs)) 
        stop("Error: 'Right Hand Side function must be a character vector")
    if (!is.null(jacfunc) && !is.character(jacfunc)) 
        stop("Error: 'jacfunc' must be a character vector")
    if (!is.null(rootfunc) && !is.character(rootfunc)) 
        stop("Error: 'rootfunc' must be a character vector")
    if (!is.null(rootfunc) && numroots <= 0) 
        stop("Error: numroots must be greater than 0")
    if (!is.numeric(numroots)) 
        stop("Error: 'numroots' must be numeric")
    if (!is.numeric(rtol)) 
        stop("Error: 'rtol' must be numeric")
    if (!is.numeric(atol)) 
        stop("Error: 'atol' must be numeric")
    if (!is.numeric(maxstep)) 
        stop("Error: 'maxsteps' must be numeric")
    if (!is.null(fndata) && !is.numeric(fndata)) 
        stop("Error: Data arguments must be numeric")
    s = 1
    rhs = getNativeSymbolInfo(rhs, PACKAGE = package)$address
    jfunc = NULL
    if (!is.null(jacfunc)) 
        jfunc = getNativeSymbolInfo(jacfunc, PACKAGE = package)$address
    rofunc = NULL
    if (!is.null(rootfunc)) 
        rofunc = getNativeSymbolInfo(rootfunc, PACKAGE = package)$address
    solutions = .Call("cvodes", PACKAGE = "Rsundials", as.double(y), 
        as.double(times), rhs, as.double(fndata), jfunc, rofunc, 
        as.integer(numroots), as.integer(s), as.double(rtol), 
        as.double(atol), as.integer(maxnumsteps), as.integer(maxstep), 
        as.integer(verbose), as.integer(lasttime))
    if (lasttime == TRUE) 
        rows = 1
    else rows = length(times)
    solutions <- matrix(solutions, rows)
    col <- c()
    for (i in 1:length(y)) col[i] <- paste("y", i, sep = "")
    if (lasttime == TRUE) 
        dimnames(solutions) <- list(times[length(times)], col)
    else dimnames(solutions) <- list(times, col)
    solutions
}
<environment: namespace:Rsundials>

下面还粘贴了关于我的R

的详细信息
> version
               _                           
platform       x86_64-apple-darwin13.1.0   
arch           x86_64                      
os             darwin13.1.0                
system         x86_64, darwin13.1.0        
status                                     
major          3                           
minor          1.0                         
year           2014                        
month          04                          
day            10                          
svn rev        65387                       
language       R                           
version.string R version 3.1.0 (2014-04-10)
nickname       Spring Dance     

我读到这可能是64位v / s 32位编译问题,我不知道如何在64位标志中指定编译。我是R和C的新手,所以任何帮助都会非常感激!!

发布更新我读到此错误可能是由于链接库问题所致。我查看了这个包的src文件夹,并且有一个文件Makevars.old,这意味着我可能需要链接库

PKG_CFLAGS=-w

PKG_LIBS=-L. -lsundials_cvodes -lsundials_ida -lsundials_fida -lsundials_nvecserial

注意 - 软件包手册说&#34;安装SUNDIALS不是软件包的先决条件&#34;。所以我把所有文件都卡在src文件夹中这个包在我的项目文件夹中(所以希望没有找到文件错误)。

现在我尝试使用终端

中的以下命令进行编译
export PKG_CFLAGS="-w"    
export PKG_LIBS="-L. -lsundials_cvodes -lsundials_nvecserial"
R CMD SHLIB test_rsundials.c sundials_dense.c nvector_serial.c 

我现在收到以下错误

clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include   -w -fPIC  -Wall -mtune=core2 -g -O2  -c nvector_serial.c -o nvector_serial.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o nvector_serial.so nvector_serial.o sundials_dense.o test_rsundials.o -L. -lsundials_cvodes -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lsundials_cvodes
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nvector_serial.so] Error 1

虽然,我确实看到生成了三个新的.o个文件(test_rsundials.onvector_serial.osundials_dense.o

任何帮助都将不胜感激!!

0 个答案:

没有答案