简体   繁体   English

在Mac OS X上将GNU Scientific Library链接到R.

[英]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. 我一直在使用C语言编写的程序上工作一段时间,该程序使用GSL中的函数运行MCMC。 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. 我已经阅读了很多GNU文档和编写R扩展,并且我已经阅读了很多关于使用RcppGSL的内容,但在我看来,在C中编写更容易,然后将函数动态加载到R.我已经看到了许多来源描述了如何在Windows上构建函数,但我在如何在基于Unix的系统上使用它的所有内容都是它“相对简单”和“简单”但是我无法使它工作。

The C script I am dying for someone to get to work is an easy one. 我渴望上班的C剧本很容易。 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). 它只是采用一个具有给定行数和列数的数组,将其转换为gsl_matrix,然后将其转换回数组(这基本上是我的程序所做的,除了整个更改条目的内容)。 The C script is C脚本是

#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. 我所做的是确保我的终端(Mac OS X)和R共享与simple.c保存到的目录相同的目录。 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. 进入我的终端创建相应的simple.so文件,也保存到桌面。 Then in R i can do 然后在R我能做到

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. 我对此错误感到困惑,因为好像编译器识别出make文件< gsl/gsl_linalg.h >所以我认为它不能识别该函数的原因是因为R没有连接到库,但我有不知道如何解决这个问题。

At this point, if the functions were all recognizable, I could then perform the function in R 此时,如果函数都是可识别的,那么我可以在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 如果我的函数动态正确加载它会工作,我会回到X完全放入我作为.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. 任何洞察都会非常有用,无论是否有人将GSL库成功链接到Mac OS X上的R可能会给我留言或评论会非常感激。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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