Rcpp project clean and rebuild - c++

I'm trying to build the sample hello world file that comes by default when an Rcpp package project is created in RStudio on Mac.
I'm using latest version of RStudio (Version 1.3.959) and R (4.0.2). The clang version is 11.0.3 and have the command line tools installed.
Unlike the expected compile output of rcpp_hello_world.cpp to create .o objects I see that in my project, RStudio uses R and tries to byte-compile, avoiding the C compilation.
I also updated the Makeconf for gnu++17
(CXX = clang++ -mmacosx-version-min=10.13 -std=gnu++17)
I haev installed Rcpp, RcppEigen and BH packages and created a new Rcpp project and I updated the NAMESPACE file to recognize the Rcpp tags for interface functions.
Here is the output I got:
==> Rcpp::compileAttributes()
* Updated R/RcppExports.R
==> R CMD INSTALL --preclean --no-multiarch --with-keep.source RcppPackage003a
* installing to library ‘/Users/username/Library/R/4.0/library’
* installing *source* package ‘RcppPackage003a’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘RcppPackage003a’ in library.dynam(lib, package, package.lib):
shared object ‘RcppPackage003a.so’ not found
Error: loading failed
Execution halted
Any pointers on what I could be doing wrong and why it's not trying to compile the src files?
As a quick Rcpp install test I was able to call a C++ function from R (referring this doc - http://heather.cs.ucdavis.edu/Rcpp.pdf)
by sourcing a test cpp file with following function -
#include <Rcpp.h>
// [[Rcpp::export]]
void hello()
{
Rprintf("Hello, world!\n");
}
and then in R console calling the hello function after sourcing the cpp file:
library(Rcpp)
sourceCpp("hello.cpp")
hello()
How can I debug the missing link where it is not picking up the cpp file to be compiled?
Thanks in advance!

Related

linking of C++ and Python with BOOST; error = rule "Copyright" unknown in module "xml"

I am trying to execute linking of C++ and Python with BOOST.
Using the example from https://www.boost.org/doc/libs/1_63_0/libs/python/doc/html/tutorial/tutorial/hello.html#tutorial.hello.let_s_jam, and issuing bjam at ~/libs/python/example/tutorial/ which contains these files:
hello.cpp hello.py Jamfile
The read-out from the console error is:
/home/benjamin/boost/boost_1_71_0/tools/build/src/tools/types/xml.jam:12:
in load ERROR: rule "Copyright" unknown in module "xml".
Here is what I undertook to solve this problem:
I built boost from boost_1_71_0.tar.bz2 using the ./b2 file in the directory /boost/boost_1_71_0/ which is dated November 14, 2019.
My Ubuntu 18.04 system has a b2 link at /usr/bin/b2 -> bjam, which is older (March 6, 2018) and the bjam file (March 6, 2018).
No other bjam files exist on the system.
From bjam for boost 1.54, I tried:
sudo-apt install bjam
error: E: Package 'bjam' has no installation candidate.
All the programs in the user-config.jam file (e.g., c++, msvc/code, python3) are correct, which is located in my /home/benjamin/boost/boost_1_71_0/tools/build/example/.
For pity's sake and to help me (newbie), please advise.
Looking at error message it seems that error is with file /home/benjamin/boost/boost_1_71_0/tools/build/src/tools/types/xml.jam
I checked with my local installation (which is boost 1.65.1), but I am not able to locate any file with name xml.jam. But I have few other .jam file at location /usr/share/boost-build/src/tools/types/. As example
asm.jam
# Copyright Craig Rodrigues 2005. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
type ASM : s S asm ;
So it seems that your xml.jam either invalid OR few lines should be commented (as in above file).
Try below options.
Check your xml.jam, if there are any content which are not related jam then you should comment out information.
Rename your xml.jam to xml.jam.bkp. This file may not be needed.(Not needed in my case with boost 1.65.1)
Try to install everything from Ubuntu package manager and use that. You can install all boost module by sudo apt install libboost-all-dev. I tried https://github.com/boostorg/python/tree/develop/example code with my local installed boost (1.65.1) from package manager and it work fine.

Can't compile R package

I am the author of https://github.com/akoyabio/rtree. Until recently, I was able to develop the package in RStudio on Windows with no problems. I was able to check and install the package without any error.
Something has changed and I can no longer build the package. In RStudio, "Install and restart" (with --debug) gives the output
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source --debug rtree
Loading C:\Users\kjohnson\Documents\Rprofile.site
processing 'rtree'
a directory
* installing to library 'C:/Program Files/R/Library'
* build_help_types=html
* DBG: 'R CMD INSTALL' now doing do_install()
* created lock directory 'C:/Program Files/R/Library/00LOCK-rtree'
* installing *source* package 'rtree' ...
** backing up earlier installation
** libs
about to run R CMD SHLIB -o rtree.dll RcppExports.cpp rtree.cpp --debug
ERROR: compilation failed for package 'rtree'
* removing 'C:/Program Files/R/Library/rtree'
* restoring previous 'C:/Program Files/R/Library/rtree'
In R CMD INSTALL
Exited with status 1.
I'm looking for help troubleshooting this error. How can I get more details about the failure?
I do have RTools installed, at C:\RTools, and my PATH includes both
C:\Rtools\bin and C:\Program Files\R\R-3.5.3\bin.
Note: I see the same error if I create a new Rcpp project in RStudio and try to build it. Thank you for any suggestions...
I never really found out how to get more visibility into the failure. I did find the cause - it was an anti-virus program called Minerva's Shield. Turning it off allowed me to build again.

R: Error in dyn.load(file, DLLpath = DLLpath, ...)

I have worked now for some time on an R project in my office's desktop computer, but need to carry the scripts with me while on travel. I copied everything into my laptop and did some changes to the code. Alas, when trying to compile (following the exact same procedure I was using in my desktop computer), the following error:
$ R CMD INSTALL --no-multiarch --with-keep.source coala
* installing to library ‘/home/my_pc/R/x86_64-pc-linux-gnu-library/3.3’
* installing *source* package ‘coala’ ...
** libs
make: Nothing to be done for `all'.
installing to /home/my_pc/R/x86_64-pc-linux-gnu-library/3.3/coala/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/home/my_pc/R/x86_64-pc-linux-gnu-library/3.3/coala/libs/coala.so':
/home/my_pc/R/x86_64-pc-linux-gnu-library/3.3/coala/libs/coala.so: undefined symbol: _ZSt24__throw_out_of_range_fmtPKcz
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/my_pc/R/x86_64-pc-linux-gnu-library/3.3/coala’
* restoring previous ‘/home/my_pc/R/x86_64-pc-linux-gnu-library/3.3/coala’
Both computers have the same Ubuntu but different R version. Also, I have to mention that I am working in two libraries at the same time and that the accompanying library update without problems, which makes this error message more puzzling. I have checked answers that may be related to this question, but can't figure out how to apply their solutions:
R: error installing packages UBUNTU - Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object
Got message unable to load shared object stats.so when R starts
Is there a way to solve this problem? Which may be the cause? What is the undefined symbol _ZSt24__throw_out_of_range_fmtPKcz?
I had a similar issue with rgdal on Ubuntu. Re-installing the package was enough to getting it working again.
$ sudo R
> install.packages("rgdal")
> quit()
$ exit
What is .libPaths() giving you as the output?
I encountered the same problem using the latest update today (R 3.3.1 for MacOS X). My R installation seemed to crash after updating it. My solution was simply to remove the lib directories from the file system and reinstall R:
rm -rf /Users/johann/Library/R/3.3/library
sudo rm -rf Library/Frameworks/R.framework/Versions/3.3/Resources/library
Hope this helps...
The problem is the linkage with your g++ stdlib. Different versions of Ubuntu have different versions of g++ stdlib. You cant always copy binaries from one to the other.
You may fix this by recompiling R for the laptop, or using the Ubuntu R packages. e.g. http://packages.ubuntu.com/xenial/r-base
For Mac / Linux
Press command + space
Type terminal and press enter to open terminal. In terminal paste this open /Library/Frameworks/R.framework/Resources/library and press enter. A finder window should open with a lot of folders
Each folder matches an installed R package.
Delete them all, close RStudio, reopen RStudio and install the packages you need with install.packages("dplyr") etc
After I did this, the issue went away

dyn.load error linking a package with Rcpp

I've made an R package with Rcpp, to use the methods of a library I programmed in c++.
I've R running on the last version:
R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04 LTS
I'm executing the following instruction to install my package:
> install.packages("mypackage", repos = NULL)
The package compiles well, I have the .o files of my source code, but in the linking phase I got the error:
* installing *source* package ‘rbdd’ ...
** libs
make: No se hace nada para 'all'.
installing to /home/sergio/R/x86_64-pc-linux-gnu-library/3.2/mypackage/libs
** R
** preparing package for lazy loading
** help
Warning: /home/sergio/R/mypackage/man/mypackage-package.Rd:27: All text must be in a section
Warning: /home/sergio/R/mypackage/man/mypackage-package.Rd:28: All text must be in a section
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/home/sergio/R/x86_64-pc-linux-gnu-library/3.2/mypackage/libs/mypackage.so':
/home/sergio/R/x86_64-pc-linux-gnu-library/3.2/mypackage/libs/mypackage.so: undefined symbol: _ZN4cudd12defaultErrorENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE
Error: loading failed
Ejecución interrumpida
ERROR: loading failed
mypackge.so is created in the src folder of my package.
I've got a Makevars file (in src, too) with the following content:
PKG_CPPFLAGS=-I./buddy-2.5/src -I./cudd-3.0.0/cudd -I./cudd-3.0.0/mtr -I./cudd-3.0.0/cplusplus -I./cudd-3.0.0/dddmp -I./cudd-3.0.0/util -I./cudd-3.0.0 -isystem /usr/include/c++/v1 -std=c++11
PKG_LIBS=-lc++ -L/lib
and my NAMESPACE file has the lines:
useDynLib(mypackage)
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp, evalCpp)
Someone knows how to solve this problem?
I am a little concerned about
PKG_LIBS=-lc++ -L/lib
Did you really mean /lib? If it is your library, a more common place would be /usr/local/lib which is also search by default.
But, and that is a BIG but, you also need to understand what you need to do for ldconfig for the proper setup of libfoo.so, libfoo.so.$MAJOR and so on. I taught myself that many moons ago from a Linux HOWTO.
If and when that is setup right, you can link it to R via Rcpp. Otherwise maybe stick with system libraries, or package-local static libraries. That approach will also make your package more portable.
The problem was I am consumming external libs, and I must compile it and execute ldconfig before compile my R library.

Error in building boost:python code

I've the following boost:python code(gona.cpp).
#include <iostream>
using namespace std;
void say_hello(const char* name) {
cout << "Hello " << name << "!\n";
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}
int main(){
return 0;
}
I have boost 1.47(boost pro) installed in my system(Windows 7 32-bit).
I used Microsoft Visual Studio 2010 to build this and it has been built successfully. But I want to use this in a python code (as an import). I have the following piece of code (setup.py) to build this into a python module.
from distutils.core import setup
from distutils.extension import Extension
setup(name="PackageName",
ext_modules=[
Extension("hello", ["gona.cpp"],
libraries = ["boost_python", "boost"])
])
"gona" is the file C++ file name. I used the following command to build this in the command line.
python setup.py build
After I do this, I get the following error.
>python setup.py build
running build
running build_ext
building 'hello' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c De
cision_Tree.cpp -o build\temp.win32-2.7\Release\gona.o
gona.cpp:9:35: fatal error: boost/python/module.hpp: No such file or di
rectory
compilation terminated.
error: command 'gcc' failed with exit status 1
It seems the boost installation I have in my system works only on Visual studio(as it had been built successfully). I've run other boost programs without problems in Visual studio.
How do I build this as a python module, so that it can be imported in python code?. (Using command line or Visual studio)
Several things here:
You can also build a Python module using VS2010 directly, just set the output path to foo.pyd instead of foo.dll.
setup.py seems to use MinGW, maybe you can convince it to use the working VS2010 setup instead?
You can use "-I" to inform GCC about include paths, see the GCC documentation. I'm not sure how to tell setup.py where it should look for include paths though, but you should be able to locate that documentation easily.
It looks like setup.py is trying to compile as C code ("gcc.exe" instead of "g++.exe"), which could cause further problems.
Your code has a main() function, which is not required in a DLL (binary Python modules are effectively DLLs).