问题
I'm trying to build a simple R package with Visual Studio, here is my code:
#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.
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?
Thanks!
回答1:
Allow me to quote from the Rcpp FAQ vignette:
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 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).
So on Windows the MinGW port of gcc it is.
来源:https://stackoverflow.com/questions/34048938/building-r-packages-c-api-with-visual-studio