Re: [Rd] Call C code from R

From: Oleg Sklyar <osklyar_at_ebi.ac.uk>
Date: Mon, 17 Sep 2007 09:35:31 +0100

  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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Mon 17 Sep 2007 - 08:37:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Sep 2007 - 15:11:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.