[R] Porting from Linux to Windows

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jan 14 11:21:12 CET 2005


commercial at s-boehringer.de writes:

> I intend to port an R project from Linux to Windows.
> It involves C code that is loaded via dyn.load().
> I could manage to produce a 'dll' File using cygwin which seems to be
> o.k.
> 
> Now, using dyn.load("pcr.dll") i get:
> 
> Error in dyn.load(x, as.logical(local), as.logical(now)) :
>         unable to load shared library
> "c:/cygwin/home/pingu/rt-pcr/pcr.dll":
>   LoadLibrary failure:  Invalid access to memory location.
> 
> Besides modifying the makefile no changes have been made to the source
> code. This is the Makefile (the relevant target is winlib):
> ---
> RHOME="C:/Program Files/R/rw1090"
> 
> SOURCE=lambert.c
> OBJ=lambert.o brent.o nrutil.o pcr.o
> #CFLAGS=-I/usr/lib/R/include -g -O2
> CFLAGS+=-I${RHOME}/src/include -mno-cygwin
> CFLAGS+=-I/usr/lib/R/include -g -O2
> OBJT=${OBJ} test.o
> OBJR=${OBJ} pcr-R.o
> 
> ${OBJ} : ${SOURCE}
> #	cc -I/usr/lib/R/include -o $@.o $@.c
> 
> test : test.o ${OBJ}
> 	cc -g -o test ${OBJT} -lc -lm
> 
> lib : ${OBJR}
> 	cc -o pcr.so ${OBJR} -lc -lm -shared
> 
> # we may not use ld because the -mno-cygwin cannot be passed
> # which is required
> winlib : ${OBJR}
> 	cc -mno-cygwin  -o pcr.dll ${OBJR} ${RHOME}/bin/R.dll -lc -lm -shared
> ---
> 
> Does anybody have some clue? I am using gcc v3.3.3 and R1.9.0 on
> Windows. If there is a recipe somewhere that I have missed I would be
> most grateful.

You're definitely missing R CMD SHLIB for a start. DLLs are more
complicated to make than .so files because they require explicit
export files and such. 

This has all been packaged up in a user friendly (as far as Windows
allows) set of development tools and whatnot. The place to start
reading is readme.packages in the windows distribution, also available
from e.g.

https://svn.r-project.org/R/trunk/src/gnuwin32/README.packages 

As the comment at the start of the document indicates, you should take
care to read the whole text and follow the instructions very
precisely. People often get in trouble by reading what they think
it says and not what it actually says...

If your original project is in the form of a valid CRAN package, and
all the build tools are in place on windows, the porting effort can be
as little as "R CMD build --binary mysrc.tar.gz".

You probably also want to read the "Writing R Extensions" document,
which also ships with R and is on CRAN via

http://cran.r-project.org/manuals.html

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list