Skip to content

Compilers and mrgsolve

Kyle Baron edited this page Feb 27, 2017 · 30 revisions

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. But note well that the toolchain that needs to be in place is the usual toolchain required for ordinary use of R (either to compile R from source or to compile R packages from source). There 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). There are no special compiler requirements to get mrgsolve up and running; just install the compilers you would normally need to use with R and mrgsolve will compile and you will be able to compile your mrgsolve models.

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.

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.

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.

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.

Tips for 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 from incorrect Windows installs can be found here

See also

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#OS-X
  2. https://cran.r-project.org/doc/manuals/R-admin.html#Installing-R-under-OS-X

To get gfortran for Mac OSX specificially, first please note if you are using a Mavericks build or Snow Leopard build (your choice of gfortran compiler will depend on this). We recommend these gfortran compilers because they are the ones with which R is built. There is a third option listed below if you desire to use a non-standard gfortran implementation.

  1. gfortran for Mavericks build:

  2. gfortran for Snow Leopard build:

  3. To use an alternative gfortran compiler:

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

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; copy and paste carefully.

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.

Clone this wiki locally