使用 Visual Studio 构建 R 包 (C API)
Building R packages (C API) with Visual Studio
我正在尝试使用 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;
}
我安装了 R 运行时和 RTools。
当我尝试编译它时,出现以下 link 错误:
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)
嗯,我想我缺少一些 linking 过程所需的二进制文件。问题是我不知道在哪里可以找到必要的 .lib
文件。在 R 运行时安装文件夹中,我可以找到 include
目录,但找不到任何 lib
目录。我错过了什么?
谢谢!
请允许我引用 Rcpp FAQ 小插图:
Can I use Rcpp
with Visual Studio ?
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.
Microsoft 或多或少地竭尽全力确保其 OS 和工具不符合 POSIX。当 R 在 Unix / POSIX 世界中成长时,存在一个无法(轻易)弥合的鸿沟。
所以在 Windows gcc 的 MinGW 端口上。
我正在尝试使用 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;
}
我安装了 R 运行时和 RTools。
当我尝试编译它时,出现以下 link 错误:
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)
嗯,我想我缺少一些 linking 过程所需的二进制文件。问题是我不知道在哪里可以找到必要的 .lib
文件。在 R 运行时安装文件夹中,我可以找到 include
目录,但找不到任何 lib
目录。我错过了什么?
谢谢!
请允许我引用 Rcpp FAQ 小插图:
Can I use
Rcpp
with Visual Studio ?Not a chance.
And that is not because we are meanies but because
R
and Visual Studio simply do not get along. AsRcpp
is all about extendingR
withC++
interfaces, we are bound by the available toolchain. AndR
simply does not compile with Visual Studio. Go complain to its vendor if you are still upset.
Microsoft 或多或少地竭尽全力确保其 OS 和工具不符合 POSIX。当 R 在 Unix / POSIX 世界中成长时,存在一个无法(轻易)弥合的鸿沟。
所以在 Windows gcc 的 MinGW 端口上。