Skip to content

Compilers and mrgsolve

Kyle Baron edited this page Jan 30, 2018 · 30 revisions

Introduction

Current versions of C++ and FORTRAN compilers are required to use mrgsolve. Available compilers and requirements may vary by operating system. Please see system-specific instructions below. In addition to the advice provided here, here is very detailed and complete information on compilers to use with R on the r-project website (https://cran.r-project.org/doc/manuals/R-admin.html and see links below).

If you did install the compilers and still cannot install mrgsolve or compile a model in R, please see the section on testing your compilers. This will give you some simple example code that is independent of mrgsolve that will only run if the compiler is installed properly.

You can also check out the pkgbuild package (https://github.com/r-pkgs/pkgbuild). This package has some functions that will help you diagnose problems with your R build toolchain.

For example, try

install.packages("devtools")
devtools::install_github("r-pkgs/pkgbuild")

check_build_tools()

Please post on the issue tracker if you have questions about compiler requirements.

Windows users

You need to download and properly install the Windows toolset (Rtools). Do not use any compiler other than the one provided by the r-project. You MUST use Rtools. Please update to a recent version even if you think you already have it installed.

installr package

You are encouraged to try the installr package to help you install the correct Rtools package.

install.packages("installr")
install.Rtools()

This might work just fine for your installation. If not, please keep reading.

Installing the Rtools toolchain

These tips are based on many hours spent helping people install Rtools for Windows properly.

  • Read about the Windows toolset
    • Especially if things are not working for you, there is important information to be found in this link. If you haven't read this, read it now.
  • Download RtoolsXX.exe
    • XX is the version number (e.g. Rtools32.exe)
    • Get the version number that corresponds to your version of R
    • Do not get a version that is not frozen yet; experience has told us that the latest / newest version isn't at all the best
  • Install RtoolsXX.exe
    • When you are installing, pay attention for a prompt asking about changing the PATH; make sure that the installer updates PATH
  • Check your PATH environment variable in R
    • Use the command Sys.getenv("PATH") in R, after restarting to make sure the proper directories are listed there. R will not find the toolchain unless the PATH is correctly
    • If PATH is not properly set, try to set it with a system-wide environment variable
    • If a system-wide environment variable is not posible, there are some other options here
  • Install Rcpp, RcppArmadillo, and mrgsolve all from source
    • Do this even if you think you already have them installed; it is important to re-install these packages after installing Rtools
    • To install from source: install.packages(c("Rcpp", "RcppArmadillo"), type="source")

Common error messages

  • Please see this page for common error messages you might encounter if Rtools isn't installed correctly.

Having problems installing Rtools?

Installation instructions and download links for Rtools can be found in these links:

Please read carefully. There isn't a lot to read here, but there are clear and easy-to-follow directions about what you need to do directly from the R people.

Instructions from the rstan site

You can look here for another set of instructions if the advice provided above didn't answer your questions: Rtools install instructions on the rstan github page

Mac OSX users

If using R binary from CRAN

A suitable C++ compiler is available in Xcode (https://developer.apple.com/xcode/). Be sure to download and install command line tools (bash$ xcode-select --install). Always use the most up to date version.

Xcode does not include a FORTRAN compiler. There are different FORTRAN compiler requirements for Snow Leopard or Mavericks (or greater) R builds. See the platform-specific FAQs below.

Carefully read the following for information:

  1. https://cran.r-project.org/doc/manuals/R-admin.html#macOS

There are several options for install a gfortran compiler for macOS. If you are using El Capitan or Sierra, you need an "alternative" gfortran installation. To our knowledge, this is the only viable option for El Capitan or Sierra users ... so it really should be "standard" now rather than "alternative". There are two gfortran binaries that you can choose from and there are some extra steps you need to take in writing your Makevars file too (see below).

For so-called Mavericks builds on older Mac OSX operating systems, we still recommend the gfortran-4.8 as distributed by R folks. This is an older gfortan version, but it is likely what you need for that Mavericks build.

This is a bit of a fluid situation. Please don't hesitate to ask for help on the issue tracker. We'd be happy to help or advise as best as we can.

  1. To use an alternative gfortran compiler, especially if using El Capitan or Sierra:

  2. gfortran for Mavericks build:

If using R installed from homebrew

  • We recommend and support using the CRAN binary and toolchain, but we understand that a pure homebrew implementation (R and toolchain) has worked.
    • The key seems to be matching the homebrew R install with the homebrew gcc install.
  • If you are using the homebrew approach, please contact personnel from that project for help with compiler issues.

Unix users

UNIX usually include C++ and FORTRAN compliers. If not, install gcc.

Test your compilers

Here is some code that you can run in an R script (in Rstudio) that will help you test if your C++ compiler is properly installed. You should be able to run R CMD SHLIB without any errors.

Test your C++ compiler

Get the source code here.

ccode <- 'extern "C" {void test() {double x=2.1; x = x+1;}}\n'
cat(file="R_Cpp_Test.cpp", ccode)
system("R CMD SHLIB R_Cpp_Test.cpp")

The output might look like this on Mac:

/usr/bin/clang++  -Wall -O2 -Qunused-arguments -Wpedantic -I/Library/Frameworks/R.framework/Resources/include 
-DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC  -Wall -mtune=core2 -g 
-O2  -c R_Cpp_Test.cpp -o R_Cpp_Test.o
/usr/bin/clang++ -Wall -O2 -Qunused-arguments -Wpedantic -dynamiclib -Wl,-headerpad_max_install_names 
-undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib 
-L/usr/local/lib -o R_Cpp_Test.so R_Cpp_Test.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework 
-Wl,CoreFoundation

The output might look like this on Windows:

c:/Rtools/mingw_64/bin/g++  -I"C:/Users/YSU/R-33~1.1/include" -DNDEBUG 
-I"d:/Compiler/gcc-4.9.3/local330/include"     -O2 -Wall  -mtune=core2 -c R_Cpp_Test.cpp -o R_Cpp_Test.o
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o R_Cpp_Test.dll tmp.def R_Cpp_Test.o 
-Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib 
-LC:/Users/YSU/R-33~1.1/bin/x64 -lR

The output might look like this on Linux:

g++ -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 
-Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c R_Cpp_Test.cpp -o R_Cpp_Test.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o R_Cpp_Test.so R_Cpp_Test.o 
-L/usr/lib/R/lib -lR

Test your FORTRAN compiler

NOTE this will be picky about spaces; please download the source code here.

fcode <- '
      subroutine bar(n, x)
      integer n
      double precision x(n)
      integer i
      do 100 i = 1, n
          x(i) = x(i) ** 2 
  100 continue
      end
'
cat(file="R_FORTRAN_Test.f", fcode)
system("R CMD SHLIB R_FORTRAN_Test.f")

Result on Mac:

gfortran-4.8   -fPIC  -g -O2  -c R_FORTRAN_Test.f -o R_FORTRAN_Test.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module 
-multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib 
-o R_FORTRAN_Test.so R_FORTRAN_Test.o -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 
-lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework 
-Wl,CoreFoundation

Result on Windows:

c:/Rtools/mingw_64/bin/gfortran      -O2  -mtune=core2 -c R_FORTRAN_Test.f -o R_FORTRAN_Test.o
c:/Rtools/mingw_64/bin/gcc -shared -s -static-libgcc -o R_FORTRAN_Test.dll tmp.def R_FORTRAN_Test.o 
-Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib -lgfortran -lm -lquadmath 
-LC:/Users/YSU/R-33~1.1/bin/x64 -lR

Result on Linux:

gfortran   -fpic  -g -O2  -c R_FORTRAN_Test.f -o R_FORTRAN_Test.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o R_FORTRAN_Test.so 
R_FORTRAN_Test.o -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR

If running the simple example code presented here, it means your compiler is not properly installed. Please touch base with us on the issue tracker for help.

Additional technical notes

The compilers will be used to compile C++ and FORTRAN code inside the mrgsolve package as well as user-created models. It is imperative that mrgsolve be compiled with the same compiler used to compile the user-created model. If different compilers are used, it is likely that a segmentation fault will happen. This behavior would be expected when different compilers are used.

Clone this wiki locally