Problem with loading compiled c code in R x64 usin

2019-02-10 02:07发布

问题:

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:)

回答1:

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.



回答2:

what i did is to compile with --arch x64 and --arch 32 once a time and manually put corresponding .dll ( with the same name ) under separate folders src-x64 and src-i386 respectively, these two folders are under the same directory where the folder src is.



标签: c r dll