使用Visual Studio构建R包(C API)

时间:2021-10-25 23:10:10

I'm trying to build a simple R package with Visual Studio, here is my code:

我正在尝试用Visual Studio构建一个简单的R包,以下是我的代码:

#include <R.h>
#include <Rinternals.h>

SEXP add(SEXP a, SEXP b) {
  SEXP result = PROTECT(allocVector(REALSXP, 1));
  REAL(result)[0] = asReal(a) + asReal(b);
  UNPROTECT(1);

  return result;
}

I have R runtime and RTools installed.

我安装了R运行时和RTools。

When I try to compile it, I get the following link error:

当我试图编译它时,我得到以下链接错误:

error LNK2019: unresolved external symbol REAL referenced in function "struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)" (?add@@YAPEAUSEXPREC@@PEAU1@0@Z)
error LNK2019: unresolved external symbol Rf_asReal referenced in function "struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)" (?add@@YAPEAUSEXPREC@@PEAU1@0@Z)
error LNK2019: unresolved external symbol Rf_allocVector referenced in function "struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)" (?add@@YAPEAUSEXPREC@@PEAU1@0@Z)
error LNK2019: unresolved external symbol Rf_protect referenced in function "struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)" (?add@@YAPEAUSEXPREC@@PEAU1@0@Z)
error LNK2019: unresolved external symbol Rf_unprotect referenced in function "struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)" (?add@@YAPEAUSEXPREC@@PEAU1@0@Z)

Well, I guess that i'm missing some binaries that are required for the linking process. The problem is that I have no idea where can I find the necessary .lib files. Inside the R runtime installation folder I can find the include directory, but I cannot find any lib directory. What am I missing?

我想我漏掉了一些链接过程所需要的二进制文件。问题是我不知道在哪里可以找到必要的.lib文件。在R运行时安装文件夹中,我可以找到包含目录,但是我找不到任何lib目录。我缺少什么?

Thanks!

谢谢!

1 个解决方案

#1


3  

Allow me to quote from the Rcpp FAQ vignette:

请允许我引用Rcpp常见问题:

Can I use Rcpp with Visual Studio ?

我可以在Visual Studio中使用Rcpp吗?

Not a chance.

不是一个机会。

And that is not because we are meanies but because R and Visual Studio simply do not get along. As Rcpp is all about extending R with C++ interfaces, we are bound by the available toolchain. And R simply does not compile with Visual Studio. Go complain to its vendor if you are still upset.

这并不是因为我们是卑鄙小人,而是因为R和Visual Studio不能和睦相处。由于Rcpp都是关于使用c++接口扩展R的,所以我们受到可用工具链的约束。而R仅仅是不使用Visual Studio编译。如果你还不高兴,就去找供应商投诉。

Microsoft more or less went out of its way to ensure what its OS and tooling were not POSIX compliant. As R grew up in a Unix / POSIX world there simply is a gap you cannot bridge (easily).

微软或多或少都在努力确保其操作系统和工具不兼容POSIX。随着R在Unix / POSIX世界中长大,存在着一个不能轻易跨越的鸿沟。

So on Windows the MinGW port of gcc it is.

所以在Windows上就是gcc的MinGW端口。

#1


3  

Allow me to quote from the Rcpp FAQ vignette:

请允许我引用Rcpp常见问题:

Can I use Rcpp with Visual Studio ?

我可以在Visual Studio中使用Rcpp吗?

Not a chance.

不是一个机会。

And that is not because we are meanies but because R and Visual Studio simply do not get along. As Rcpp is all about extending R with C++ interfaces, we are bound by the available toolchain. And R simply does not compile with Visual Studio. Go complain to its vendor if you are still upset.

这并不是因为我们是卑鄙小人,而是因为R和Visual Studio不能和睦相处。由于Rcpp都是关于使用c++接口扩展R的,所以我们受到可用工具链的约束。而R仅仅是不使用Visual Studio编译。如果你还不高兴,就去找供应商投诉。

Microsoft more or less went out of its way to ensure what its OS and tooling were not POSIX compliant. As R grew up in a Unix / POSIX world there simply is a gap you cannot bridge (easily).

微软或多或少都在努力确保其操作系统和工具不兼容POSIX。随着R在Unix / POSIX世界中长大,存在着一个不能轻易跨越的鸿沟。

So on Windows the MinGW port of gcc it is.

所以在Windows上就是gcc的MinGW端口。