R: C symbol name not in load table - error in linking with external .c files

半城伤御伤魂 提交于 2019-12-22 18:33:52

问题


I am trying to use the Rsundials package to solve a system of Ordinary Differential Equations (ODEs) using SUNDIALS. First, I am trying to run the example in the manual.

Rsundials can be used to solve the ODEs when the right hand side of the ODEs is described in a C file.

First, the ODEs are described in a C file (example program from the package)

#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);
}

saved in a file test_rsundials.c. The file is then compiled using the following command in the terminal

R CMD SHLIB ./test_rsundials.c

The file compiles without any error message and I see test_rsundials.so and test_rsundials.o in my directory being generated.

Then I use the dyn.load to load the package -

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

No error message in the step above also, finally I call the cvode solver using the following commands (as shown in the manual of the package)

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)

This generates the error -

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

I also tried the following command

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)

which gives me the error

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

I am not sure how can I get around it, or what it means. I am pasting the code from cvodes below for reference

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>

Also pasted below are details about my 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     

I read that this might be 64-bit v/s 32-bit compilation issue, I am not sure how I can specify compilation in 64-bit flag. I am a newbie at both R and C, so any help will be much appreciated!!

Posting an update -- I read that this error might be due to linking with library issues. I looked at the src folder of this package, and the there is a file Makevars.old which implies that I might need to link with the libraries

PKG_CFLAGS=-w

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

NOTE - The package manual says that "Installation of SUNDIALS is not a prerequisite for the package". So I have stuck all the files in src folder of this package in my project folder (so hopefully there are no file not found errors).

Now I tried to compile with the following commands in the terminal

export PKG_CFLAGS="-w"    
export PKG_LIBS="-L. -lsundials_cvodes -lsundials_nvecserial"
R CMD SHLIB test_rsundials.c sundials_dense.c nvector_serial.c 

I get the following error now

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

Although, I do see three new .o files (test_rsundials.o, nvector_serial.o and sundials_dense.o) are generated

Any help will be much appreciated!!

来源:https://stackoverflow.com/questions/29443730/r-c-symbol-name-not-in-load-table-error-in-linking-with-external-c-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!