[Rcpp-devel] Shared code in C++ files

Martin Lysy mlysy at uwaterloo.ca
Wed Apr 13 23:15:59 CEST 2016


In the document "Rcpp Attributes: Rcpp version 0.12.3 as of January 10,
2016", Section 2.10.2 explains how to share code between C++ files.  I am
obtaining errors when attempting to compile using the outlined steps on
both Windows and Linux.

Windows:
Platform: x86_64-w64-mingw32/x64 (64-bit)
Version: Windows 8.1
R version 3.2.4 Revised (2016-03-16 r70336) -- "Very Secure Dishes"
Rcpp version 0.12.4

Linux:
Platform: x86_64-pc-linux-gnu (64-bit)
Version: CentOS release 6.7 (Final)
R version: 3.2.2 (2015-08-14) -- "Fire Safety"
(I do not have admin privileges to update R on this machine)
Rcpp version: 0.12.4

Sample code is:

#-----------------------------------------------------------------------------------------------

require(Rcpp)

# header file
header <- "
#ifndef __UTILITIES__
#define __UTILITIES__
double timesTwo(double x);
#endif // __UTILITIES__
"
cat(header, file = "utilities.hpp")

# implementation file
impl <- "
#include \"utilities.hpp\"
double timesTwo(double x) {
  return x * 2;
}
"
cat(impl, file = "utilities.cpp")

# main file
main <- "
#include <Rcpp.h>
using namespace Rcpp;
#include \"utilities.hpp\"
//[[Rcpp::export]]
double TimesTwo(double x) {
  return timesTwo(x);
}
"
cat(main, file = "main.cpp")

# works on linux
sourceCpp(file = "main.cpp")

# does not work on linux
sourceCpp(code = main)

#-----------------------------------------------------------------------------------------------

Neither of the last two commands works for me on Windows.  Attached below
is the output of the failed commands for Windows and Linux (first command
for former, second command for latter).  Can you please indicate what is
wrong with the steps above?

Best regards,

Martin Lysy
Assistant Professor of Statistics
Department of Statistics and Actuarial Science
University of Waterloo

====================================================

WINDOWS OUTPUT

> sourceCpp(file = "main.cpp", verbose = TRUE)

Generated extern "C" functions
--------------------------------------------------------


#include <Rcpp.h>
// TimesTwo
double TimesTwo(double x);
RcppExport SEXP sourceCpp_0_TimesTwo(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject __result;
    Rcpp::RNGScope __rngScope;
    Rcpp::traits::input_parameter< double >::type x(xSEXP);
    __result = Rcpp::wrap(TimesTwo(x));
    return __result;
END_RCPP
}

Generated R functions
-------------------------------------------------------

`.sourceCpp_0_DLLInfo` <-
dyn.load('C:/Users/mlysy/AppData/Local/Temp/RtmpclqYkL/sourcecpp_c5c157c5536/sourceCpp_1.dll')

TimesTwo <- Rcpp:::sourceCppFunction(function(x) {}, FALSE,
`.sourceCpp_0_DLLInfo`, 'sourceCpp_0_TimesTwo')

rm(`.sourceCpp_0_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: C:/Users/mlysy/AppData/Local/Temp/RtmpclqYkL/sourcecpp_c5c157c5536

c:/PROGRA~1/R/R-32~1.4RE/bin/x64/R CMD SHLIB -o "sourceCpp_1.dll"
"c:\Users\Jerome\Documents\R\test\utilities.cpp" "main.cpp"
make: *** No rule to make target
`c:\Users\Jerome\Documents\R\test\utilities.o', needed by
`sourceCpp_1.dll'.  Stop.
Warning message:
running command 'make -f "c:/PROGRA~1/R/R-32~1.4RE/etc/x64/Makeconf" -f
"c:/PROGRA~1/R/R-32~1.4RE/share/make/winshlib.mk" -f
"C:\Users\mlysy\AppData\Roaming/.R/Makevars"
SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)'
SHLIB="sourceCpp_1.dll" WIN=64 TCLBIN=64
OBJECTS="c:\Users\Jerome\Documents\R\test\utilities.o main.o"' had status 2
Error in sourceCpp(file = "main.cpp", verbose = TRUE) :
  Error 1 occurred building shared library.

==============================================================

LINUX OUTPUT

> sourceCpp(code = main, verbose = TRUE)

Generated extern "C" functions
--------------------------------------------------------


#include <Rcpp.h>
// TimesTwo
double TimesTwo(double x);
RcppExport SEXP sourceCpp_0_TimesTwo(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject __result;
    Rcpp::RNGScope __rngScope;
    Rcpp::traits::input_parameter< double >::type x(xSEXP);
    __result = Rcpp::wrap(TimesTwo(x));
    return __result;
END_RCPP
}

Generated R functions
-------------------------------------------------------

`.sourceCpp_0_DLLInfo` <-
dyn.load('/tmp/RtmpEhyQ33/sourcecpp_7c167caa37d5/sourceCpp_1.so')

TimesTwo <- Rcpp:::sourceCppFunction(function(x) {}, FALSE,
`.sourceCpp_0_DLLInfo`, 'sourceCpp_0_TimesTwo')

rm(`.sourceCpp_0_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /tmp/RtmpEhyQ33/sourcecpp_7c167caa37d5

/software/r-3.2.2-tcl86/distribution/lib64/R/bin/R CMD SHLIB -o
'sourceCpp_1.so'  'file7c16504c2103.cpp'
g++ -I/software/r-3.2.2-tcl86/distribution/lib64/R/include -DNDEBUG
-I/software/readline/include -I/software/gsl-1.15/include
-I"/fsys1/.software/arch/r-3.2.2-tcl86/distribution/lib64/R-contrib/Rcpp/include"
-I"/tmp/RtmpEhyQ33"    -fpic  -g -O2  -c file7c16504c2103.cpp -o
file7c16504c2103.o
file7c16504c2103.cpp:4:25: error: utilities.hpp: No such file or directory
file7c16504c2103.cpp: In function 'double TimesTwo(double)':
file7c16504c2103.cpp:7: error: 'timesTwo' was not declared in this scope
make: *** [file7c16504c2103.o] Error 1
Error in sourceCpp(code = main, verbose = TRUE) :
  Error 1 occurred building shared library.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160413/9dd2a1cb/attachment.html>


More information about the Rcpp-devel mailing list