From ripley at stats.ox.ac.uk  Fri May 24 17:02:33 2013
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 24 May 2013 16:02:33 +0100
Subject: [Rd] Problem with Rboolean in c++ code
In-Reply-To: <20130524165518.Horde.av7PI2EwhY5Rn39WwjJ0HYA@webmailnew.dds.nl>
References: <20130524085315.Horde.kTXxB2EwhY5Rnw5bwgT19rA@webmailnew.dds.nl>
	<519F6CD3.2000407@stats.ox.ac.uk>
	<20130524165518.Horde.av7PI2EwhY5Rn39WwjJ0HYA@webmailnew.dds.nl>
Message-ID: <519F8109.2020406@stats.ox.ac.uk>

On 24/05/2013 15:55, Jan van der Laan wrote:
>
> Thanks. That does the trick. Although I now have to rewrite some other
> stuff, but I have just started so better now than later.
>
> To be honest, a first and even a second read of the r-extensions manual
> did not really make it clear, to me at least, that Rdefines.h is
> preferred above Rinternals.h. Now that I reread it again, I can see a
> slight preference for Rdefines.h, but you really have to read closely.

That's backwards.  Rinternals.h is the definitive version, used by R itself.

>
> Thanks again.
>
> Jan
>
>
> Prof Brian Ripley <ripley at stats.ox.ac.uk> schreef:
>
>> On 24/05/2013 07:53, Jan van der Laan wrote:
>>>
>>> I am trying to use R_RegisterCFinalizerEx to ensure some c++ object is
>>> properly deleted after use. However, I run into some problems when
>>> trying to pass in the third argument which is an Rboolean.  The line
>>> 'R_RegisterCFinalizerEx(p, finalizer, TRUE);' generates the following
>>> error when trying to compile using R CMD SHLIB:
>>>
>>> g++ -I/usr/share/R/include -DNDEBUG      -fpic  -O2 -pipe -g  -c
>>> rboolean.cpp -o rboolean.o
>>> rboolean.cpp: In function ?SEXPREC* create(SEXP)?:
>>> rboolean.cpp:21:46: error: invalid conversion from ?int? to ?Rboolean?
>>> [-fpermissive]
>>> In file included from /usr/share/R/include/Rdefines.h:29:0,
>>>                  from rboolean.cpp:4:
>>> /usr/share/R/include/Rinternals.h:764:6: error:   initializing argument
>>> 3 of ?void R_RegisterCFinalizerEx(SEXP, R_CFinalizer_t, Rboolean)?
>>> [-fpermissive]
>>> make: *** [rboolean.o] Error 1
>>>
>>>
>>> I have managed to reduce the problem to the example below:
>>>
>>> ===== foo.cpp =====
>>> #include <R.h>
>>> #include <Rdefines.h>
>>>
>>> void foo() {
>>>   Rboolean b = TRUE;
>>> }
>>> ==================
>>>
>>> With the extension .cpp this generates the error above, with the
>>> extension .c (don't put both in the same directory; at least not with
>>> the same name) it compiles without errors or warnings.
>>>
>>> When looking at the headers (R_ext/Boolean.h and Rdefines.h) it seems
>>> that TRUE and FALSE are also defined as constants which seems to
>>> conflict with the enum values of Rboolean.
>>>
>>> For now I use the compiler flag -fpermissive (as suggested by g++), but
>>> I suppose this is not acceptable when submitting to CRAN. Am I doing
>>> something wrong? Is there a workaround/solution?
>>
>> Don't use Rdefines.h: use Rinternals.h.
>>
>> Rdefines.h was for compatibility with S code from the 1990s: it is not
>> kept up to date.
>>
>>>
>>> Thanks.
>>>
>>> Jan
>>
>>
>> --
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
>
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Problem with Rboolean in c++ code
Prof Brian Ripley