4

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!

gipouf
  • 1,221
  • 3
  • 15
  • 43
  • I do not think that the standard binary installations of R have the files you need. I know that *nix users need to install an expanded set of files in order to have the correct header files available. – IRTFM Dec 02 '15 at 17:56
  • 2
    See the R Installation and Administration manual section on [the Windows toolset](https://cran.r-project.org/doc/manuals/r-release/R-admin.html#The-Windows-toolset) -- basically, you'll have a very difficult time using Visual Studio, and instead need to use MinGW-based Rtools. – Martin Morgan Dec 02 '15 at 18:28
  • Could you try VS2015 which supports R natively? Maybe the problem can be fixed sometime? Btw, `.C()` can work with VS, example [here](https://devblogs.nvidia.com/parallelforall/accelerate-r-applications-cuda/). – Patric Mar 30 '16 at 09:21

1 Answers1

4

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. 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 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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725