Discussion:
[Rcpp-devel] Problem with RInside "hello world" example
Michael Hannon
2012-05-22 07:29:43 UTC
Permalink
Greetings.  I'm trying to go through some of the examples in the notes from
Dirk's Rcpp Masterclass of 28 April 2011.  (I didn't take the class.  I just
found the notes on the Internet.  Please let me know if my use of them
violates some law or protocol.  In case it makes any difference, I'm not
making any money from this project.)

I've run into a problem with the RInside "hello world" example.  I can not
get it to compile on my system.  The first of the many error messages is:

    undefined reference to `RInside::RInside(int, char const* const*, bool)

I've appended the details.  I get the same error if I omit the reference to
the c++0x standard.  What am I missing?

Thanks,

-- Mike


$ g++ --version
g++ (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat RI-hw.cpp

#include <RInside.h>              // embedded R via RInside

int main(int argc, char *argv[]) {

    RInside R(argc, argv);        // create embedded R inst.

    R["txt"] = "Hello, world!\n"; // assign  to 'txt' in R

    R.parseEvalQ("cat(txt)");     // eval string, ignore result

    exit(0);
}

$ cat Makefile
RI-hw.exe: RI-hw.cpp
    g++ -std=c++0x                              \
            -o RI-hw.exe                            \
        -I/usr/lib64/R/library/RInside/include/ \
        -I/usr/lib64/R/library/Rcpp/include/    \
        -I/usr/include/R                        \
            RI-hw.cpp

$ make
g++ -std=c++0x                              \
            -o RI-hw.exe                            \
    -I/usr/lib64/R/library/RInside/include/ \
    -I/usr/lib64/R/library/Rcpp/include/    \
    -I/usr/include/R                        \
            RI-hw.cpp
/tmp/ccDoMFpi.o: In function `main':
RI-hw.cpp:(.text+0xbe): undefined reference to `RInside::RInside(int, char
const* const*, bool)'
RI-hw.cpp:(.text+0xf6): undefined reference to
`RInside::operator[](std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
RI-hw.cpp:(.text+0x15f): undefined reference to
`RInside::parseEvalQ(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
RI-hw.cpp:(.text+0x1e0): undefined reference to `RInside::~RInside()'
/tmp/ccDoMFpi.o: In function `Rcpp::wrap(char const*)':
RI-hw.cpp:(.text._ZN4Rcpp4wrapEPKc[Rcpp::wrap(char const*)]+0x16): undefined
reference to `R_NilValue'
RI-hw.cpp:(.text._ZN4Rcpp4wrapEPKc[Rcpp::wrap(char const*)]+0x24): undefined
reference to `Rf_mkString'
/tmp/ccDoMFpi.o: In function `bool Rcpp::Environment::assign<char
[15]>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >
const&, char const (&) [15]) const':
RI-hw.cpp:(.text._ZNK4Rcpp11Environment6assignIA15_cEEbRKSsRKT_[bool
Rcpp::Environment::assign<char [15]>(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&, char const (&) [15])
const]+0x32): undefined reference to
`Rcpp::Environment::assign(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, SEXPREC*) const'
collect2: ld returned 1 exit status
make: *** [RI-hw.exe] Error 1
Darren Cook
2012-05-22 07:40:13 UTC
Permalink
I've run into a problem with the RInside "hello world" example. I can not
undefined reference to `RInside::RInside(int, char const* const*, bool)
You are not telling g++ where to find the R libraries that you need to
link with.

I've a similar basic RInside example and I've got a comment in the
header that says compile it with this:

g++ -I/usr/local/lib/R/site-library/Rcpp/include
-I/usr/local/lib/R/site-library/RInside/include -I/usr/share/R/include
-L/usr/lib64/R/lib -lR -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp
-L/usr/local/lib/R/site-library/RInside/lib -lRInside
-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -lRInside
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib main.cpp


You may have to adjust the paths (I'm on ubuntu 10.04, and I think
default locations for all R packages), but it gives you an idea of what
you're missing.

Darren

P.S. In that same directory I've also a rather long Makefile that
describes itself as a "simple Makefile". It doesn't look like I wrote
it; I guess it is from the RInside examples or vignette.
--
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)
Michael Hannon
2012-05-22 08:04:31 UTC
Permalink
Post by Darren Cook
I've run into a problem with the RInside "hello world" > example.  I can
not get it to compile on my system.  The first of the many error
     undefined reference to `RInside::RInside(int, char const* const*, bool)
You are not telling g++ where to find the R libraries that you need to
link with.
I've a similar basic RInside example and I've got a comment in the
g++ -I/usr/local/lib/R/site-library/Rcpp/include
-I/usr/local/lib/R/site-library/RInside/include -I/usr/share/R/include
-L/usr/lib64/R/lib -lR -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp
-L/usr/local/lib/R/site-library/RInside/lib -lRInside
-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -lRInside
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib main.cpp
You may have to adjust the paths (I'm on ubuntu 10.04, and I think
default locations for all R packages), but it gives you an idea of what
you're missing.
Thanks, Darren.  I think that must be the issue.  I'll try adding the
additional include/library directories later today.

For the record, it occurred to me that I didn't include any R, Rcpp, or
RInside information in my original post.  I've appended it below.

-- Mike


$ R --vanilla

R version 2.15.0 (2012-03-30)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-redhat-linux-gnu (64-bit)
Post by Darren Cook
library(help=Rcpp)
[...]
Package:            Rcpp
Title:              Seamless R and C++ Integration
Version:            0.9.9
Post by Darren Cook
library(help=RInside)
[...]
Package:            RInside
Title:              C++ classes to embed R in C++ applications
Version:            0.2.6
Dirk Eddelbuettel
2012-05-22 10:19:57 UTC
Permalink
On 22 May 2012 at 01:04, Michael Hannon wrote:
| Darren Cook <***@dcook.org> wrote:
|
| >> I've run into a problem with the RInside "hello world" > example.  I can
| >> not get it to compile on my system.  The first of the many error
| >> messages is:
| >>
| >>     undefined reference to `RInside::RInside(int, char const* const*, bool)
| >
| > You are not telling g++ where to find the R libraries that you need to
| > link with.

Correct -- the RInside package comes with four example directories.

And each of these four directories has a Makefile. The easiest to start with is

examples/standard/Makefile

as you can simply drop in a new file 'myhelloworld.cpp' (or any other name
you choose) into this directory and say 'make myhelloworld' --- and the
binary 'myhelloworld' will be created for you.

It does this by querying R via Rscript to learn about

headers and libraries for R

headers and libraries for Rcpp

headers and libraries for RInside

and all six components are needed for compiling and linking.

Dirk
--
Dirk Eddelbuettel | ***@debian.org | http://dirk.eddelbuettel.com
Michael Hannon
2012-05-22 19:51:12 UTC
Permalink
Post by Dirk Eddelbuettel
|
| >> I've run into a problem with the RInside "hello world" > example.  I can
| >> not get it to compile on my system.  The first of the many error
| >>
| >>     undefined reference to `RInside::RInside(int, char const* const*, bool)
| >
| > You are not telling g++ where to find the R libraries that you need to
| > link with.
Correct -- the RInside package comes with four example directories.
And each of these four directories has a Makefile.  The easiest to start with
is
   examples/standard/Makefile
as you can simply drop in a new file 'myhelloworld.cpp' (or any other name
you choose) into this directory and say 'make myhelloworld' --- and the
binary 'myhelloworld' will be created for you.
It does this by querying R via Rscript to learn about
   headers and libraries for R
   headers and libraries for Rcpp
   headers and libraries for RInside
and all six components are needed for compiling and linking.
 
Hmm.  Thanks, Dirk.  That Makefile is downright spooky smart (and worth a
study in its own right).  I was unaware of the "RHOME" option for R and the
"shell" function in GNU make.  Given that I had only one *.cpp file in the
directory, I got the binary to build simply by typing "make", but I did check
that "make <filename>" also appears to work.

-- Mike
Dirk Eddelbuettel
2012-05-22 20:11:29 UTC
Permalink
On 22 May 2012 at 12:51, Michael Hannon wrote:
| Dirk Eddelbuettel <***@debian.org> wrote:
|
| > On 22 May 2012 at 01:04, Michael Hannon wrote:
| >| Darren Cook <***@dcook.org> wrote:
| >|
| >| >> I've run into a problem with the RInside "hello world" > example.  I can
| >| >> not get it to compile on my system.  The first of the many error
| >| >> messages is:
| >| >>
| >| >>     undefined reference to `RInside::RInside(int, char const* const*,
| >bool)
| >| >
| >| > You are not telling g++ where to find the R libraries that you need to
| >| > link with.
| >
| >Correct -- the RInside package comes with four example directories.
| >
| >And each of these four directories has a Makefile.  The easiest to start with
| >is
| >
| >   examples/standard/Makefile
| >
| >as you can simply drop in a new file 'myhelloworld.cpp' (or any other name
| >you choose) into this directory and say 'make myhelloworld' --- and the
| >binary 'myhelloworld' will be created for you.
| >
| >It does this by querying R via Rscript to learn about
| >
| >   headers and libraries for R
| >
| >   headers and libraries for Rcpp
| >
| >   headers and libraries for RInside
| >
| >and all six components are needed for compiling and linking.
|  
| Hmm.  Thanks, Dirk.  That Makefile is downright spooky smart (and worth a
| study in its own right). 

:-)

"make" as a language actually won the same ACM Software Systems Awards that
John Chambers won for R (and which Smalltalk, Tcl/Tk, Netscape/The Web,
... won too).

I have learned a few tricks over the years, but that Makefile is pretty
standard (apart from the the 'list all .cpp and derive basenames from it).

| I was unaware of the "RHOME" option for R and the
| "shell" function in GNU make.  Given that I had only one *.cpp file in the
| directory, I got the binary to build simply by typing "make", but I did check
| that "make <filename>" also appears to work.

Yup. You need the latter to make the former (via implicit 'make all') work.

Dirk
--
Dirk Eddelbuettel | ***@debian.org | http://dirk.eddelbuettel.com
Loading...