Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with loading compiled c code in R x64 using dyn.load

Tags:

c

r

dll

I recently went from a 32bit laptop to a 64bit desktop (both win7). I just found out that I get an error now when loading dll's using dyn.load. I guess this is a simple mistake and I am overlooking something.

For example, I write this simple c function (foo.c):

void foo( int *x) {*x = *x + 1;}

Then compile it in command prompt:

R CMD SHLIB foo.c

Then in 32bit R I can use it in R:

> dyn.load("foo.dll")
> .C("foo",as.integer(1))
[[1]]
[1] 2

but in 64bit R I get:

> dyn.load("foo.dll")
Error in inDL(x, as.logical(local), as.logical(now), ...) : 
  unable to load shared object 'C:/Users/Sacha/Documents/R/foo.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.
nd.

Edit:

For reference, R CMD can be forced in an architecture by using --arch 64x :

R --arch x64 CMD SHLIB foo.c

Quite clear actually, I knew I was making a rooky mistake:)

like image 566
Sacha Epskamp Avatar asked Feb 06 '11 23:02

Sacha Epskamp


1 Answers

My guess is that you are compiling it to a 32 bit target. You need to build it on your 64 bit machine with 64 bit tools. You can't load a 32 bit DLL into a 64 bit process, and vice versa.

like image 168
David Heffernan Avatar answered Nov 05 '22 12:11

David Heffernan