问题
I'm trying to get some c++ code to compile using sourceCpp and RcppArmadillo. I'm using R 3.3.3 on Ubuntu, Rcpp 0.12.10 and RcppArmadillo 0.7.800.2.0. The file, armatest.cpp, is totally stripped down.
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
using namespace Rcpp;
int one() {
return 1;
}
When I attempt to compile this from within Rstudio server, I get the following compilation error:
> Rcpp::sourceCpp("armatest.cpp", verbose = TRUE, rebuild=TRUE)
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
Generated R functions
-------------------------------------------------------
`.sourceCpp_1_DLLInfo` <- dyn.load('/tmp/Rtmp3oSZra/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_47d41ae4918/sourceCpp_10.so')
rm(`.sourceCpp_1_DLLInfo`)
Building shared library
--------------------------------------------------------
DIR: /tmp/Rtmp3oSZra/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_47d41ae4918
/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_10.so' --preclean 'armatest.cpp'
g++ -I/usr/share/R/include -DNDEBUG -I"/home/amercer/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" -I"/home/amercer/R/x86_64-pc-linux-gnu-library/3.3/RcppArmadillo/include" -I"/home/amercer" -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c armatest.cpp -o armatest.o
g++ -shared -L/usr/lib/R/lib -Wl,-z,relro -o sourceCpp_10.so armatest.o -llapack -lblas -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
/usr/share/R/share/make/shlib.mk:6: recipe for target 'sourceCpp_10.so' failed
Error in Rcpp::sourceCpp("armatest.cpp", verbose = TRUE, rebuild = TRUE) :
Error 1 occurred building shared library.
However, when I run R from the command line (outside of Rstudio-server), and try again, it compiles just fine.
>Rcpp::sourceCpp("armatest.cpp", verbose = TRUE, rebuild=TRUE)
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
Generated R functions
-------------------------------------------------------
`.sourceCpp_1_DLLInfo` <- dyn.load('/tmp/RtmptRileh/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_1966315b9edd0/sourceCpp_4.so')
rm(`.sourceCpp_1_DLLInfo`)
Building shared library
--------------------------------------------------------
DIR: /tmp/RtmptRileh/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_1966315b9edd0
/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_4.so' --preclean 'armatest.cpp'
g++ -I/usr/share/R/include -DNDEBUG -I"/home/amercer/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" -I"/home/amercer/R/x86_64-pc-linux-gnu-library/3.3/RcppArmadillo/include" -I"/home/amercer" -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c armatest.cpp -o armatest.o
g++ -shared -L/usr/lib/R/lib -Wl,-z,relro -o sourceCpp_4.so armatest.o -llapack -lblas -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
Warning message:
In Rcpp::sourceCpp("armatest.cpp", verbose = TRUE, rebuild = TRUE) :
No Rcpp::export attributes or RCPP_MODULE declarations found in source
As far as I can tell, they are executing exactly the same commands with the same flags. What explains the difference? Rcpp on its own works just fine for me in Rstudio-server, so it's something about RcppAarmadillo specifically.
回答1:
Likely something local to your setup as it works fine here also from RStudio---but note that I did add an // [[Rcpp::export]]
as well as an auto-executing example:
> Rcpp::sourceCpp("/tmp/armaQ.cpp")
> one()
[1] 1
>
Your example, minimally altered:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
using namespace Rcpp;
// [[Rcpp::export]]
int one() {
return 1;
}
/*** R
one()
*/
来源:https://stackoverflow.com/questions/43711710/code-using-rcpparmadillo-compiles-when-running-r-from-terminal-but-not-rstudio-s