这起初对我来说是一个简单的任务,但我无法使以下内容正常工作。我正在尝试将Fortran子程序包装成Rcpp调用,以便在R中使用该函数。目标是将该函数合并到软件包中,因此仅使用dyn.load()在特定*.so文件上不可行(除非有人能向我展示如何?)。从阅读类似的帖子,我怀疑在makevars文件中指定标志可以解决问题,但提供的信息非常简洁,在此链接中,需要进一步澄清。
我已经尽可能接近文档进行了以下操作:
1. 使用Rcpp.package.skeleton创建软件包结构 2. 将我的Fortran文件(hello.f)放置在src目录中 3. 为Rcpp包装器创建了一个基本的cpp文件(hello.cpp) 4. 创建一个R文件来创建“干净”的函数调用(即避免用户使用.Call,并允许在.Call之前运行其他内部计算)
然而,当我尝试构建我的软件包(使用RStudio)时,我会得到以下错误输出:
我的文件如下所示: hello.f
我已经尽可能接近文档进行了以下操作:
1. 使用Rcpp.package.skeleton创建软件包结构 2. 将我的Fortran文件(hello.f)放置在src目录中 3. 为Rcpp包装器创建了一个基本的cpp文件(hello.cpp) 4. 创建一个R文件来创建“干净”的函数调用(即避免用户使用.Call,并允许在.Call之前运行其他内部计算)
然而,当我尝试构建我的软件包(使用RStudio)时,我会得到以下错误输出:
==> R CMD INSTALL --no-multiarch --with-keep.source fortran
g++ -shared -o fortran.so RcppExports.o hello.o hello.o rcpp_hello_world.o -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
* installing to library ‘/home/.../R/x86_64-pc-linux-gnu-library/3.0’
* installing *source* package ‘fortran’ ...
** libs
hello.o: In function `hello_wrapper':
/home/.../r_code/fortran/src/hello.cpp:16: multiple definition of `hello_wrapper'
hello.o:/home/.../r_code/fortran/src/hello.cpp:16: first defined here
collect2: error: ld returned 1 exit status
make: *** [fortran.so] Error 1
ERROR: compilation failed for package ‘fortran’
* removing ‘/home/.../R/x86_64-pc-linux-gnu-library/3.0/fortran’
Exited with status 1.
我的文件如下所示: hello.f
subroutine hello()
print *, "hello world"
end subroutine hello
hello.h
extern "C"
{
void hello();
}
hello.cpp
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include "hello.h"
#ifdef __cplusplus
extern "C"
{
SEXP hello_wrapper();
}
#endif
SEXP
hello_wrapper ()
{
hello();
}
wrapper.R
hello_r <- function(){
.Call("hello_wrapper");
}
hello.f
重命名为hello_fortran.f
,将hello.cpp
重命名为hello_cpp.cpp
后,这使我更接近了,但现在出现了另一个错误:dyn.load... unable to load shared object /home/.../fortran.so: undefined symbol: hello
。 - cdeterman