Register C++ version of Rcpp function and use it within the other Rcpp function in a new package - rcpp

I have a package hpa that has some functions written in Rcpp. I want to use some of these functions within my new R-package. It is necesseraly to use this functions in "Rcpp form" in order to avoid perfomance penalty so I can't to export them in a usual way.
I have found a similar questions. Following the answer of Dirk Eddelbuettel I have investigated this file and written the following code (within init.c file that has been placed in the src folder) in hope to make polynomialIndex function (from hpa package) become available (in Rcpp i.e. C++ form) for Rcpp functions of other pacakges (including my new one):
#include <Rconfig.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include "polynomialIndex.h"
/* definition of functions provided for .Call() */
static const R_CallMethodDef callMethods[] = {
{ "polynomialIndex", (DL_FUNC) &polynomialIndex, 1 },
{ NULL, NULL, 0 }
};
/* functions being called when package is loaded -- used to register */
/* the functions we are exporting here */
void R_init_RApiSerialize(DllInfo *info) {
/* used by external packages linking to internal serialization code from C */
R_RegisterCCallable("hpa", "polynomialIndex",
(DL_FUNC) &polynomialIndex);
R_registerRoutines(info,
NULL, /* slot for .C */
callMethods, /* slot for .Call */
NULL, /* slot for .Fortran */
NULL); /* slot for .External */
R_useDynamicSymbols(info, TRUE); /* controls visibility */
}
Unfortenatelly when I am trying to build hpa package the following error message arise:
/mingw64/bin/gcc -I"C:/R/R-41~1.0/include" -DNDEBUG -I'C:/R/R-4.1.0/library/Rcpp/include' -I'C:/R/R-4.1.0/library/RcppArmadillo/include' -I'C:/R/R-4.1.0/library/RcppParallel/include' -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c init.c -o init.o
In file included from C:/R/R-4.1.0/library/Rcpp/include/Rcpp/r/headers.h:66,
from C:/R/R-4.1.0/library/Rcpp/include/RcppCommon.h:30,
from C:/R/R-4.1.0/library/RcppArmadillo/include/RcppArmadilloForward.h:26,
from C:/R/R-4.1.0/library/RcppArmadillo/include/RcppArmadillo.h:31,
from polynomialIndex.h:4,
from init.c:4:
C:/R/R-4.1.0/library/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: cmath: No such file or directory
#include <cmath>
^~~~~~~
compilation terminated.
make: *** [C:/R/R-41~1.0/etc/x64/Makeconf:238: init.o] Error 1
It seems to be associated with the fact that polynomialIndex.h file includes <RcppArmadillo.h>. The file itself looks as follows:
#ifndef hpa_polynomialIndex_H
#define hpa_polynomialIndex_H
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace RcppArmadillo;
NumericMatrix polynomialIndex(NumericVector pol_degrees,
bool is_validation);
String printPolynomial(NumericVector pol_degrees,
NumericVector pol_coefficients,
bool is_validation);
#endif
Then I have tried to remove polynomialIndex.h from init.c and to declare polynomialIndex directly inside init.c file. Unfortunatelly it results into the other error message:
/mingw64/bin/gcc -I"C:/R/R-41~1.0/include" -DNDEBUG -I'C:/R/R-4.1.0/library/Rcpp/include' -I'C:/R/R-4.1.0/library/RcppArmadillo/include' -I'C:/R/R-4.1.0/library/RcppParallel/include' -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c init.c -o init.o
** using staged installation
** libs
/mingw64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o hpa.dll tmp.def ParallelFunctions.o RcppExports.o hpaBinary.o hpaML.o hpaMain.o hpaSelection.o hpaValidation.o init.o normalMoments.o polynomialIndex.o spline.o -LC:/R/R-41~1.0/bin/x64 -lRlapack -LC:/R/R-41~1.0/bin/x64 -lRblas -lgfortran -lm -lquadmath -LC:/R/R-41~1.0/bin/x64 -lR
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: init.o:init.c:(.text+0x8): undefined reference to `polynomialIndex'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: init.o:init.c:(.rdata+0x28): undefined reference to `polynomialIndex'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: init.o:init.c:(.rdata$.refptr.polynomialIndex[.refptr.polynomialIndex]+0x0): undefined reference to `polynomialIndex'
Please help me to figure out these problems. In the end I want to be able to use polynomialIndex function within my new package. According to the information I have found it should look something like this (simplifed expample):
// [[Rcpp::depends(hpa)]]
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector newFunc(NumericVector pol_degrees,
bool is_validation) {
static SEXP(*polynomialIndex)(Rcpp::NumericVector, bool) = NULL;
polynomialIndex = (SEXP(*)(Rcpp::NumericVector, bool)) R_GetCCallable("hpa", "polynomialIndex");
NumericVector result = polynomialIndex(pol_degrees, is_validation);
return(result);
}
P.S. This question is also similar to this one but does not resolve my problem.

This has been discussed before, and I would urge you to study these other questions and experiment with their answers.
Here you appear to have a cross-over from a C compilation (started by gcc) touching a file in which you crossed over to C++. Which, plainly stated, you cannot do for C compilation.
R offers C interfaces only. Rcpp helps you by autogenerating complying interface files. If you want to extend/alter them you have to play by the rules.
Wrapping C++ code in (simpler, more portable, cross-compiler, ...) C interfaces is a decades old trick. You likely find many resources for it.

Following Dirk Eddelbuettel's suggestion I have made additional search concerning this problem. Unfortunatelly I have found an extremely simple solution.
First, I have deleted init.c file.
Second, I have added the following line of code to all .cpp files of hpa package.
// [[Rcpp::interfaces(r, cpp)]]
Third I have added the following code to the description file of my new package in order to link it to hpa package (I have made it before the solution has been found but in order to provide a full reciepe it seems reasonable to add this information).
Imports: Rcpp (>= 1.0.6), hpa (>= 1.2.1)
Depends: hpa (>= 1.2.1)
LinkingTo: Rcpp, RcppArmadillo, hpa
Fourth, by the same reasons I have added the following line of code to the namespace file:
import(hpa)
Fifth, I have compiled hpa package (as usual) and have found a new folder named inst. Inside this folder there is a folder include. In this folder I have found a new file hpa.h. So I have simply included this file into the code and simplified the call to the function to hpa::polynomialIndex(...). So the resulting (succesfully working!) code is as follows:
// [[Rcpp::depends(hpa)]]
#include <RcppArmadillo.h>
#include <hpa.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector newFunc(NumericVector pol_degrees,
bool is_validation) {
NumericVector result = hpa::polynomialIndex(pol_degrees, is_validation);
return(result);
}
P. S. It was informative to investigate the file hpa\inst\include\hpa_RcppExports.h to understand how does [[Rcpp::interfaces(r, cpp)]] resolves the problem.

Related

Updating to new ubuntu server breaks g++ build with a bunch of c++/9 errors?

We are moving to a new Ubuntu server (newer Ubuntu version) and the old build is not working. We get tons of errors from c++/9. A couple examples below. Any idea how I get the build (a single .cpp file) to work using g++?
/usr/include/c++/9/array: In function 'bool std::operator<(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)':
/usr/include/c++/9/array:264:19: error: 'lexicographical_compare' is not a member of 'std'; did you mean 'lexicographical_compare'?
264 | return std::lexicographical_compare(__a.begin(), __a.end(),
| ^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/memory:62,
from /usr/include/unicode/localpointer.h:45,
from /usr/include/unicode/uenum.h:23,
from /usr/include/unicode/ucnv.h:53,
from /usr/include/libxml2/libxml/encoding.h:31,
from /usr/include/libxml2/libxml/parser.h:810,
from /usr/include/libxml2/libxml/xmlerror.h:10,
from /usr/include/libxml2/libxml/xpath.h:26,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-xml.h:27,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-env.h:29,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-client.h:27,
from myapp.cpp:6318:
/usr/include/c++/9/bits/stl_algobase.h:1277:5: note: 'lexicographical_compare' declared here
1277 | lexicographical_compare(_II1 __first1, _II1 __last1,
| ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/array: In member function 'void std::array<_Tp, _Nm>::fill(const value_type&)':
/usr/include/c++/9/array:117:14: error: 'fill_n' is not a member of 'std'; did you mean 'fill_n'?
117 | { std::fill_n(begin(), size(), __u); }
| ^~~~~~
In file included from /usr/include/c++/9/memory:62,
from /usr/include/unicode/localpointer.h:45,
from /usr/include/unicode/uenum.h:23,
from /usr/include/unicode/ucnv.h:53,
from /usr/include/libxml2/libxml/encoding.h:31,
from /usr/include/libxml2/libxml/parser.h:810,
from /usr/include/libxml2/libxml/xmlerror.h:10,
from /usr/include/libxml2/libxml/xpath.h:26,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-xml.h:27,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-env.h:29,
from /usr/local/include/libcsoap-1.1/libcsoap/soap-client.h:27,
from myapp.cpp:6318:
I setup a test.cpp and included the same headers as myapp.cpp and it worked. Weird, so I then tried a few #define items included in myapp.cpp and tried again, worked. Now in myapp.cpp the include files for the soap-client.h was down in the middle of the code for CGI support. I moved the #include for that up to the top with the other #include items and it compiled fine.
My only thought is there must have been something in the code, either a function, typedef or #define that conflicted with some of the standard headers?
Anyway, if you run in to something like that, here is one thing to check.

Error: conflicting types when trying to make a simple syscall

I'm brand new to Linux programming and I'm trying to implement a simple system call loosely following this guide: https://medium.com/anubhav-shrimal/adding-a-hello-world-system-call-to-linux-kernel-dad32875872. In my linux kernel directory, I created a new directory called my_syscall. Within that directory, I created my_syscall.c. Here is my_syscall.c
#include <linux/syscalls.h>
#include <linux/kernel.h>
asmlinkage long sys_my_syscall(int i) {
prink(KERN_INFO "This is the system call.");
return(0);
}
I then created a Makefile in the my_syscall directory with a single line:
obj-y := my_syscall.o
I then edited this line in the Makefile in the kernel directory to be:
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ my_syscall/
Then, in the directory linux-5.4.15/arch/x86/entry/syscalls, I edited the syscall_64.tbl to include the following line at the very end:
548 64 my_syscall sys_my_syscall
Finally, in the directory linux-5.4.15/include/linux, I edited the syscalls.h file to include this line before the #endif:
asmlinkage long sys_my_syscall(int i);
Now, when I run the command sudo make, I run into the following error soon after:
./arch/x86/include/generated/asm/syscalls_64.h:2664:19: error: conflicting types for 'sys_my_syscall'
__SYSCALL_64(548, sys_my_syscall, )
arch/x86/entry/syscall_64.c:18:60: note: in definition of macro '__SYSCALL-64'
#define __SYSCALL_64(nr, sym, qual) extern asmlinkage long sym(const struct pt_regs *);
In file included from arch/x86/entry/syscall_64.c:7:0:
./include/linux/syscalls.h:1423:17: note: previous declaration of 'sys_my_syscall' was here
asmlinkage long sys_my_syscall(int i);
^
make[3]: *** [arch/x86/entry/syscall_64.o] Error 1
make[2]: *** [arch/x86/entry] Error 2
make[1]: *** [arch/x86] Error 2
make: *** [sub-make] Error 2
I have no idea how to approach this error. With a conflicting types error, I would think I declared the syscall differently in someplace, but in both my_syscall.c and the syscalls.h files, the declaration is the same. These were the only two files where the syscall is declared, but it is also named within syscall_64.tbl and it seems like this is where linux is trying to point me towards. However, I don't see what's wrong with how I declared it in the table as I followed the guide directly. Any help with this would be greatly appreciated!
Info:
Kernel version: 5.4.15
Linux Distribution: Ubuntu 14
I just changed the location where the syscall number is defined in syscall_64.tbl.
Instead of this:
548 64 my_syscall sys_my_syscall
I wrote this:
436 common my_syscall __x64_sys_my_syscall
Screen Capture of my configuration
It worked out.
I'm doing something similar and got the exact same error.
What fixed the error for me is changing the last part of the syscall_64.tbl table entry from "sys_my_syscall" to "__x64_sys_my_syscall". If you scroll up, other entries have the same prefix. The kernel started compiling after I made that change.
I eventually gave up on trying to implement this in kernel 5. Unfortunately, none of the other solutions resulted in my kernel compiling. I rolled back my kernel and followed the steps here. This resulted in the system call working correctly. I'm not sure how to make this function in kernel 5+.
#include <linux/syscalls.h>
#include <linux/kernel.h>
SYSCALL_DEFINE1(my_syscall, int, i)
{
printk(KERN_INFO "This is the system call (param %d).\n", i);
return(0);
}
For kernel 5, try deleting "sys_" before "my_syscall" and try. It worked for me
Some architectures (including x86-64) use syscall wrappers to call the real syscall handler. To define the real syscall handler and its wrappers (for architectures that use syscall wrappers), use one of the SYSCALL_DEFINE<n> macros before the body of the syscall handler. The parameters of the SYSCALL_DEFINE<n> macros are the function name, followed by <n> pairs of ``type, param'' for the function parameters.
Your sys_my_syscall syscall handler function has one parameter, so use the SYSCALL_DEFINE1 macro before the body of the function:
#include <linux/syscalls.h>
#include <linux/kernel.h>
SYSCALL_DEFINE1(sys_my_syscall, int, i)
{
printk(KERN_INFO "This is the system call (param %d).\n", i);
return(0);
}

How to install tsim-0.84 on Windows 10 with Cygwin?

For a laboration in class we are using an old train simulator for Linux called tsim. I am trying to get this to run on my Windows 10 laptop with Cygwin but I get an error when running the "make" command.
AddToggleWidget.c:3:27: fatal error: X11/Intrinsic.h: No such file or directory
The thing is I think the simulator is so old that it wants to use this X11/Intrinsic.h from an old library called xorg-x11-devel. Because I already have it in a newer one called libXt-devel. This is based on this old cygwin thread.
I have looked everywhere for a way to get the xorg-x11-devel library but can't find it so any help would be greatly appreciated.
The laboration.
The Train Simulator, tsim(source code).
You need to install the libXt-devel package.
To verify if the libXt-devel is properly installed
$ cygcheck -c libXt-devel
Cygwin Package Information
Package Version Status
libXt-devel 1.1.5-1 OK
As it seems that instead your real problem is how to build tsim on cygwin and you are focusing on the wrong issue, I will also provide you the recipe I used.
The following patch is needed for proper build and link:
--- orig/tsim-0.84/src/Makefile.am 2008-09-10 12:45:17.000000000 +0200
+++ tsim-0.84/src/Makefile.am 2016-09-05 22:06:47.665318000 +0200
## -11,5 +11,6 ##
tsim_CFLAGS = -std=gnu99 -pedantic -Wall -Wextra -Wmissing-prototypes \
-DRESDIR=\"${datadir}/tsim\"
-tsim_LDFLAGS = -L/usr/X11R6/lib -lXaw -lXt -lXmu -lX11 -lXext
+tsim_LDFLAGS = -L/usr/lib
+tsim_LDADD = -largp
--- orig/tsim-0.84/src/bitmap.c 2008-09-08 13:14:16.000000000 +0200
+++ tsim-0.84/src/bitmap.c 2016-09-05 22:14:02.032117400 +0200
## -17,14 +17,14 ##
static void GetBitmapPath(
String name,
String full_name) {
- sprintf(full_name, "%s/%s/%s", app_dir(), bitmap_dir(), name);
+ sprintf(full_name, "%s%s/%s", app_dir(), bitmap_dir(), name);
}
void ReadCustomBitmaps(
Widget w) {
char custom_directory[BUFSIZE];
- sprintf(custom_directory, "%s/%s/customBitmaps", app_dir(), bitmap_dir());
+ sprintf(custom_directory, "%s%s/customBitmaps", app_dir(), bitmap_dir());
gBitmapDirectory = BDAddDirectory(w, custom_directory, NULL);
}
--- orig/tsim-0.84/src/pipe.c 2008-09-09 12:55:58.000000000 +0200
+++ tsim-0.84/src/pipe.c 2016-09-05 18:47:40.261754300 +0200
## -3,6 +3,7 ##
#include <stdlib.h> /* exit */
#include <unistd.h> /* read */
#include <sys/param.h> /* MIN, MAX */
+#include <sys/socket.h>
#if defined sun3 || defined sun4
#include <sys/filio.h>
Makefile.am is changed as /usr/X11R6/lib has been replace long time ago by /usr/lib and the libs should be put in LDADD not on LDFLAGS to avoid problem of order durink linking . -largp is missing from the requirement while the the other libs will be correctly added during configure
The changes in bitmap.c are needed to avoid wrongly loading default data. Cygwin have a dedicate meaning for path starting with "//" that are a side effect of current code.
Step by step instruction:
- install libargp-devel, libX11-devel, libXaw-devel, libXext-devel, libXmu-devel, libXt-devel
- unpack the tsim-0.84.tar.gz
- apply the patch
- run autoreconf , it will properly rebuilds the Makefile.in
- run ./configure it will properly rebuilds the Makefile
- run make
- run make install
- run tsim
As bonus, how the program looks:

Rcpp & RInside link error

I am running R3.1 on Kubuntu 14.04 and using Codeblocks as an IDE for trying an RInside program from Dirk Eddelbuettels eg:
http://dirk.eddelbuettel.com/blog/2011/03/25/#rinside_and_qt
`
// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
//
// GPL'ed
#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["txt"] = "Hello, world!\n"; // assign a char* (string) to 'txt'
R.parseEvalQ("cat(txt)"); // eval the init string, ignoring any returns
exit(0);
}
`
and i get the following error:
/usr/bin/ld: obj/Debug/main.o||undefined reference to symbol 'REprintf'|
and I don't know what library is missing. Anyone know?
Thanks
That is the environment I develop on (apart from the difference that I am now on 15.05) which is almost certain to work -- but you got an error indicating that you did not link against libR.
That is almost surely due to you not doing what the README suggests ie for the dozen+ examples in directory examples/standard/ do
make # compile and link all
./rinside_sample0 # run the first
If you insist on using an IDE you can use the contributed cmake/ directory. But the code is tested and released with for use with the (GNU)makefile.

Error on dlclose: "shared objects still referenced"

I have the following error on a dlclose()'d .so: "Shared objects still referenced". I didn't find too much information about it. Did you have such a problem before? What kind of programming errors (or compiling options?) can cause this?
The only thing I tried is to find if MY module has successfully released all of the referenced shared objects, but all of them was ok as I see. At least hopefully. I could not really use LD_DEBUG yet.
(Posting re. discussion in the question's comments.)
The following might trigger that error/warning on QNX. We first need two shared libraries:
shared_needs.c
/* References a function defined elsewhere. */
void defined_elsewhere(void);
void f(void) {
defined_elsewhere();
}
shared_has.c
/* Has the function referenced above. */
void defined_elsewhere(void) {}
We compile them as follows (with sonames and other good practice omitted to keep things simple):
$ gcc -shared -fPIC shared_has.c -o shared_has.so
$ gcc -shared -fPIC shared_needs.c -o shared_needs.so
We now load/unload the libraries with the following code (loader.c),
#include <dlfcn.h>
#include <stdio.h>
int main(void) {
void *shared_has;
/* Pass RTLD_GLOBAL so that defined_elsewhere() becomes
available to libraries loaded later. */
shared_has = dlopen("shared_has.so", RTLD_NOW | RTLD_GLOBAL);
if (shared_has == NULL) {
fprintf(stderr, "%s\n", dlerror());
return 1;
}
/* shared_needs.so resolves defined_elsewhere() from
shared_has.so. */
if (dlopen("shared_needs.so", RTLD_NOW) == NULL) {
fprintf(stderr, "%s\n", dlerror());
return 1;
}
/* Might get warning here on QNX, since defined_elsewhere()
is still used. */
if (dlclose(shared_has) != 0) {
fprintf(stderr, "%s\n", dlerror());
return 1;
}
return 0;
}
, which we compile and run as follows:
$ gcc loader.c -ldl -o loader
$ LD_LIBRARY_PATH=. ./loader
The above might print the warning on QNX. It should be safe though (as it is on Linux), as the dlclose() man page on QNX says the following:
An object won't be removed from the address space until all references to that object (via dlopen() or dependencies from other objects) have been closed.
As a side note, you can run objdump -T shared_needs.so to see that shared_needs.so has an undefined reference to defined_elsewhere() (look for **UND**).
I can't find "Shared objects still referenced" error message in Linux or in opensource libraries. This message only mentioned in context of QNX.
This QT patch https://qt.gitorious.org/qt/qtbase/commit/4af257eb3cfeef93adefda5f981742ffb58ba0ad says that this error is informative and can be ignored
On QNX that's only "informative"
Shared objects still referenced" dlerror should actually be treated as "for your information" only, not as an actual error.
We can say more about error only with additional information:
debug logs (DL_DEBUG - http://www.qnx.com/developers/docs/6.5.0/topic/com.qnx.doc.neutrino_lib_ref/d/dlopen.html)
or the source of QNX recent dl libraries (QNX now is closed-source and there should be no source of it and its libs in the Internet; you should ask current QNX owners for support or sources licensing)

Resources