#include <Rcpp11> file not found error - r

I am attempting to use the Rcpp11 package in r from it's source over at:
devtools::install_github("Rcpp11/Rcpp11")
I read here: http://blog.r-enthusiasts.com/2014/05/27/disambiguating-rcpp11-and-rcpp/
that I can use include <Rcpp11> + using namespace Rcpp11 at the head of my .cpp file but I get this error upon sourcing:
egfile.cpp:1:10: fatal error: 'Rcpp11' file not found
#include <Rcpp11>
^
1 error generated.
where egfile.cpp:
#include <Rcpp11>
using namespace Rcpp11;
// Below is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar)
// For more on using Rcpp click the Help button on the editor toolbar
// [[Rcpp::export]]
int timesTwo(int x) {
return x * 2;
}
Any help with this? or should I defer to the // [[Rcpp::plugins(cpp11)]] method ?
Update: using the attributes::sourceCpp()
devtools::install_github("Rcpp11/attributes")
then restart from a clean session:
library(Rcpp11)
attributes::sourceCpp("egfile.cpp") # in home directory.
I get:
In file included from file2c1e303c4ed6.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp11:4:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp.h:238:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:54:1: error: no member named 'Rf_pnorm' in the global namespace
RCPP_DPQ__2(norm,double mean = 0, double sd = 1, mean, sd )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:30:51: note: expanded from macro 'RCPP_DPQ__2'
#define RCPP_DPQ__2(__NAME__,PAR1,PAR2,VAL1,VAL2) RCPP_DPQ(__NAME__,RCPP_ECHO(RCPP_CONCAT(PAR1,PAR2)), RCPP_ECHO(RCPP_CONCAT(VAL1,VAL2)) )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:14:31: note: expanded from macro 'RCPP_DPQ'
decltype(sapply( x, ::Rf_p##__NAME__, VAL, lower, log )) \
~~^
<scratch space>:208:1: note: expanded from here
Rf_pnorm
^
In file included from file2c1e303c4ed6.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp11:4:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp.h:238:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:54:1: error: no member named 'Rf_pnorm' in the global namespace
RCPP_DPQ__2(norm,double mean = 0, double sd = 1, mean, sd )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:30:51: note: expanded from macro 'RCPP_DPQ__2'
#define RCPP_DPQ__2(__NAME__,PAR1,PAR2,VAL1,VAL2) RCPP_DPQ(__NAME__,RCPP_ECHO(RCPP_CONCAT(PAR1,PAR2)), RCPP_ECHO(RCPP_CONCAT(VAL1,VAL2)) )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:16:29: note: expanded from macro 'RCPP_DPQ'
return sapply( x, ::Rf_p##__NAME__, VAL, lower, log ) ; \
~~^
<scratch space>:208:1: note: expanded from here
Rf_pnorm
^
In file included from file2c1e303c4ed6.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp11:4:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp.h:238:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:54:1: error: no member named 'Rf_qnorm' in the global namespace
RCPP_DPQ__2(norm,double mean = 0, double sd = 1, mean, sd )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:30:51: note: expanded from macro 'RCPP_DPQ__2'
#define RCPP_DPQ__2(__NAME__,PAR1,PAR2,VAL1,VAL2) RCPP_DPQ(__NAME__,RCPP_ECHO(RCPP_CONCAT(PAR1,PAR2)), RCPP_ECHO(RCPP_CONCAT(VAL1,VAL2)) )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:20:31: note: expanded from macro 'RCPP_DPQ'
decltype(sapply( x, ::Rf_q##__NAME__, VAL, lower, log )) \
~~^
<scratch space>:208:1: note: expanded from here
Rf_qnorm
^
In file included from file2c1e303c4ed6.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp11:4:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp.h:238:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:54:1: error: no member named 'Rf_qnorm' in the global namespace
RCPP_DPQ__2(norm,double mean = 0, double sd = 1, mean, sd )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:30:51: note: expanded from macro 'RCPP_DPQ__2'
#define RCPP_DPQ__2(__NAME__,PAR1,PAR2,VAL1,VAL2) RCPP_DPQ(__NAME__,RCPP_ECHO(RCPP_CONCAT(PAR1,PAR2)), RCPP_ECHO(RCPP_CONCAT(VAL1,VAL2)) )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp11/include/Rcpp/stats/stats.h:22:29: note: expanded from macro 'RCPP_DPQ'
return sapply( x, ::Rf_q##__NAME__, VAL, lower, log ) ; \
~~^
<scratch space>:208:1: note: expanded from here
Rf_qnorm
^
4 errors generated.
make: *** [file2c1e303c4ed6.o] Error 1
Error in dyn.load(basename(dynlib)) :
unable to load shared object '/private/var/folders/77/9nq5hj5d37b1rhbzdt0z3twr0000gn/T/RtmpcjWOsg/file2c1e303c4ed6.so':
dlopen(/private/var/folders/77/9nq5hj5d37b1rhbzdt0z3twr0000gn/T/RtmpcjWOsg/file2c1e303c4ed6.so, 6): image not found
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB 'file2c1e303c4ed6.cpp'' had status 1

Presumably, you are trying to compile it from R Studio, which will use Rcpp::sourceCpp. You need the implementation of sourceCpp from the attributes package to generate the glue code for Rcpp11. attributes is available from github:
devtools::install_github("Rcpp11/attributes")
Then use this version to compile your file, i.e.
attributes::sourceCpp( "yourfile.cpp" )
Compiling an Rcpp11 file using Rstudio ui is something we have not yet dealt with, but something we identified as needed here

Related

Accessing an external variable from a C library

I am currently learning C and am trying to understand the possibilities of dynamic libraries.
My current question is, if I have a simple "Hello World" application in C called "ProgA", and this program dynamically loads a shared library with some example code called "LibB", can LibB access a global variable in ProgA, which was declared as external?
Given is the following example code for demonstration of the problem:
file header.h
#ifndef TEST_H
#define TEST_H
typedef struct test_import_s {
int some_field;
} test_import_t;
extern test_import_t newtestimport;
#endif
file prog_a.c
#include <stdio.h>
#include <windows.h>
#include "header.h"
test_import_t newtestimport = {
.some_field = 42
};
int main()
{
HINSTANCE hinstLib;
typedef void (*FunctionPointer)();
newtestimport.some_field = 42;
hinstLib = LoadLibrary("lib_b.dll");
if (hinstLib != NULL)
{
FunctionPointer initialize_lib_b;
initialize_lib_b = (FunctionPointer)GetProcAddress(hinstLib, "initialize_lib_b");
if (initialize_lib_b != NULL)
{
initialize_lib_b();
}
FreeLibrary(hinstLib);
}
return 0;
}
file lib_b.c
#include <stdio.h>
#include "header.h"
test_import_t *timp;
void initialize_lib_b() {
timp = &newtestimport;
int some_field = timp->some_field;
printf("Result from function: %d\n", some_field);
}
file CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(dynamic-library-2 C)
set(CMAKE_C_STANDARD 23)
add_library(lib_b SHARED lib_b.c)
set_target_properties(lib_b PROPERTIES PREFIX "" OUTPUT_NAME "lib_b")
add_executable(prog_a prog_a.c)
target_link_libraries(prog_a lib_b)
In the above example, the headerfile header.h defines the struct test_import_t and an external variable newtestimport using this struct. In the C file of the main program prog_a.c one property of this struct is assigned the value 42. It then dynamically loads the library lib_b.c using the Windows API and executes a function in it. The function then should access the variable newtestimport of the main program and print out the value of the variable (42).
This example does not work. The compiler throws the following error:
====================[ Build | prog_a | Debug ]==================================
C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\cmake\win\x64\bin\cmake.exe --build C:\Users\user1\projects\learning-c\cmake-build-debug --target prog_a -j 9
[1/2] Linking C shared library dynamic-library-2\lib_b.dll
FAILED: dynamic-library-2/lib_b.dll dynamic-library-2/liblib_b.dll.a
cmd.exe /C "cd . && C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\mingw\bin\gcc.exe -fPIC -g -Wl,--export-all-symbols -shared -o dynamic-library-2\lib_b.dll -Wl,--out-implib,dynamic-library-2\liblib_b.dll.a -Wl,--major-image-version,0,--minor-image-version,0 dynamic-library-2/CMakeFiles/lib_b.dir/lib_b.c.obj -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\mingw\bin/ld.exe: dynamic-library-2/CMakeFiles/lib_b.dir/lib_b.c.obj:lib_b.c:(.rdata$.refptr.newtestimport[.refptr.newtestimport]+0x0): undefined reference to `newtestimport'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
How can the example be fixed to accomplish the described goal?
Windows DLLs are self-contained, and can not have undefined references similar to newtestimport, unless these references are satisfied by another DLL.
How can the example be fixed to accomplish the described goal?
The best fix is to pass the address of newtestimport into the function that needs it (initialize_lib_b() here).
If for some reason you can't do that, your next best option is to define the newtestimport as a dllexport variable in another DLL, e.g. lib_c.dll.
Then both the main executable and lib_b.dll would be linked against lib_c.lib, and would both use that variable from lib_c.dll.
P.S. Global variables are a "code smell" and a significant source of bugs. You should avoid them whenever possible, and in your example there doesn't seem to be any good reason to use them.

qmake variable reference in project file

I am trying to use qmake to include all files in a directory (this project is an external subversion project with hundreds of files). I am using qmake version 3.1.
What I tried was something like:
server_files = $$files($$PWD/server)
SOURCES += server_files(*.cpp, true)
The first line does not give any error but the second line gives:
:-1: warning: Failure to find: server_files(*.cpp,
:-1: warning: Failure to find: true)
Putting a $ sign in front of the variable as SOURCES += $server_files(*.cpp, true) gives the same error.
The following example function takes a variable name as its only argument, extracts a list of values from the variable with the eval() built-in function, and compiles a list of files:
defineReplace(headersAndSources) {
variable = $$1
names = $$eval($$variable)
headers =
sources =
for(name, names) {
header = $${name}.h
exists($$header) {
headers += $$header
}
source = $${name}.cpp
exists($$source) {
sources += $$source
}
}
return($$headers $$sources)
}
Variable Processing Functions

qmake: Nested function call in .pro files

Given the following code contained in a qmake project file:
QWT_LIB_BINARY=/usr/lib/libqwt-qt4.so
TEMP1 = $$basename(QWT_LIB_BINARY)
TEMP2 = $$replace(TEMP1, .so, )
QWT_LIB_NAME = $$replace(TEMP2, lib, )
message(QWT library name = $$QWT_LIB_NAME)
Eliminates the path, the 'lib' prefix and the '.so' suffix of the string
contained in the QWT_LIB_BINARY variable. When compiling, the 'make' command
outputs the following line:
Project MESSAGE: QWT library name = qwt-qt4
When the temporary variable assignations are eliminated, like this:
QWT_LIB_BINARY=/usr/lib/libqwt-qt4.so
QWT_LIB_NAME = $$replace($$replace($$basename(QWT_LIB_BINARY), .so, ), lib, )
message(QWT library name = $$QWT_LIB_NAME)
The 'make' command outputs the message:
Project MESSAGE: QWT library name =
Indicating that the last code is not working correctly. Is it possible to
nest function calls in '.pro' files in some other way, so it can work?

Compilation issue with sitmo prng, c++11, and armadillo

I'm trying to compile the sitmo prng under C++11 within an R package. The problematic code has been packaged and is available here. The objective of this R package is to make available the sitmo header file so that other packages are able to use the LinkTo field within description. As an added bonus, the package is scheduled to ship with an Armadillo + OpenMP example. There is one other package, mvnfast, that uses sitmo, but only under c++98 and boost headers.
I believe that the error which I am receiving is specific to OS X and clang. I haven't been able to replicate it on Windows via win-build. With that being said, the error is:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: error: non-type template argument is not a constant expression
const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
The error has only popped up on the Rcpp dev list. The resolution in this case was to compile under C++98 and use boost.
The above error is followed by the following notes:
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3773:18: note: in instantiation of function template specialization 'std::__1::generate_canonical<double, 53, sitmo::prng_engine>' requested here
* _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3737:17: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
{return (*this)(__g, __p_);}
^
sitmo_test.cpp:77:26: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
double u = distunif(engine);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: note: non-constexpr function 'max' cannot be used in a constant expression
const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
^
../inst/include/prng_engine.hpp:100:23: note: declared here
static result_type (max)() { return 0xFFFFFFFF; }
The version of clang being used is:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
Looking into the code, there is a bug in the sitmo prng_engine.h. min() and max() were declared as
static result_type (min)() { return 0; }
static result_type (max)() { return 0xFFFFFFFF; }
If you take a look at, say, standard LCG max from here, you could see that it is declared constexpr, ditto for min.
As soon as you make those methods constexpr in the sitmo header file, I believe you could use them in template expression.
UPDATE
I've looked into GCC 5 headers, methods indeed are declared constexpr

Rcpp - Compile a C++ file with multiple functions

I have just quick question about how to compile a .cpp file with multiple function in it.
Let's say I have c++ file like this : Functions.cpp
#include <Rcpp.h>
#include <math.h> /* pow */
using namespace Rcpp;
// [[Rcpp::export]]
int Function1 (int N, NumericMatrix W){
return ....
}
// [[Rcpp::export]]
int Function2 (int N, NumericMatrix W){
return ....
}
and then I would like to compile the file and be able to to call both functions.
I have done it, but what happens is when I try to load the Functions.o it gives me this":
Error in inDL(x, as.logical(local), as.logical(now), ...) :
unable to load shared object 'C:/Users/Functions.o':
LoadLibrary failure: %1 is not a valid Win32 application.
Anybody , any idea ?
Just use sourceCpp("nameofyourfile.cpp")
If you you have no syntax error preventing compilation both Function1 and Function2 will be accessible in your R session.
Literally thousands of such files have been written, and almost one hundred are at your disposal over at the Rcpp Gallery.

Resources