These are my notes for how to build R, and the ncdf/ncdf4 packages, under Windows. The information here is for my own personal use, but I've put it here in case it is helpful to someone else trying to build these, or other, packages under Windows. I *do* *not* support any users in attempting to build ncdf or ncdf4 under windows. USE THIS INFORMATION AT YOUR OWN RISK!!!

I use a 64-bit installation of Windows 7.

Building R for Windows 64-bit

2013-05-09: Starting from scratch on my Lenovo T420 laptop at work

1) Make sure to ALWAYS open consoles by RIGHT-CLIKING on the icon and selecting "Run as Administrator". Otherwise, there can be odd permission errors.

2) Delete any previous R directories from your system's PATH variable. I use RapidEE (Rapid Environmental Editor) for this task, but you can also go to control panel -> system -> advanced system settings (look in the upper left) -> Environmental Variables, click on 'Path', then click 'Edit'. Open new MS-DOS command windows after this step so they have the proper path.

3) Download R toolset from CRAN's "Building R for Windows" page. I downloaded "Rtools30.exe" and ran it as administrator, selecting "R\2013-05-09" as the directory to install into. I chose both the 32-bit and 64-bit build, and included the extra tools necessary for a R build (Tcl). Allow the installer to set the system PATH, since you removed old entries in step 2.

4) Download the R src gzip file from CRAN. Put it in c:R\2013-05-09, then:

	gzip --de *gz
	tar xvf *tar

4) Do the following:

	cd c:\R\2013-05-09\Rtools
	mkdir tmp

5) cd to "c:\R\2013-05-09\R-3.3.0\src\gnuwin32"

6) Edit file Mkrules.dist. 1) Change the line that says "MULTI = " to "MULTI = 64". 2) Change the line that says "BINPREF64 = x86_64-w64-mingw32-" to "BINPREF64 = c:/R/2013-05-09/Rtools/gcc-4.6.3/i686-w64-mingw32/bin/".

7) Type 'make'.

8) Got a problem with Tcl include file tcl.h not being found. While in directory R/2013-05-09/R-3.0.0 had to do this:
cp -r ../R64/Tcl .
and then 'make again.

Building the ncdf/ncdf4 package for Windows, 64-bit

1) if ncdf is already installed:
R CMD REMOVE ncdf4

2) Make a new temporary directory my_packages, for example, "C:\R\2013-05-09\my_packages"

2) Copy ncdf4_1.9.tar.gz (the source package) to my_packages

3) In directory my_packages:

	gzip --de ncdf4_1.9.tar.gz 
	tar xvf ncdf4_1.9.tar
Move the original tar file to some other name: "mkdir orig ; mv ncdf4_1.9.tar orig/."

3.1) cd ncdf4/R and copy load.R.in to load.R; edit it so that the library always loads (i.e., get rid of the "if" and "else" lines, and only include the library.dynam line).

3.5) cd ../src, and put this into file Makevars.win:
PKG_LIBS=-Lc:/R/netcdf_win32_dlls_from_unidata -lnetcdf

where, for example, c:R/netcdf_win32_dlls_from_unidata (NOTE: use FORWARD slashes in the Makevars.win, not backslashes) is the location where we will put all the netcdf dll's downloaded from unidata (a later step).

4) Make a gzipped tar file of this modified ncdf directory so we can install it:
cd ../.. ; tar cvf ncdf4_1.4.tar ncdf4 ; gzip -v ncdf4_1.4.tar




OLD NOTES

2012-02-09: Starting from scratch on my Lenovo T420 laptop at work

1) Download R toolset from CRAN's "Building R for Windows" page. I downloaded "Rtools215.exe" and ran it, selecting "R\2012-02-09" as the directory to install into. I chose a 64-bit build, and included the extra tools necessary for a R build (Tcl).

2) Set the Windows path to use the toolset in this order: Rtools\bin; Rtools\gcc-4.6.3\bin. Put these BEFORE the other elements in the path (otherwise you get errors regarding "comm: file 1 not in sorted order"), and make sure to set these into the SYSTEM path var, not the user's path var. To check the current path, open a regular MS-DOS command line window and type "PATH". To set the path in windows 7, go to control panel -> system -> advanced system settings (look in the upper left) -> Environmental Variables, click on 'Path', then click 'Edit'. Open new MS-DOS command windows after this step so they have the proper path.

3) Download the R src gzip file from CRAN. Put it in c:R\2012-02, then:
gzip --de *gz
tar xvf *tar
I got a lot of "file ownership" errors on this step but it doesn't seem to have hurt it any.

4) cd to "c:\R\R-2.14.1\src\gnuwin32" and type "make".

5) I had *HUGE* problems with errors such as "cannot create directory /tmp/R2240: directory nonexistant". I think the problem is that directory /tmp is not "found" as a valid directory even though c:\tmp is there. Note that R2240 is a temporary file that is supposed to contain all the .R source code, which is then moved to a file called "all.R". My work around is to edit file "..share/make/basepkg.mk", and change the long script in the target for mkR1: from this:

	@(f=$${TMPDIR:-/tmp}/Rtmpfile; \
	  echo "**b $${f}"; \
	    cat $(RSRC) > "$${f}"; \
	  $(SHELL) $(top_srcdir)/tools/move-if-change "$${f}" all.R)
	f=$${TMPDIR:-/tmp}/Rtmpfile
	cat $(RSRC) > all.R
	echo "done with long script"
	@$(SHELL) $(top_srcdir)/tools/copy-if-change all.R \
	...etc...

to just the super-simplified:

	cat $(RSRC) > all.R

Had to do a similar thing to target mkR2, but include a package header:

	$(ECHO) ".packageName <- \"$(pkg)\"" >  all.R
	cat `LC_COLLATE=C ls $(srcdir)/R/*.R` >> all.R

*** SEE MY MODIFIED FILE ...share\make\basepkg.mk ***

6) Got a problem with Tcl include file tcl.h not being found. While in directory R/2012-02/R-2.14.1 had to do this:
cp -r ../R64/Tcl .
Actually that still didn't work. For some reason it seems to be building the 32 bit version rather than the 64 bit version, so I downloaded the 32 bit Rtools and copied the Tcl library over.

7) Final bin is in R-2.14.1/bin/R.exe. Add R-2.14.1/bin to your windows path, so it can be started up in any DOS command line window just by typing "R".

Building the ncdf/ncdf4 package for Windows

0) if ncdf is already installed:
R CMD REMOVE ncdf4

1) Make a new temporary directory my_packages, for example, "C:\R\2011-07\my_packages"

2) Copy ncdf4_1.4.tar.gz (the source package) to my_packages

3) In directory my_packages:
gzip --de ncdf4_1.4.tar.gz and then tar xvf ncdf4_1.4.tar
Move the original tar file to some other name: "mkdir orig ; mv ncdf4_1.4.tar orig/."

3.1) cd ncdf4/R and copy load.R.in to load.R; edit it so that the library always loads (i.e., get rid of the "if" and "else" lines, and only include the library.dynam line).

3.5) cd ../src, and put this into file Makevars.win:
PKG_LIBS=-Lc:/R/netcdf_win32_dlls_from_unidata -lnetcdf

where, for example, c:R/netcdf_win32_dlls_from_unidata (NOTE: use FORWARD slashes in the Makevars.win, not backslashes) is the location where we will put all the netcdf dll's downloaded from unidata (a later step).

4) Make a gzipped tar file of this modified ncdf directory so we can install it:
cd ../.. ; tar cvf ncdf4_1.4.tar ncdf4 ; gzip -v ncdf4_1.4.tar

4b) Download Unidata's precompiled netcdf .dlls. THESE ARE HARD TO FIND. They are *not* under the download link. Instead they are in the FAQ, in the question: How can I use netCDF-4.1 with Windows? NOTE that there is also a question How can I use netCDF-4 with Windows? that links to an OLDER VERSION (4.0.1), and should not be used. Man they make it difficult!! Here is a direct link: Download netcdf-4.1.1 windows-32 dlls

5) Put a copy of netcdf.h from the netcdf 4.1.1 package source, directory "libsrc4" (note: NOT "libsrc") into the R/2012-02-09/R-2.14.1/include directory. Get these from a download of the netcdf 4.1.1 src package, not from the windows dll zip downloaded in the prevous step. For example:

cd C:R\2012-02-09\R-2.14.1\include
cp /where/ever/libsrc4/netcdf.h .

6) Copy all NCAR's distributed netcdf dll's to R:
cd C:R\2012-02-09\R-2.14.1\bin\i386
cp /where/ever/netcdf.dll .
cp /where/ever/hdf5_hldll.dll .
cp /where/ever/hdf5dll.dll .
cp /where/ever/szip.dll .
cp /where/ever/zlib1.dll .

NOTE: if one of the dll's depends on ANOTHER dll that is not copied as above, then the calling dll generates an error that it is "not a valid Win32 application", which is misleading!!

7) Back in the my_packages directory, type "R CMD INSTALL --build ncdf4_1.4.tar.gz". This should produce a "ncdf4_1.4.zip" file.

8) This new zip file does not have the necessary hdf5 and netcdf dll's (NCAR's dll's) in it. We need to put the dll's into the zip file. For some reason I found this tricky, many things that I thought would work seemed to, but then R choked when trying to install the zip file (saying it could not 'read' the zip file?!?). It seems to care about the NAME of the zip file; nothing but "ncdf4_1.4.zip" will apparently work. The following procedure seemed to work:

	a) rm -r ncdf4
	b) unzip ncdf4_1.4.zip  (using the MS-DOS command line unzip program, not windows XP unzip feature)
	c) mv ncdf4_1.4.zip orig/.
	d) go into ncdf4/libs/i386 ; copy all the hdf5 and netcdf dll's from step 6 to this directory, so that
	   it has ncdf4.dll AND netcdf.dll, hdf4_hldll.dll, szip.dll, zlib1.dll, etc. in it.
	e) go back out to my_packages
	f) zip -r ncdf4_1.4.zip ncdf4 (again using the MS-DOS command line 'zip' program)

You now have a file ncdf4_1.4.zip that you can install from the windows R gui using menu item Packages... | Install packages from local zip files.


Go back to the ncdf/ncdf4 home page


Last modified: 26 May 2011
Contact: dpierce@ucsd.edu