Skip to content

An example of how to create an R package that utilizes C++ code that calls Fortran code

License

Notifications You must be signed in to change notification settings

duckmayr/mixedlang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mixedlang

mixedlang is an example of how to create an R package that utilizes C++ code that calls Fortran code (hence the name "mixedlang," short for mixed language programming). The package was created as part of an answer to a Stack Overflowquestion about creating an R function that leverages both C++ and Fortran (available here).

The package was created with the following steps:

  1. Set up the package structure from R with RcppArmadillo::RcppArmadillo.package.skeleton("mixedlang") (I only used RcppArmadillo rather than Rcpp since the related Stack Overflow question was -- there's nothing Armadillo specific to this example)
  2. Added the C++ and Fortran code files to the src/ folder
  3. In R, run Rcpp::compileAttributes("mixedlang/") then devtools::install("mixedlang/")

The main things to take into account when calling Fortran code from C++ are:

  1. Fortran arguments are passed by reference, not by value.

  2. Since MULTIPLY is defined in another file, we need to declare it in our C++ file so the compiler knows the argument and return types.

    a. When declaring the Fortran function for our C++ file, we'll drop the case of the function name and append an underscore, since the Fortran compiler should do this by default.

    b. We have to declare the function within an extern "C" linkage specification; C++ compilers cannot typically use function names as unique identifiers since it allows overloading, but for calling Fortran functions, we need it to do exactly that which the extern "C" linkage specification accomplishes (see, for example, this Stack Overflow answer).

About

An example of how to create an R package that utilizes C++ code that calls Fortran code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published