top of page
Search

How to build a C++ library package in R

  • Writer: Behzad Alimoradian
    Behzad Alimoradian
  • Mar 4, 2021
  • 3 min read

I am currently building an R package that has both Boost and Quantlib dependency. It is a library package, that took me a while to compile in R. I thought it might be interesting and useful to share my experience with others. I came up with the following solution that works for a Windows 64-bit platform.


Here are the instructions:


Download and install R from: http://cran.r-project.org/bin/windows/base/

Download and install the latest version of Rtools from: http://cran.r-project.org/bin/windows/Rtools/

Download and install MikTex from: http://miktex.org/download

Add to System Environment PATH: to R,

Example: C:\R-3.1.2\bin; to RTools, example: C:\Rtools\bin;c:\Rtools\gcc-4.6.3\bin;C:\Rtools\gcc-4.6.3\i686-w64-mingw32\bin; to perl, example: to miktex, example: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin; Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables Edit path variable by appending paths after ;


Download Boost and build it with Rtools

  1. Unpack loaded code

  2. Open command line Win+R -> cmd.exe

  3. Change current folder to your Boost folder. For example, cd C:\libraries\boost_1_57_0 4. Type command: bootstrap mingw

  4. Type command: b2 toolset=gcc address-model=3. Your compiled libraries are now in stage/lib subfolder

  5. Type command: b2 toolset=gcc address-model=64. Your compiled libraries are now in stage/lib subfolder

  6. In case you have problems due to missing ml, make sure MASM and MASM64 (ml.exe and ml64.exe) are available in PATH. This should be in the C:\Program Files (x86)\Microsoft Visual Studio path

Download QuantLib and build it with your version of Boost headers.

You can download QuantLib from http://quantlib.org/ make sure to download the .tar.gz file. Here we have loaded QuantLib-1.5


open msys and run the following instructions:

  1. cd C:/Working/QuantLib

  2. tar xzvf QuantLib-1.4.tar.gz. Also make sure that the path to mingw is set to the one in Rtools

open msys, run the following instructions:

  1. cd C:/Working/QuantLib/QuantLib-1.4

  2. configure CXX=’g++ -m32′ CXXFLAGS=’-g -O2 -std=c++0x’ –with-boost-include=C:/Working/boost/boost_1_57_0 –prefix=C:/Working/QuantLib/QuantLib-1.4

Build the same library for x64 architecture: CXX=’g++ -m64′ CXXFLAGS=’-g -O2 - std=c++0x’ Note that you should have around 5GB of free space for successful compilation


Create a folder for your package

  1. Create three folders: R, man, src

  2. Create or update DESCRIPTION file with information about the package, its author, and link libraries

  3. Copy the code you need to build the function into the src folder

  4. If the original code could be compiled without issues with Rtools compiler (mingw), you should only add dllmain.cpp with defined dllentry

  5. Make a file named NAMESPACE in the root directory. There we define which dlls we need for the package to work (useDynLib) and what we should export from them (export or export pattern). Also we should identify which R packets we should import (import)

R package run:

  1. Write wrapper on R to call function from dll

  2. To make documentation, open R. Type install.packages(‘roxygen2’). Change folder to your package root folder. Type roxygen2::roxygenise().

Create file Makevars.win to build your package library in the windows environment.

In this file it is very important to set following the variables: SOURCES, OBJECTS, PKG_CPPFLAGS, PKG_LIBS. These variables will be updated as you continue to develop your code. Of course, you can add additional build targets. SOURCES is an enumeration of all cpp files you need to build your function from the project (including dependencies). You should update this variable whenever you add additional cpp files. OBJECTS is an enumeration of object files compiled from SOURCES. PKG_CPPFLAGS is the set of flags used for code compilation, including paths to include files. Note we also use the -std=c++0x flag for Boost compatibility. Here the $(shell RScript -e Rcpp:::CxxFlags()) command helps access R and Rcpp include files. You should update this variable whenever you add new h or hpp files to build. PKG_LIBS is the set of flags used for code linking, including paths to additional libraries. $(shell RScript -e Rcpp:::LdFlags()) helps to link Rcpp libraries. You should update this variable whenever you require the use of a new library in your code


Prepare environment.

For some reason in current version of R, makefile for package uses only gcc for linking. And we need to use g++ to support template functionality of boost %.dll: replace line 216 in \etc\i386\Makeconf and in \etc\x64\Makeconf $(SHLIB_LD) -shared $(DLLFLAGS) -o $@ $*.def $^ $(ALL_LIBS) with $(SHLIB_CXXLD) -shared -o $@ $*.def $^ $(ALL_LIBS) or replace line: CC = $(BINPREF)gcc $(M_ARCH) with CC = $(BINPREF)g++ $(M_ARCH)


If you successfully complete all the previous steps, you can verify your package.

Open command line Win+R cmd.exe. Change current folder to your package parent folder. Type: R CMD check

 
 
 

Recent Posts

See All
On Dupire local volatility

The Dupire equation for local volatility is surely one of the biggest technological discoveries in the pricing of equity derivatives. It...

 
 
 
What is the risk neutral measure?

Mastering risk neutral probabilities took me a while so I thought it worthwhile to share my experience. The risk neutral probability is...

 
 
 
On variable annuities

It’s been a while since I looked at variable annuity products so I am writing this post to refresh my own memory as well as provide...

 
 
 

Comments


Post: Blog2_Post
  • LinkedIn

©2021 by Behzad Alimoradian.

bottom of page