简体   繁体   中英

Linking the GNU Scientific Library to R on Mac OS X

I've been working for a while on a program I am writing in C that runs an MCMC using functions from the GSL. I've read a lot of the GNU documentation and the writing R extensions, and I've read plenty about using RcppGSL, but it seems to me to be easier to write in C and then dynamic load the function into R. I have seen many sources describing how to build the function on Windows, but everything I see on how to use it on Unix based systems is that it is "relatively straightforward" and "simple" however I can not get it to work.

The C script I am dying for someone to get to work is an easy one. It's simply to take an array with a given number of rows and columns, turn it into a gsl_matrix, then turn it back into an array (This is essentially what my program does anyway except for the whole changing what the entries are). The C script is

#include <R.h>
#include <stdio.h>
#include <gsl/gsl_linalg.h>

void simple( int *n, int *rows, int *cols)
{
  int r,c;
  int Cols = *cols; //This step, and the step below it are unnecessary except for readability
  int Rows = *rows;
  gsl_matrix * m = gsl_matrix_alloc (Rows, Cols); // Declares a gsl_matrix m of size Rows x Cols

  for( r = 0; r< Rows; r++)
    for (c=0; c< Cols; c++)
      gsl_matrix_set(m,r,c, *(n+sizeof(int)*(Cols*r+c))); // The array is organized by rows, sets matrix values

  for( r=0; r< Rows; r++)
    for (c=0; c < Cols; c++)
      *(n+sizeof(int)*(Cols*r+c))=gsl_matrix_get(m,r,c); // This return matrix values to the array (should be the same as before)
}

What I've done is made sure that my terminal (Mac OS X) and R share the same directory that simple.c is saved to. I compile the above typing

R CMD SHLIB simple.c

into my terminal to create the corresponding simple.so file, also saved to the desktop. Then in R i can do

dyn.load("simple.so")

This is where I receive the error

Symbol not found: _gsl_matrix_alloc 
Expected in: flat namespace

I'm confused by this error because it seems as though the compiler recognizes the make file < gsl/gsl_linalg.h > so I assume the reason it doesn't recognize the function is because R is not connected to the library, but I have no idea how to resolve this.

At this point, if the functions were all recognizable, I could then perform the function in R

x=.C("simple", c(as.integer(c(1,4,7,2,5,8,3,6,9)),as.integer(3),as.integer(3)))

and if my function dynamically loaded correctly it would work, and I would get back for X exactly what I put in as the second input for .C

Any insight would be extremely helpful, whether anyone has succesfully linked the GSL library to R on Mac OS X could message me or comment would be much appreciated. The only thing I can ever find on help forums is that it is "straightforward" or "relatively simple" but I have no idea what to do! Please help!

您需要指定gsl函数的位置:

R CMD SHLIB simple.c -lgsl -lgslcblas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM