[Rcpp-devel] Rcpp with C++ and Fortran

Nick Matzke matzke at berkeley.edu
Wed Jan 23 22:02:23 CET 2013


The short version is that basically you just have to put the 
FORTRAN .f files into /src in your package, and access them 
from R with the .C command.  The hardest part for me was 
figuring out just what the heck the inputs and outputs were 
for my FORTRAN code, as it was vaguely documented.

I have a functioning example in the rexpokit package:

https://r-forge.r-project.org/scm/viewvc.php?view=rev&root=rexpokit&revision=5

Cheers,
Nick


On 1/23/13 12:38 PM, Douglas Bates wrote:
> Could you clarify a bit, Rodney.  When you say you want to
> "link it with Fortran code" are you using the word "link" in
> the sense of linking to object code or do you mean that you
> will copy the Fortran source files into your package's src
> directory and have them compiled with other source files?
>   The second, which I would describe as "calling Fortran
> subroutines from C++ code", is definitely easier.
>
> To actually call a Fortran subroutine you need to know about
> appending underscores on entry point names and the fact that
> all arguments to Fortran functions are passed by reference.
>   That is, everything is a pointer.  It might be easiest if
> you could post the calling sequence of a particular
> subroutine so we could show the exact code.  I'm happy to
> carry on the conversation off-list if this seems like too
> much noise for this list.  Discussing calling Fortran code
> on a C++ list is a "keep 'em down on the farm after they've
> seen Paris" situation.
>
>
> On Wed, Jan 23, 2013 at 2:16 PM, Rodney Sparapani
> <rsparapa at mcw.edu <mailto:rsparapa at mcw.edu>> wrote:
>
>     Hi!
>
>     Quick question:  has anyone used Rcpp with C++ and Fortran?
>
>     Longer version:  We have developed some C++ code to do
>     MCMC sampling
>     with RcppEigen.  It works pretty well:  about a 70X speed
>     improvement over R code.  We have been able to link this
>     with some
>     ARMS C code rather easily.  However, now we want to link
>     it with
>     Fortran code from the DPpackage R package.
>
>     I have looked through as many Rcpp docs as I could get
>     my hands, but I
>     didn't see anything about Fortran (plus googling).
>       Anyways, what is
>     the best way to call Fortran functions from our C++ code
>     that will be
>     used via Rcpp/inline?  I've played around with f2c to
>     convert the
>     Fortran code to C (but it makes me nervous ;o)  Should I
>     be working
>     from a package skeleton?  Basically, any advice is
>     welcome.  Thanks!
>
>     --
>     Rodney Sparapani, PhD  Center for Patient Care and
>     Outcomes Research
>     Sr. Biostatistician http://www.mcw.edu/pcor
>     4 wheels good, 2 wheels better!   Medical College of
>     Wisconsin (MCW)
>     WWLD?:  What Would Lombardi Do?   Milwaukee, WI, USA
>     _________________________________________________
>     Rcpp-devel mailing list
>     Rcpp-devel at lists.r-forge.r-__project.org
>     <mailto:Rcpp-devel at lists.r-forge.r-project.org>
>     https://lists.r-forge.r-__project.org/cgi-bin/mailman/__listinfo/rcpp-devel
>     <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
>
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>

-- 
====================================================
Nicholas J. Matzke
Ph.D. Candidate, Graduate Student Researcher

Huelsenbeck Lab
Center for Theoretical Evolutionary Genomics
4151 VLSB (Valley Life Sciences Building)
Department of Integrative Biology
University of California, Berkeley

Graduate Student Instructor, IB200B
Principles of Phylogenetics: Ecology and Evolution
http://ib.berkeley.edu/courses/ib200b/
http://phylo.wikidot.com/


Lab websites:
http://ib.berkeley.edu/people/lab_detail.php?lab=54
http://fisher.berkeley.edu/cteg/hlab.html
Dept. personal page: 
http://ib.berkeley.edu/people/students/person_detail.php?person=370
Lab personal page: 
http://fisher.berkeley.edu/cteg/members/matzke.html
Lab phone: 510-643-6299
Dept. fax: 510-643-6264

Cell phone: 510-301-0179
Email: matzke at berkeley.edu

Mailing address:
Department of Integrative Biology
1005 Valley Life Sciences Building #3140
Berkeley, CA 94720-3140

-----------------------------------------------------
"[W]hen people thought the earth was flat, they were wrong. 
When people thought the earth was spherical, they were 
wrong. But if you think that thinking the earth is spherical 
is just as wrong as thinking the earth is flat, then your 
view is wronger than both of them put together."

Isaac Asimov (1989). "The Relativity of Wrong." The 
Skeptical Inquirer, 14(1), 35-44. Fall 1989.
http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm
====================================================


More information about the Rcpp-devel mailing list