Skip to content

Compilers and mrgsolve

Kyle Baron edited this page Apr 26, 2019 · 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.

Installing the Rtools toolchain

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

  • Download RtoolsXX.exe from this page
    • XX is the version number (e.g. Rtools35.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; you might have to opt in to this path modification
  • 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
  • Common error messages from incorrect Windows installs can be found here

Common error messages

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

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

Install C++ and gfortran compilers found here: https://cran.r-project.org/bin/macosx/tools/

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 (right-click, and download) 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 (right-click, and 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.