From osklyar at ebi.ac.uk  Mon Sep 17 10:35:31 2007
From: osklyar at ebi.ac.uk (Oleg Sklyar)
Date: Mon, 17 Sep 2007 09:35:31 +0100
Subject: [Rd] Call C code from R
In-Reply-To: <web-11654315@hermes.nsu.ru>
References: <web-11654315@hermes.nsu.ru>
Message-ID: <1190018131.25747.5.camel@maitai.windows.ebi.ac.uk>

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



[Rd] Call C code from R
Oleg Sklyar