I'm developing a C++ library and wanted to interface it with both Python and R.
The core idea is that R and Python are basically 'empty shells' used for plotting and not much else and all the core computation is done in C++, so that the interface is just a bunch of functions that run the equivalent of a C++ 'main' and return the output to R or Python instead of saving stuff to file.
My idea was to use goolge's protocol buffer in a server/client fashion: data is passed from Python (R) to C++ using a serialized message, is then deserialized in C++. Computation is run in C++ and to Python (R) is returned a list of bytes/strings corresponding each to a serialized proto object.
In Python, this works like a charm thanks to Pybind11. However, in R I get a weird error:
/usr/include/google/protobuf/arena_impl.h:174:60: error: macro "Free" passed 3 arguments, but takes just 1
void (*block_dealloc)(void*, size_t));
I believe this issue is due to Rcpp defining a similar macro to protobuf. The following MWE reproduces the same error.
This is the fold structure
.
├── src
│ ├── rcpp_exports.cpp
├── student.proto
└── test.R
in test.R
Rcpp::sourceCpp(file="src/rcpp_exports.cpp")
in rcpp_exports.cpp
#include <Rcpp.h>
using namespace Rcpp;
#include "student.pb.h"
// [[Rcpp::export]]
std::string foo() {
Student stud;
stud.set_grade(25);
return stud.DebugString();
}
in student.proto
syntax = "proto3";
message Student {
double grade = 1;
bool pass = 2;
}
the protobuf file is then compiled using
> protoc --proto_path=. --cpp_out=./src student.proto
and finally I run the test.R file
> Rscript test.R
Getting the following error log:
In file included from /usr/include/google/protobuf/arena.h:55:0,
from /home/mario/dev/tesi/test_rproto/src/student.pb.h:24,
from rcpp_exports.cpp:3:
/usr/include/google/protobuf/arena_impl.h:174:60: error: macro "Free" passed 3 arguments, but takes just 1
void (*block_dealloc)(void*, size_t));
^
In file included from /usr/share/R/include/R.h:91:0,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp/r/headers.h:63,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/RcppCommon.h:29,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp.h:27,
from rcpp_exports.cpp:1:
/usr/share/R/include/R_ext/RS.h:74:37: error: expected identifier before ‘(’ token
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
/usr/share/R/include/R_ext/RS.h:74:47: error: ‘parameter’ declared as function returning a function
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
/usr/share/R/include/R_ext/RS.h:74:50: error: expected ‘)’ before ‘,’ token
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
make: *** [rcpp_exports.o] Error 1
I would recommend a few things for defensive programming:
Do not flatten namespaces i.e. remove using namespace Rcpp;
and call your functins explicitly with prefix.
Ask for stringent includes (which we can't retroactively make a default) by defining #define STRICT_R_HEADERS
before including Rcpp.h
or other R headers.
Use R functions without the remap, i.e Rf_error()
by defining R_NO_REMAP
but Rcpp should do that for you.
That should avoid the clash you found here by being a little liberal in what you included and how.
Lastly, we wrote RProtoBuf
(and it actually had a hand in creating what is the current Rcpp
too) well over a decade ago yet I never got that error message you have here. So it clearly can be avoided.