tree.hh doesn't compile on Ubuntu 14.04 - c++

I am using the latest tree.hh from http://tree.phi-sci.com/download.html.
It produces the errors (please see below). How can I fix it? How can this class produce so many errors?
Thanks in advance for your suggesstions?
In file included from item_tree.h:4:0,
from Player.h:44,
from xinemediaplayer.c:26:
tree.hh:82:36: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree(tree<T, tree_node_allocator>&&); // move constructor^M
^
tree.hh:82:38: error: invalid constructor; you probably meant ‘tree<T, tree_node_allocator> (const tree<T, tree_node_allocator>&)’
tree(tree<T, tree_node_allocator>&&); // move constructor^M
^
tree.hh:85:70: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T,tree_node_allocator>& operator=(tree<T, tree_node_allocator>&&); // move assignment^M
^
tree.hh:503:64: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>&& x) ^M
^
tree.hh:503:1: error: prototype for ‘tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>)’ does not match any in class ‘tree<T, tree_node_allocator>’
tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>&& x) ^M
^
tree.hh:81:3: error: candidates are: tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>&)
tree(const tree<T, tree_node_allocator>&); // copy constructor^M
^
tree.hh:80:3: error: tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>::iterator_base&)
tree(const iterator_base&);^M
^
tree.hh:496:1: error: tree<T, tree_node_allocator>::tree(const T&)
tree<T, tree_node_allocator>::tree(const T& x) ^M
^
tree.hh:490:1: error: tree<T, tree_node_allocator>::tree()
tree<T, tree_node_allocator>::tree() ^M
^
tree.hh:562:98: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T,tree_node_allocator>& tree<T, tree_node_allocator>::operator=(tree<T, tree_node_allocator>&& x)^M
^
tree.hh: In member function ‘tree<T, tree_node_allocator>& tree<T, tree_node_allocator>::operator=(tree<T, tree_node_allocator>)’:...
an example of tree.hh:
64 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
65 class tree {
66 protected:
67 typedef tree_node_<T> tree_node;
68 public:
69 /// Value of the data stored at a node.
70 typedef T value_type;
71
72 class iterator_base;
73 class pre_order_iterator;
74 class post_order_iterator;
75 class sibling_iterator;
76 class leaf_iterator;
77
78 tree(); // empty constructor
79 tree(const T&); // constructor setting given element as head
80 tree(const iterator_base&);
81 tree(const tree<T, tree_node_allocator>&); // copy constructor
82 tree(tree<T, tree_node_allocator>&&); // move constructor
83 ~tree();
84 tree<T,tree_node_allocator>& operator=(const tree<T, tree_node_allocator>&); // copy assignment
85 tree<T,tree_node_allocator>& operator=(tree<T, tree_node_allocator>&&); // move assignment
86
87 /// Base class for iterators, only pointers stored, no traversal logic.
88 #ifdef __SGI_STL_PORT
89 class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
90 #else
The Makefile is:
1 PLUGIN = xinemediaplayer
2 OBJS = $(PLUGIN).o Control.o Player.o Reel.o SpuDecode.o Utils.o \
3 curl_download.o imagetools.o \
4 item_tree.o m3u_parser.o pls_parser.o timecounter.o \
5 xineOsd.o xineOsdInfoMenu.o xineOsdFunction.o Playlist.o
6 VDRDIR = ../../..
7 LIBDIR = ../../lib
8 TMPDIR = /tmp
9 -include $(VDRDIR)/Make.config
10 ARCHIVE = $(PLUGIN)-$(VERSION)
11 PACKAGE = vdr-$(ARCHIVE)
12 OBJS += cddb.o cdtext.o cdrom.o
13 OBJS += XineLib.o GstreamerLib.o gstTools.o ExternalLib.o
14 LIBS += -lxine `pkg-config --libs gstreamer-0.10` `pkg-config --libs gstreamer-interfaces-0.10`
15 INCLUDES += `pkg-config --cflags gstreamer-0.10` `pkg-config --cflags gstreamer-interfaces-0.10`
16 -include $(VDRDIR)/Make.common
17 INCLUDES +=
18 INCLUDES +=`taglib-config --cflags`
19 LDFLAGS +=`taglib-config --libs`
20 LIBS += `curl-config --libs` -lcdio -lcddb
21
22 DEFINES += -DPLAYER_VERSION=\"$(PLAYER_VERSION)\" -D__LINUX__ -D__STL_CONFIG_H
23
24 target-for-compatibility-with-vanilla-vdr:
25 $(LIBDIR)/$#.$(APIVERSION)
Make.config:
1 REELVDR=1
2 DEBUG = 1
3 VDRVER ?= 2.1
4 CCACHE := $(shell which ccache)
5 MAKE += -j3
6 LSB_RELEASE := $(shell lsb_release -sr)
7 MACHINE ?= -m32 -march=pentium3 -mmmx -msse -mfpmath=sse
8 CC = $(CCACHE) $(DISTCC) $(CROSS)gcc
9 CFLAGS = -O2 -g -pg $(MACHINE)
10 CXX = $(CCACHE) $(DISTCC) $(CROSS)g++
11 CXXFLAGS = -g3 -funroll-loops -fomit-frame-pointer $(MACHINE) -Wall -Woverloaded-virtual -D_FILE_OFFSET_BITS=64 -D_ LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
12 STRIP = $(CROSS)strip
13 export CC CXX CFLAGS CXXFLAGS
14 CC := $(CC)-4.8
15 CXX := $(CXX)-4.8
16 DEFINES += -DDVBAPI_V5
17 DEFINES += -DNEW_FFMPEG
18 USEMYSQL = 1
19 DEVICE_ATTRIBUTES = 1
20 DVBDIR := /usr/src/linux-headers-3.13.0-66-generic/include/config/dvb
21 VDRVER := 2.1

&& is C++ 11 feature.
You should use --std=c++11 or --std=gnu++11 command line option when making g++ call.

Related

GCC (MingW) + LD generate nearly empty executable. Why?

For a specific requirement, I need my binary to fall below 510bytes.
Doing the program in assembler, I get <100 bytes, while adding same code in C++, the resulting code is over 1K. Looking the resulting binary, mostly all ( + 90%) is full of 0x00 characters (Only a few characters at the beginning and end are really populated).
Currently building commands:
gcc -Wall -Os -s -Wl,--stack,32 -nostdinc -nostdinc++ -fno-ident -fno-builtin -I. -c -o cppFile.coff cppFile.cpp
nasm -f elf -o main.elf main.asm
ld -T link.ld -o out.elf main.elf cppFile.coff
objcopy -O binary out.elf out.bin
Size of files generated:
cppFile.coff = 684 bytes
main.elf = 640
out.elf = 2707
out.bin = 1112
When not linking to the cppFile.coff (with only one empty function)
out.elf = 1984
out.bin = 31 (tested and working)
Why GCC or LD add so many nul charactres?
How to remove this empty space?
ENTRY(start)
phys = 0x7c00;
HEAP_SIZE = 0;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
}
end = .;
}

Undefined reference within static library, although the function is there

I try to compile QDP++ C++ library using the Intel 17 compiler and Intel MPI on an Intel Xeon supercomputer. The dependency QMP C library has already been compiled with that compiler (mpiicc), there were no compilation errors.
When trying to configure QDP++, I get this error from the Intel compiler:
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_comm_get_allocated':
QMP_init.c:(.text+0x55): undefined reference to `QMP_abort_mpi(int)'
And this from GCC 5.4:
~/local-jureca/lib/libqmp.a(QMP_init.o):QMP_init.c:function QMP_comm_get_allocated: error: undefined reference to 'QMP_abort_mpi(int)'
So it is an actual problem and not just something that the Intel C++ with MPI compiler cannot do.
The output of nm ~/local-jureca/lib/libqmp.a contains this stanza where QMP_comm_get_allocated resides in.
QMP_init.o:
U atol
U __ctype_b_loc
U exit
U __gcc_personality_v0
U __gxx_personality_v0
U malloc
00000000000015f0 T QMP_abort
00000000000015c0 T QMP_abort_string
0000000000000010 D QMP_allocated_comm
0000000000000018 d QMP_allocated_comm_s
0000000000000000 D QMP_args
0000000000000000 b QMP_args_s
U QMP_barrier
U QMP_comm_declare_logical_topology_map
0000000000000000 T QMP_comm_get_allocated
0000000000001760 T QMP_comm_get_default
00000000000000e0 T QMP_comm_get_job
U QMP_comm_get_logical_coordinates
U QMP_comm_logical_topology_is_declared
0000000000000070 T QMP_comm_set_allocated
00000000000016f0 T QMP_comm_set_default
0000000000000150 T QMP_comm_set_job
U QMP_comm_split
0000000000000048 D QMP_default_comm
U QMP_error
00000000000001c0 T QMP_finalize_msg_passing
U QMP_get_allocated_dimensions
U QMP_get_allocated_number_of_dimensions
U QMP_get_msg_passing_type
0000000000001620 T QMP_init_msg_passing
0000000000000040 D QMP_job_comm
0000000000000008 D QMP_machine
0000000000000048 b QMP_machine_s
0000000000000080 B QMP_stack_level
U __svml_idiv4
0000000000000000 r __$U0
0000000000000017 r __$U1
000000000000002e r __$U2
000000000000003f r __$U3
0000000000000091 r __$U4
000000000000007c r __$U5
0000000000000050 r __$U6
000000000000005a r __$U7
0000000000000067 r __$U8
U _Unwind_Resume
0000000000000500 t _Z12process_argsPiPPPc
000000000000002c r _Z12process_argsPiPPPc$$LSDA
U _Z13QMP_abort_mpii
U _Z20QMP_init_machine_mpiPiPPPc16QMP_thread_levelPS3_
U _Z28QMP_finalize_msg_passing_mpiv
00000000000001e0 t _Z9get_colorv
0000000000000000 r _Z9get_colorv$$LSDA
Another stanza contains the function QMP_abort_mpi which supposedly cannot be found:
QMP_init_mpi.o:
U malloc
U MPI_Abort
U MPI_Comm_dup
U MPI_Comm_rank
U MPI_Comm_size
U MPI_Finalize
U MPI_Get_processor_name
U MPI_Init_thread
0000000000000010 T QMP_abort_mpi
U QMP_abort_string
U QMP_allocated_comm
0000000000000000 T QMP_finalize_msg_passing_mpi
0000000000000020 T QMP_init_machine_mpi
U QMP_machine
The relevant output of `configure is:
configure: Parscalar build! Checking for QMP
checking for qmp-config... ~/local-jureca/bin/qmp-config
configure: Found QMP configuration program ~/local-jureca/bin/qmp-config
configure: QMP compile flags: -I~/local-jureca/include
configure: QMP linking flags: -L~/local-jureca/lib
configure: QMP libraries flags: -lqmp
checking if we can compile/link of a simple QMP C++ program... no
configure: error: Cannot compile/link a basic QMP C++ program!
Check QMP_CFLAGS, QMP_LDFLAGS, QMP_LIBS.
So it seems to find the library just fine.
In the config.log a little larger chunk is this:
configure:4227: /usr/local/software/jureca/Stages/2016b/software/impi/2017.0.098-iccifort-2017.0.098-GCC-5.4.0/bin64/mpiicpc -o conftest -O2 -Wall -fopenmp --std=c++11 -I~/local-jureca/include -L~/local-jureca/lib conftest.cpp -lqmp >&5
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_comm_get_allocated':
QMP_init.c:(.text+0x55): undefined reference to `QMP_abort_mpi(int)'
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_comm_set_allocated':
QMP_init.c:(.text+0xc7): undefined reference to `QMP_abort_mpi(int)'
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_comm_get_job':
QMP_init.c:(.text+0x135): undefined reference to `QMP_abort_mpi(int)'
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_comm_set_job':
QMP_init.c:(.text+0x1a7): undefined reference to `QMP_abort_mpi(int)'
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `QMP_finalize_msg_passing':
QMP_init.c:(.text+0x1cf): undefined reference to `QMP_finalize_msg_passing_mpi()'
~/local-jureca/lib/libqmp.a(QMP_init.o): In function `get_color()':
QMP_init.c:(.text+0x29b): undefined reference to `QMP_abort_mpi(int)'
QMP_init.c:(.text+0x45c): undefined reference to `QMP_abort_mpi(int)'
QMP_init.c:(.text+0x4be): undefined reference to `QMP_abort_mpi(int)'
configure:4227: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "qdp++"
| #define PACKAGE_TARNAME "qdp--"
| #define PACKAGE_VERSION "1.44.0"
| #define PACKAGE_STRING "qdp++ 1.44.0"
| #define PACKAGE_BUGREPORT "edwards#jlab.org"
| #define PACKAGE_URL ""
| #define PACKAGE "qdp--"
| #define VERSION "1.44.0"
| #define QDP_ND 4
| #define QDP_NC 3
| #define QDP_NS 4
| #define QDP_AC_ALIGNMENT_SIZE 16
| #define QDP_USE_GENERIC_OPTS 1
| #define QDP_USE_BLUEGENEL 1
| #define BASE_PRECISION 64
| #define QDP_USE_CB2_LAYOUT 1
| #define ARCH_PARSCALAR 1
| /* end confdefs.h. */
| #include "qmp.h"
| int
| main ()
| {
|
| int argc ; char **argv ;
| QMP_thread_level_t prv;
| ;
| QMP_init_msg_passing(&argc, &argv, QMP_THREAD_SINGLE, &prv) ;
| ;
| QMP_finalize_msg_passing() ;
|
| ;
| return 0;
| }
configure:4253: checking if we can compile/link of a simple QMP C++ program
configure:4261: result: no
configure:4263: error: Cannot compile/link a basic QMP C++ program!
Check QMP_CFLAGS, QMP_LDFLAGS, QMP_LIBS.
I have replaced the absolute path to my home directory with ~ because the actual path is not interesting.
What happens here? And how can I go about fixing it?
Update 1
This is part of the make output for QMP. It seems to be compiled with mpiicc which is a C (not C++) compiler:
depbase=`echo mpi/QMP_topology_mpi.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
/usr/local/software/jureca/Stages/2016b/software/impi/2017.0.098-iccifort-2017.0.098-GCC-5.4.0/bin64/mpiicc -DHAVE_CONFIG_H -I. -I../include -I../include -O2 -Wall -fopenmp -MT mpi/QMP_topology_mpi.o -MD -MP -MF $depbase.Tpo -c -o mpi/QMP_topology_mpi.o mpi/QMP_topology_mpi.c &&\
mv -f $depbase.Tpo $depbase.Po
And then the static library is linked together from the various units:
rm -f libqmp.a
ar cru libqmp.a QMP_comm.o QMP_error.o QMP_grid.o QMP_init.o QMP_machine.o QMP_mem.o QMP_split.o QMP_topology.o QMP_util.o mpi/QMP_comm_mpi.o mpi/QMP_error_mpi.o mpi/QMP_init_mpi.o mpi/QMP_mem_mpi.o mpi/QMP_split_mpi.o mpi/QMP_topology_mpi.o

error in abline when run via RInside

I'm running into a problem using RInside vs the console. This is all run on ubuntu 14.04 using R 3.2.4 installed via apt-get from CRAN. Here is the c++ and R code:
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R.parseEval("source('abline.R')");
}
abline.R
bp <- data.frame(
age = c(28, 23, 52, 42, 27, 29, 43, 34, 40, 28),
systolic = c(70, 68, 90, 75, 68, 80, 78, 70, 80, 72))
str(bp)
attach(bp)
bp.lm <- lm(systolic ~ age)
plot(age, systolic)
abline(bp.lm)
lines(lowess(age, systolic, f=0.75), lty=2)
The R code works fine from the console, but errors when the program is run.
mlilback#rc2x:/tmp/abtest$ ./abtest
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
Error in if (noInt) { : argument is of length zero
terminate called after throwing an instance of 'std::runtime_error'
what(): Error evaluating: source('abline.R')
Aborted (core dumped)
The if (noInt) { is from the source of abline (line 18 in my version of R). I'm completely stumped as to why this only happens via RInside.
Any ideas?
Works for me without any issues. Ubuntu 16.04. Running out of the examples directory to get the GNUmakefile-based build for free:
~/git/rinside/inst/examples/standard(master)$ vi soquestion.cpp
~/git/rinside/inst/examples/standard(master)$ make soquestion
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
-Wno-unused -Wall soquestion.cpp -Wl,--export-dynamic -fopenmp \
-L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack \
-L/usr/local/lib/R/site-library/RInside/lib -lRInside \
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o soquestion
~/git/rinside/inst/examples/standard(master)$ vi abline.R
~/git/rinside/inst/examples/standard(master)$ ./soquestion
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
~/git/rinside/inst/examples/standard(master)$
I literally just copied an pasted your two files. Also:
~/git/rinside/inst/examples/standard(master)$ ls -1tr | tail -4
soquestion.cpp
soquestion
abline.R
Rplots.pdf
~/git/rinside/inst/examples/standard(master)$
You probably want to open a device file via pdf() or png() ...

Undefined reference to function when linking [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
Trying to make a c++ program splitting a line and creating 2 std::vector to save elements from said line.
This is the function I call:
void readConf(const char *name, std::vector<t_objectLibrary> libs, std::vector<IObject *> &objs, std::vector<IObject *> &timer)
I placed in inside config.hpp which has it's prototype :
void readConf(const char *name, std::vector<t_objectLibrary> libs,std::vector<IObject *> &objs, std::vector<IObject *> timer);
and in the main I call it like this :
int main(int ac, char **av)
{
std::vector<IObject *> objs;
std::vector<IObject *> timer;
std::vector<t_objectLibrary> libs;
libs = lib_read("./src/objectLibrary");
readConf("config.txt", libs, objs, timer);
...
}
yet I get this error message when I compile with the Makefile. Everything is fine until the last message of compilation it says this :
Here is the Makefile I use (Asked in the comments) :
11 │ SRC = src/getvalue.cpp \
12 │ src/iobject.cpp \
13 │ src/attributes.cpp \
14 │ src/dynamic_lib.cpp \
15 │ src/config.cpp \
16 │ src/color.cpp \
17 │ src/main.cpp
18 │
19 │ OBJ = $(SRC:.cpp=.o)
20 │
21 │ NAME = conf
22 │
23 │ CXX = g++ -std=c++11 -Wall -Wextra -lsfml-graphics -lsfml-window -lsfml-system -fPIC
24 │
25 │ RM = rm -f
26 │
27 │ all: $(NAME) $(OBJ)
28 │
29 │ $(NAME): $(OBJ)
30 │ $(CXX) $(OBJ) -o $(NAME) -ldl
31 │
32 │ clean:
33 │ $(RM) $(OBJ)
34 │
35 │ fclean: clean
36 │ $(RM) $(NAME)
37 │
38 │ re: fclean all
39 │
40 │ .phony: all clean fclean re
The function signature in the hpp doesn't exactly match that in the declaration.
For further reading, consider another SO thread.
In your case, perhaps, a typo (check method signature in hpp).
hth

Raising exceptions in Rcpp

I am trying to report errors from my rcpp code. I am using the constructor exception (const char *message_, const char *file, int line) from http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html. To isolate the problem, I wrote the following bar.cpp:
#include <Rcpp.h>
RcppExport SEXP bar( SEXP x){
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
}
When I run it in R, this is what I get:
> dyn.load("bar.so")
> is.loaded("bar")
[1] TRUE
> .Call("bar",12)
Error: SET_VECTOR_ELT() can only be applied to a 'list', not a 'NULL'
>
You can either
use the inline package which puts a try/catch block into the function it generates for you (by using two simple macros)
or do it manually yourself as shown in a bunch of examples on my blog, or in the examples/ section of the Rcpp package,
but doing what you (ie: throwing outside of a try/catch block) can never work.
As an added bonus, here is a complete example (which essentially already existed in the unit tests):
R> library(inline)
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ')
R> f()
Error in f() : boom
R>
Again, cxxfunction() puts a try/catch() block in here for you as you can see if you turn verbose on:
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ', verbose=TRUE)
>> setting environment variables:
PKG_LIBS = -L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib
>> LinkingTo : Rcpp
CLINK_CPPFLAGS = -I"/usr/local/lib/R/site-library/Rcpp/include"
>> Program source :
1 :
2 : // includes from the plugin
3 :
4 : #include <Rcpp.h>
5 :
6 :
7 : #ifndef BEGIN_RCPP
8 : #define BEGIN_RCPP
9 : #endif
10 :
11 : #ifndef END_RCPP
12 : #define END_RCPP
13 : #endif
14 :
15 : using namespace Rcpp;
16 :
17 :
18 :
19 : // user includes
20 :
21 :
22 : // declarations
23 : extern "C" {
24 : SEXP file4cc53282( ) ;
25 : }
26 :
27 : // definition
28 :
29 : SEXP file4cc53282( ){
30 : BEGIN_RCPP
31 :
32 : throw std::range_error("boom");
33 : return R_NilValue;
34 :
35 : END_RCPP
36 : }
37 :
38 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file4cc53282.cpp 2> file4cc53282.cpp.err.txt
ccache g++-4.6 -I/usr/share/R/include \
-I"/usr/local/lib/R/site-library/Rcpp/include" \
-fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file4cc53282.cpp \
-o file4cc53282.o
g++ -shared -o file4cc53282.so file4cc53282.o \
-L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/lib/R/lib -lR
R>
The BEGIN_RCPP and END_CPP add the magic you need here.
Please do move your questions to rcpp-devel.
Just wrap your code inside BEGIN_RCPP/END_RCPP:
RcppExport SEXP bar( SEXP x){
BEGIN_RCPP
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
END_RCPP
}
Note that you can throw normal std exceptions too:
throw std::invalid_argument("'x' is too short");