[Rd] Samples of external code with various compilers?

Mark.Bravington@csiro.au Mark.Bravington@csiro.au
Tue Dec 3 00:02:03 2002

I can remember hitting several issues with Delphi, that might be applicable
to other languages too. From simple to complicated (in Delphi), these are:

how to write a DLL and export procedures (easy).

how to declare parameters (VAR or pointers to arrays; pretty simple).

how arrays of >1 dimension map to R arrays (easy).

what call mode to use for procedures (i.e. stack order and removal of
parameters; I had always used STDCALL with S and R, and then found I was
getting bugs with R 1.6.1. So in desperation I eventually changed this at
random to C-CALL, and things started working again in R-- and continued to
work in S. To my continued puzzlement, actually.)

how to handle & return character strings (The only way I've found to return
a string of unknown length, was to call a Delphi function twice; the first
time it merely returns the number of characters in the string. Then create a
string of spaces of the correct length in R, and explicitly put that string
in a .C call to the Delphi function, which this time can fill in the actual

how to get the Delphi debugger to work on a DLL being called by R. This is
incredibly easy in S but rather difficult in R, because of a quirk to do
with starting directories.

Then there are one or two tricks I've sometimes used: e.g. how to return a
Delphi pointer to R so that a "persistent" Delphi object can be created with
one call to a Delphi function from R, then accessed again on a subsequent
call. Useful even with simple things like returning a string, and essential
with really complex structures. All languages will have their own tricks,
which are well worth some informal documentation somewhere.

And then there is the business of turning the R headers into Delphi
equivalents-- a work in progress.



Mark Bravington
PO Box 1538
Castray Esplanade
TAS 7001

phone (61) 3 6232 5118
fax (61) 3 6232 5012

#-----Original Message-----
#From: Duncan Murdoch [mailto:murdoch@stats.uwo.ca]
#Sent: Tuesday, 3 December 2002 1:15 AM
#To: ripley@stats.ox.ac.uk
#Cc: r-devel@stat.math.ethz.ch
#Subject: Re: [Rd] Samples of external code with various compilers?
#On Mon, 2 Dec 2002 08:07:19 +0000 (GMT), you wrote in message
#>I think there is information and lots of examples already for 
#I was thinking of two additions:
# 1.  Rewriting the samples in sections 4.2 and/or 4.5 in Fortran,
#Delphi, etc.  I might make them a little more elaborate, e.g. showing
#how to return a character string.
# 2.  Write up the details of how to do it in various specific
#compilers.  For example, if you're using Microsoft Visual Fortran, how
#do you create a DLL, how do you set the exported entry points, what
#bugs do you need to work around.
#>Base R has only interfaces for C and Fortran (and that via C-style
#>linkage): SJava adds .Java.  So is the issue how to write in other
#>languages to use a C interface?  `how to dyn.load functions' 
#is easy: you
#>just create an appropriate compiled object, a shared library, 
#a DLL or (on
#>MacOS X as I understand it) a module.  The issues seem to be 
#to export the
#>symbols correctly, and even more to import ones from R correctly.
#Yes, that's the main issue.  There are also issues even with C:  if
#you're using some compiler other than gcc, you probably won't compile
#using R SHLIB, so what do you need to do?
#R-devel@stat.math.ethz.ch mailing list