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

I recently switched from a 32-bit laptop to a 64-bit desktop (both win7). I just found out that when I load a dll using dyn.load I get an error. I assume this is a simple mistake, and I'm missing something.

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

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

Then compile it on the command line:

 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 64 bit 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 forcibly used in the architecture with --arch 64x :

 R --arch x64 CMD SHLIB foo.c 

Clearly, I knew that I was making an abusive mistake :)

+7
source share
2 answers

I assume that you are compiling it into a 32 bit target. You need to build it on a 64-bit machine with 64-bit tools. You cannot load a 32-bit DLL into a 64-bit process and vice versa.

+4
source

what I did was compile with -arch x64 and --arch 32 once and manually put the corresponding .dll (with the same name) in separate src-x64 and src-i386 folders respectively, these two folders under the same The directory where the src folder is located.

-one
source

All Articles