[Rd] Rtools -- compiling C dll for 64-bit

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jul 4 16:34:03 CEST 2014

On 04/07/2014 9:47 AM, Dalthorp, Daniel wrote:
> Thank-you for the prompt and helpful response. It sounds like all I 
> need is a very simple fix, but it eludes me.
> I am trying to call a custom C function from R to speed a calculation. 
> I'm on Windows 7 with R 3.0.2 and the latest Rtools. I run R CMD SHLIB 
> cfunc.c from the DOS command line and a.dll is created. In R, 
> dyn.load("cfunc.dll") works fine with 32-bit R but fails with 64-bit 
> ("LoadLibrary failure:  %1 is not a valid Win32 application", where 
> "%1" refers to cfunc.dll).
> The first part of my PATH reads: 
> c:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;C:\Program Files\R\R-3.0.2\bin\x64

If you run R CMD SHLIB you should by default get the same architecture 
as the version of R that you used.  For example, if I have 64 bit R 
first on my path I see this:

$ R CMD SHLIB test.c
gcc -m64 -I"F:/R/svn/r-devel/R64/include" -DNDEBUG          -O2 -Wall  
9 -mtune=core2 -c test.c -o test.o
gcc -m64 -shared -s -static-libgcc -o test.dll tmp.def test.o 
/R64/bin/x64 -lR

That's a 64 bit build.  If I have 32 bit R first, I see this:

$ R CMD SHLIB test.c
gcc  -I"F:/R/svn/r-devel/R/include" -DNDEBUG          -O3 -Wall 
-std=gnu99 -mtu
ne=core2 -c test.c -o test.o
gcc -shared -s -static-libgcc -o test.dll tmp.def test.o 
n/i386 -lR

That's a 32 bit build.  From the look of your PATH, you should have 64 
bit R first, but you say you're getting 32 bit DLLs, which doesn't make 
any sense.  I'll assume you've got a typo in your message somewhere, but 
you probably want to check what

R --version

prints to be sure.

Duncan Murdoch
> -Dan
> On Fri, Jul 4, 2014 at 3:21 AM, Duncan Murdoch 
> <murdoch.duncan at gmail.com <mailto:murdoch.duncan at gmail.com>> wrote:
>     On 03/07/2014, 7:53 PM, Dalthorp, Daniel wrote:> I've seen that many
>     people have had trouble using Rtools for a 64-bit
>     > compile of C functions. For some, a quick change in path statement
>     seems to
>     > corrects the problem; for others, apparently not. For me? I've been
>     working
>     > at path statements and install/uninstall/reinstall of all sorts
>     of things
>     > for four hours now with no success. The 32-bit dll loads fine, but I
>     need a
>     > 64-bit version.
>     >
>     > Is there an option to tell the compiler with Rtools to use
>     64-bit? Or
>     a way
>     > to kill off the 32-bit version on my machine so I don't need to
>     worry
>     about
>     > it?
>     >
>     > Any guidance would be greatly appreciated!
>     Your post is far too vague.  Where have you seen all these problems?
>     What are you doing?
>     Of course the tools in Rtools have options to select the architecture,
>     but users normally don't need to set those manually.  If you're
>     building
>     R, use "MULTI=64" on the make command.
>     If you're calling compilers directly, use the appropriate command line
>     option or prefix for that compiler.  These are all listed in the
>     MkRules
>     file in src/gnuwin32/MkRules.dist in the R sources, or in the help
>     pages
>     for the commands.
>     Duncan Murdoch
> -- 
> Dan Dalthorp, PhD
> Statistician
> USGS Forest and Rangeland Ecosystem Science Center
> Forest Sciences Lab, Rm 189
> 3200 SW Jefferson Way
> Corvallis, OR 97331
> ph: 541-750-0953
> ddalthorp at usgs.gov <mailto:ddalthorp at usgs.gov>

More information about the R-devel mailing list