Compiling shared dinamyc library (.dll) with R terminal in a MS-DOS command prompt window

Note: Perhaps this is a long installation procedure but it has the advantage that you are compiling C code with R, thus avoiding unnecessary build-up made by Microsoft compilers. You will get light versions of your shared dll libraries. (As an example, same C code compiled in Microsoft Visual C++ Express Edition will output a .dll file of size 54KB while this procedure outputs a 5KB dll file). Of course, once installed the procedure is straigthforward.

Installation will be from steps 1 to 7.
Future compiling will be from steps 8 to 9.

(See the troubleshoot with Windows 7 (64 bits) at the bottom)


  1. Locate the Rcmd.exe file. It should be in the home directory of your R installation. The path should be something like (depending on where you installed R): C:\Program Files\R\R-2.9.2\bin
  2. It is recommended creating shortcuts that brings you to this directory from your current R working directory. You will visit several times that location.
  3. Then, create a MS-DOS command prompt shortcut in your current R working directory which starts MS-DOS in the installed R (home) directory. To do that in Windows XP, go to: Start > All Programs > Accesories > right click on "Command Prompt" and choose "copy".
  4. Go to your R working directory and paste the shorcut. Then right click over this shortcut, choose properties and edit "Start in": "C:\Program Files\R\R-2.9.2\bin" (or other, depending on your R installation).
  5. Download and install the latest version of Rtools (a set of GNU public software which comprises all necessary tools to create R packages. We will only need the gcc compiler. You can find the Rtools with google or in this direction ( (Authors of this set of tools are in fact administrators of the R project.)
  6. During the installation you will need to specify where you installed R this time you should indicate (or other, notice it is not the same as above): C:\Program Files\R
  7. Almost at the end of the installation it will ask for Additional Tasks, here you have to ask for the installation procedure to modifiy your system path. It will suggest a default modification. Accept it by clicking Next.
    1. (Optional Troubleshooting:) If you didn't ask for the path automatic modification, you can do it manually:  Control Panel >  System >  Advanced > Environment variables >  System variables > Path > Edit
    2. And add this before what is in that line (do not forget the ending with ;) :
      • c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;c:\R\bin;
    3. You will then have something like: c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;c:\R\bin;<WHATYOUHAVEBEFORE>
  8. Following the example we are using from the very first steps, use your shortcuts to move files "ar.c" and "ar.def" in the R (home) directory. Then use your command prompt shortcut to open MS-DOS in that same location.
    1. (Optional) Type: Rcmd, maximize the window and read the posible options. We will use the SHLIB subcommand. Type: Rcmd SHLIB --help. You will see optional arguments for that subcommand. NOTE: to copy-paste things to this command window, you do the usual procedure, but for pasting in this command window you have to right click and choose paste.
  9. Type: Rcmd SHLIB ar.c   and you will get two new files ar.dll and ar.o. The file with extension o is useless. Move your .c, .def and .dll where you want, but you have to remember where to be able to call the .dll file from R.


 Troubleshoot with Windows 7 (64 bits):


If you get something like this:

"cannot create" tmp.def: "permission denied" make:... Error 2


Do this:

  • Make sure you downloaded and installed the newest version of Rtools (See the given link above. If you have an older version remove it first). Run again the command lines.
  • If that did not work. Run again step 8 and 9 (i.e. run MS-DOS command prompt in the necessary directory) but make sure to run it "as administrator". You can do this by right-clicking and choosing "Run as administrator" (check again you are in the right directory, sometimes it moves). Run again the command lines.
  • If that does not work. Repeat this last step but make sure you de-activate your antivirus. Run again the command lines.