The Tickle Trunk

R Math Library Standalone Binaries

Send Feedback      Home Page > The Tickle Trunk > R Math Library

The R Math Library

On this page you can find build instructions and precompiled binaries for the standalone version of the math library from the R statistical language. This is not included in the standard precompiled R distributions. Although the *nix versions are easy to build from the R sources, building the Windows version of the libraries can be taxing.

Download Win32 and x86 Linux binaries for the rmath library 2.2.1 (585kB)

The standalone math library is just a small part of the full R Project for Statistical Computing.

Build Instructions for the R Standalone Math Library

These instructions describe how to cross-compile a Win32 version of the library on a Linux system. Building the Windows version under Linux may sound like a roundabout approach, but it is far easier than building directly on Windows. As a side effect, the Linux version of the library can also be built (see Step 7). If you only want to build the Linux version, just follow the instructions up to step 7 and stop, but don't bother downloading or installing the cross compiler or unpacking a second copy of the source.

Cross-compiling requires a working R installation of the same version. These instructions will first build a working copy of Linux R, then proceed with building the standalone library for Win32. If you already have a working Linux R installation of the same version, then you can skip unpacking and building another one and just point the R_EXE environment variable to the existing copy in step 7.

1. Unpacking the source archive

You will need to unpack the cross-compiler tools (see below) and two copies of the R source code, one for the Linux build and one for the Win32 build. These instructions assume that you will define the following environment variables:

$TOOLS  is where the cross-compiler is unpacked
$RDIRL  is where R is unpacked for the Linux build
$RDIRW   is where R is unpacked for the Win32 build
$RLIB   is a convenient directory where you will gather the library files when you are done
$ED  is whatever text editor you want to use

For my build I used the following (making the appropriate directories):

TOOLS=~/winr/cross-tools
RDIRL=~/winr/LinuxR/R-2.2.1
RDIRW=~/winr/WinR/R-2.2.1
RLIB=~/winr/output
ED=emacs 

Get a copy of the R source and then unpack two copies, one to the $RDIRL/.. directory and one to the $RDIRW/.. directory:

cd $RDIRL
wget http://cran.stat.sfu.ca/src/base/R-2/R-2.2.1.tar.gz ..
tar zxf ../R-2.2.1.tar.gz
cd $RDIRW
tar zxf ../R-2.2.1.tar.gz 

2. Install the cross-compiler.

Note: It appears that the URLs below no longer work. Try looking here.

You can use this precompiled binary and runtime:

cd $TOOLS
wget http://www.stats.ox.ac.uk/pub/Rtools/mingw-cross5.tar.bz2
wget http://umn.dl.sourceforge.net/sourceforge/mingw/
     mingw-runtime-3.10.tar.gz

To unpack the cross-compiler:

tar jxf mingw-cross5.tar.bz2
mkdir -p i586-mingw32
cd i586-mingw32
tar zxf ../mingw-runtime-3.10.tar.gz

3. Put the cross-compiler binaries in the path.

export PATH=$PATH:$TOOLS/bin 

4. Build the Linux version of R.

You may need to run the configure script a number of times until you identify all of the components R requires that are missing on your system. On the system I used to build, I had to install packages to provide a Fortran compiler and makeinfo (part of GNU texinfo).

cd $RDIRL
./configure --with-readline=no --with-x=no
make 

I also had to turn off the above options. The --with-x=no is presumably because I didn't have the X headers installed. I'm not sure what the --with-readline option does.

With some persistence, you should eventually make it through the configure process and be able to run:

make

5. Speaking of make, you may as well go and make some tea.

6. You can verify that the Linux R build is working with:

make check 

(...and drink your tea while it runs.)

7. [Optional] Build the standalone Linux library.

Since you are here and have configured and built the Linux R anyway, you can make the standalone Linux library at this point if you wish:

cd $RDIRL/src/nmath/standalone
make 

And if you want to test it:

make test
./test 

Then copy the Linux library to your output directory:

mkdir -p $RLIB/linux-x86
cp *.so *.a $RLIB/linux-x86

8. Edit the build settings for the Win32 version:

cd $RDIRW/src/gnuwin32
cp MkRules MkRules.original
$ED MkRules 

Change the following lines (line numbers refer to the source for R-2.2.1):

Line Change
7 BUILD=CROSS
59 BINPREF=i586-mingw32-
61 HEADER=$TOOLS/i586-mingw32/include
64 R_EXE=$RDIRL/bin/R

These set up the build for cross-compilation instead of native Windows compilation, tell it what cross-compiler toolset you are using ($BINPREF is prefixed in front of all of the normal gcc tool names to get the cross-compiling versions), point it to the C header files for your cross compiler, and point it to the location of the Linux R system that you just built.

At this point, if you wanted to build the entire Win32 version of R, you could do so by executing a "make" command. I think you would first need to install at least the source code for Tcl (into $RDIRW) and iconv (into $RDIRW/src/gnuwin32/unicode) to get this to work, but I've never tried.

9. Prepare to build the standalone Win32 library.

cd $RDIRW/src/include
make -f Makefile.win config.h Rconfig.h Rmath.h
cd $RDIRW/src/nmath/standalone 

10. Modify build settings to produce a .def file.

The gcc family and Visual C++ have different ideas about how functions in a .dll should be named. After we are done compiling, we will only be able to use the library with a gcc-based compiler. We will fix this later, but we will need a .def file for the library to do so. We need to modify some compiler options to get the file. To do that:

cp Makefile.win Makefile.win.original
$ED Makefile.win 
Line 9
Replace: DLLFLAGS=-s
With: DLLFLAGS=-s -W1,--output-def,Rmath.def,--out-implib,libRmath.a

(If you don't care about linking to the library from code compiled with Visual C++, you can skip this step and all later steps that refer to the .def file.)

11. Now you should be able to build the library:

make -f Makefile.win 

12. Gather the library files somewhere convenient so you can package them up:

mkdir -p $RLIB/win32
cp *.dll *.a $RLIB/win32
mkdir -p $RLIB/include
cp $RDIRW/include/Rmath.h $RLIB/include 

13. Adjust the .def file.

You will use the .def file to make a .lib that can be linked to from Visual C++. The .def contains the "external" names of the exported functions. The compiler generated a library with internal names that exactly match the external names in this list. If you use this .def file as-is, Visual C++ will assume that the internal names have all been mangled to start with an underscore. This violates Microsoft's own specs for .def files, which state that the external and internal names are the same unless otherwise stated, but there you go. If you don't do this step, then you get a dynamic linking error when you try to call into the library. Windows will state that it couldn't find some function X. Function X is there, but Windows is really looking for _X, which isn't. The error message uses the external name rather than the internal name that is actually being searched for. Anyway, the solution is to explicitly define every internal name to be identical to its external name. The easy way to do this is:

sed 's/^ +\([a-z|A-Z|0-9|_]+\)/ \1=\1/' Rmath.def >$RLIB/win32/Rmath.def 

14. Configure the Rmath.h header file for standalone use.

cd $RLIB
$ED include/Rmath.h 
Line 36
Replace: /* #undef MATHLIB_STANDALONE */
With: #define MATHLIB_STANDALONE

15. Pack the results up for transport to the Windows environment:

zip rmath-lib-2.2.1.zip */* 

16. Generate the Static Link Library.

Once on the Windows machine, unpack the ZIP file. If using Visual C++ to link to the library, you must generate a .lib stub library for static linking. This is why we went to all the trouble with that pesky .def file:

lib /machine:i386 /def:Rmath.def

Add this library to the linker options in Visual C++.

17. [Optional] Neatness counts.

When I did step 16, I renamed Rmath.dll to libRmath.dll, Rmath.def to libRmath.def, and then edited libRmath.def to declare the LIBRARY name as libRmath. This keeps the name of the Windows version of the library the same as that for the Linux version.

18. That's it.

You should be able to use the .dll by linking to Rmath.lib from Visual C++ or libRmath.a for a GNU compiler. You can also link statically from gcc using Rmath.a.


April 23, 2007 Updated January 01, 2011