<div dir="ltr"><div><div><div>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.<br><br></div>Windows: <br>Platform: x86_64-w64-mingw32/x64 (64-bit)<br></div><div>Version: Windows 8.1<br></div>R version 3.2.4 Revised (2016-03-16 r70336) -- "Very Secure Dishes"<br></div>Rcpp version 0.12.4<br><div><br>Linux:  <br><div><div>Platform: x86_64-pc-linux-gnu (64-bit)<br>Version: CentOS release 6.7 (Final)<br>R version: 3.2.2 (2015-08-14) -- "Fire Safety"<br><div>(I do not have admin privileges to update R on this machine)<br></div>Rcpp version: 0.12.4<br></div><br><div>Sample code is:<br><br>#-----------------------------------------------------------------------------------------------<br><br>require(Rcpp)<br><br># header file<br>header <- "<br>#ifndef __UTILITIES__<br>#define __UTILITIES__<br>double timesTwo(double x);<br>#endif // __UTILITIES__<br>"<br>cat(header, file = "utilities.hpp")<br><br># implementation file<br>impl <- "<br>#include \"utilities.hpp\"<br>double timesTwo(double x) {<br>  return x * 2;<br>}<br>"<br>cat(impl, file = "utilities.cpp")<br><br># main file<br>main <- "<br>#include <Rcpp.h><br>using namespace Rcpp;<br>#include \"utilities.hpp\"<br>//[[Rcpp::export]]<br>double TimesTwo(double x) {<br>  return timesTwo(x);<br>}<br>"<br>cat(main, file = "main.cpp")<br><br># works on linux<br>sourceCpp(file = "main.cpp")<br><br># does not work on linux<br>sourceCpp(code = main)<br><br>#-----------------------------------------------------------------------------------------------<br><br></div><div>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?<br><br></div><div>Best regards,<br></div><div><br clear="all"><div><div><div><div><div dir="ltr">Martin Lysy<br>Assistant Professor of Statistics<br>Department of Statistics and Actuarial Science<br>University of Waterloo<br><br>====================================================<br><br></div><div>WINDOWS OUTPUT<br><br>> sourceCpp(file = "main.cpp", verbose = TRUE)<br><br>Generated extern "C" functions <br>--------------------------------------------------------<br><br><br>#include <Rcpp.h><br>// TimesTwo<br>double TimesTwo(double x);<br>RcppExport SEXP sourceCpp_0_TimesTwo(SEXP xSEXP) {<br>BEGIN_RCPP<br>    Rcpp::RObject __result;<br>    Rcpp::RNGScope __rngScope;<br>    Rcpp::traits::input_parameter< double >::type x(xSEXP);<br>    __result = Rcpp::wrap(TimesTwo(x));<br>    return __result;<br>END_RCPP<br>}<br><br>Generated R functions <br>-------------------------------------------------------<br><br>`.sourceCpp_0_DLLInfo` <- dyn.load('C:/Users/mlysy/AppData/Local/Temp/RtmpclqYkL/sourcecpp_c5c157c5536/sourceCpp_1.dll')<br><br>TimesTwo <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_0_DLLInfo`, 'sourceCpp_0_TimesTwo')<br><br>rm(`.sourceCpp_0_DLLInfo`)<br><br>Building shared library<br>--------------------------------------------------------<br><br>DIR: C:/Users/mlysy/AppData/Local/Temp/RtmpclqYkL/sourcecpp_c5c157c5536<br><br>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"  <br>make: *** No rule to make target `c:\Users\Jerome\Documents\R\test\utilities.o', needed by `sourceCpp_1.dll'.  Stop.<br>Warning message:<br>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/<a href="http://winshlib.mk" target="_blank">winshlib.mk</a>" -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 <br>Error in sourceCpp(file = "main.cpp", verbose = TRUE) : <br>  Error 1 occurred building shared library.<br><br>==============================================================<br><br></div><div>LINUX OUTPUT<br><br>> sourceCpp(code = main, verbose = TRUE)<br><br>Generated extern "C" functions<br>--------------------------------------------------------<br><br><br>#include <Rcpp.h><br>// TimesTwo<br>double TimesTwo(double x);<br>RcppExport SEXP sourceCpp_0_TimesTwo(SEXP xSEXP) {<br>BEGIN_RCPP<br>    Rcpp::RObject __result;<br>    Rcpp::RNGScope __rngScope;<br>    Rcpp::traits::input_parameter< double >::type x(xSEXP);<br>    __result = Rcpp::wrap(TimesTwo(x));<br>    return __result;<br>END_RCPP<br>}<br><br>Generated R functions<br>-------------------------------------------------------<br><br>`.sourceCpp_0_DLLInfo` <- dyn.load('/tmp/RtmpEhyQ33/sourcecpp_7c167caa37d5/sourceCpp_1.so')<br><br>TimesTwo <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_0_DLLInfo`, 'sourceCpp_0_TimesTwo')<br><br>rm(`.sourceCpp_0_DLLInfo`)<br><br>Building shared library<br>--------------------------------------------------------<br><br>DIR: /tmp/RtmpEhyQ33/sourcecpp_7c167caa37d5<br><br>/software/r-3.2.2-tcl86/distribution/lib64/R/bin/R CMD SHLIB -o 'sourceCpp_1.so'  'file7c16504c2103.cpp'<br>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<br>file7c16504c2103.cpp:4:25: error: utilities.hpp: No such file or directory<br>file7c16504c2103.cpp: In function 'double TimesTwo(double)':<br>file7c16504c2103.cpp:7: error: 'timesTwo' was not declared in this scope<br>make: *** [file7c16504c2103.o] Error 1<br>Error in sourceCpp(code = main, verbose = TRUE) :<br>  Error 1 occurred building shared library.<br><br></div></div></div>
</div></div></div></div></div></div>