gpt4 book ai didi

c++ - 在 Rcpp 中包装一个 Fortran 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:08 27 4
gpt4 key购买 nike

这最初在我看来是一项简单的任务,但我无法完成以下工作。我试图将一个 fortran 子例程包装到一个 Rcpp 调用中,以便在 R 中使用该函数。目标是将该函数合并到一个包中,因此仅在特定 *.so 文件上使用 dyn.load() 的想法是不可行的(除非有人可以告诉我怎么做?)。通过阅读类似的帖子,我怀疑在 makevars 文件中指定标志可能会解决问题,但提供的信息非常简洁 here并真诚地感谢一些澄清。

我已尽可能接近文档完成以下操作。

  1. 使用 Rcpp.package.skeleton 创建包结构
  2. 将我的 fortran 文件 (hello.f) 放在 src 目录中
  3. 为 Rcpp 包装器 (hello.cpp) 创建了一个基本的 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.

我的文件如下:

你好.f

subroutine hello()
print *, "hello world"
end subroutine hello

你好.h

extern "C"
{
void 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");
}

最佳答案

我相信这个问题很简单,就像当您包含名称名称具有不同扩展名的多个文件时 R 不高兴一样。尝试将 hello.f 重命名为 hello_fortran.f 并从那里开始。

关于c++ - 在 Rcpp 中包装一个 Fortran 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com