Skip to content

Link error with R.dll

November 21, 2015

I’ve taken a crack at compiling the R package gputools on Windows, but I hit a bit of a roadblock. I’ll describe the steps I’ve taken below, hopefully someone will be able to suggest a way forward.

Some background: gputools provides an interface between R and CUDA, the nVidia API for executing code on a graphics card (GPU). On Linux or Mac OS, installation is fairly straightforward because the CUDA compiler nvcc depends on gcc or clang, respectively. However, interfacing between R and CUDA on Windows is an unsolved problem, since the respective toolchains are fundamentally incompatible. The only supported toolchain for compiling R on Windows is MinGW with gcc and gfortran. On the other hand, the nVidia CUDA Toolkit requires certain specific versions of Microsoft Visual Studio.

I installed Visual Studio Community 2013 and nVidia Toolkit 7.5.18 for Windows 10. You can fool the gputools configure script by running it inside a Cygwin64 terminal. This enables you to generate a Makefile for gcc + nvcc, but of course this won’t actually work as I’ve explained above. There are some Makefile entries that you will need to edit manually, as follows:

NVCC = nvcc
CUDA_LIB = $(CUDA_HOME)/lib/x64
R_LIB = -L"$(R_HOME)/bin/x64" -lR
CFLAGS = -O2 -Wall
TARGETS = gputools.dll

Before you run make, add cl.exe to your PATH so that nvcc can find it:

set PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin

You will also need to create a file vcvars64.bat in the directory VC\bin\amd64, containing only a single line:

CALL setenv /x64

Unfortunately, there is some unportable code in and that won’t compile in cl: error: expression must have a constant value error: expression must have a constant value
2 errors detected in the compilation of "qrdecomp.cpp1.ii".
make: *** [qrdecomp.o] Error 2

../VC/INCLUDE\xlocale(1610): error: identifier "_First1" is undefined
../VC/INCLUDE\xlocale(1610): error: identifier "_Last1" is undefined
../VC/INCLUDE\xlocale(1610): error: identifier "_Count" is undefined
../VC/INCLUDE\xlocale(1845): error: identifier "_First1" is undefined
../VC/INCLUDE\xlocale(1845): error: identifier "_Last1" is undefined
../VC/INCLUDE\xlocale(1845): error: identifier "_Count" is undefined
6 errors detected in the compilation of "cuseful.cpp1.ii".
make: *** [cuseful.o] Error 2

For, you need to allocate the arrays using Calloc (and deallocate using Free). For, the problem is with STL std::string. I chose to work around this problem using good old char* instead. Most of the calls to that private method use .c_str() anyway. If cl had an equivalent compile flag to -std=gnu99 then these changes wouldn’t be necessary.

This is where I hit the final problem, which currently has me stumped:

cuseful.o : error LNK2019: unresolved external symbol Rprintf referenced in function "void __cdecl printMat(int,int,float const *,char const *)" (?printMat@@YAXHHPEBMPEBD@Z)
gputools.dll : fatal error LNK1120: 15 unresolved externals
make: *** [gputools.dll] Error 2

The version of R that I have installed provides R.dll, but not R.lib. I tried using pexports to create a .def file, but I still end up with unresolved externals as above. It is possible that the only way around this is to compile my own copy of R.dll from source.




From → R

  1. Daniel permalink

    Great post, I’ve just found it today after trying to do exactly what you did and looking for solutions to the qrdecomp/cuseful issues. Did you ever find a way to get gputools to compile on Windows given the lack of an R.lib?

  2. Sadly, no. I planned to revisit this once CUDA 8.0 was released, since it supports Visual Studio 2015. However, even with Microsoft R Open (MRO) and R Tools for Visual Studio (RTVS), it doesn’t sound like there will ever be a compatible toolchain for CUDA in R on Windows:

  3. The documentation for Rth mentions Windows, but it really isn’t clear whether they’ve ever gotten it working on that platform:

    Another option would be OpenCL, since you might be able to compile that with the Intel INDE for Windows:

  4. Daniel permalink

    However, I am not a C/C++ developer, so I made some pretty severe guesses on how to fix and

    For, I did this:
    // added after line 803
    (804) float * scales;
    (805) double * Beta;
    (806) scales = Calloc(blockSize, float);
    (807) Beta = Calloc(blockSize, double);
    // added after line 912

    For, I did this:
    (line 104) changed return type of function to be char *
    (line 133) deleted .c_str()
    (line 140) deleted .c_str()
    I also deleted all #include lines at top

    Would you be able to let me know if these are correct and what you used in your code?

Trackbacks & Pingbacks

  1. Fixing compile issues on SPARC Solaris | Matt Moores

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Richard Everitt's blog

Computational Bayesian statistics

Let's Look at the Figures

David Firth's blog

Nicholas Tierney

Computational Bayesian statistics

One weiRd tip

Computational Bayesian statistics

Series B'log

discussion blog for JRSS Series B papers

Mad (Data) Scientist

Musings, useful code etc. on R and data science


R news and tutorials contributed by (750) R bloggers

Another Astrostatistics Blog

The random musings of a reformed astronomer ...

Darren Wilkinson's research blog

Statistics, computing, data science, Bayes, stochastic modelling, systems biology and bioinformatics


Computational Bayesian statistics

StatsLife - Significance magazine

Computational Bayesian statistics

(badness 10000)

Computational Bayesian statistics

Igor Kromin

Computational Bayesian statistics


I can't get no

Xi'an's Og

an attempt at bloggin, nothing more...

Sam Clifford

Postdoctoral Fellow, Bayesian Statistics, Aerosol Science

Bayesian Research & Applications Group

Frontier Research in Bayesian Methodology & Computation

%d bloggers like this: