[Rd] Call C code from R
Oleg Sklyar
osklyar at ebi.ac.uk
Mon Sep 17 10:35:31 CEST 2007
1) You include libraries just like you would always do. Here is the
example from my package that uses external libraries of ImageMagick and
GTK, standard libraries (stdio, pthread) and R libraries:
#include <R.h>
#include <Rdefines.h>
#include <R_ext/Rdynload.h>
#include <R_ext/Error.h>
#include <R_ext/Memory.h>
#include <magick/ImageMagick.h>
#include <stdio.h>
#ifndef WIN32
# include <pthread.h>
#endif
/* These are to use GTK */
#ifdef USE_GTK
# include <gtk/gtk.h>
# ifdef WIN32
typedef unsigned long ulong;
# include <sys/types.h>
# else
# include <gdk/gdkx.h>
# endif
#endif
2) The above is C code and no namespaces are used, however the piece
below is C++ code using namespaces and STL. With C++ you only need to
make sure that you export your function as C: extern "C" {}:
#include <R.h>
#include <Rdefines.h>
#include <R_ext/Rdynload.h>
#include <R_ext/Error.h>
#include <R_ext/Memory.h>
#include <magick/ImageMagick.h>
/* list of STL, C++ */
#include <list>
#define BG 0.0
struct TheSeed {
int index, seed;
};
typedef std::list<int> IntList;
typedef std::list<TheSeed> SeedList;
Oleg
On Mon, 2007-09-17 at 09:14 +0700, Ольга К. Камнева wrote:
> Hello, All!
>
> I'm new for R-devel list. And I'd like to ask some questions,
> maybe they will be stuped for the most part of members of the
> list.
> I need to call function which is written in C++ from R.
> My questions are:
> 1. How should I include libraries (for example, iomanip,
> sstream, iostream)?
> 2. Can I use namespace?
>
> Thanks All :)
> Olga
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
--
Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466
More information about the R-devel
mailing list