我是C++的初学者,我想在C++代码中向RcppEigen添加一个新的程序包“CRTRCppEigen”,而运行某些.bat
文件时发生了一些错误。任何帮助将不胜感激。
这是我要添加到文件“src”中的RcppEigen的名为“CRTRCppEigen”的C++代码函数:
#include < Rcpp.h >
#include < RcppEigen.h >
#include <Eigen/Dense>
#include <iostream>
#include <string>
using Eigen::MatrixXd;
using namespace std;
using namespace Rcpp;
using namespace RcppEigen;
using namespace Eigen;
RcppExport SEXP MatOp(SEXP Xr, SEXP Yr, SEXP Kr)
{
MatrixXd X = RcppEigen::as<MatrixXd>(Xr);
MatrixXd Y = RcppEigen::as<MatrixXd>(Yr);
string K = Rcpp::as<string>(Kr);
int n=X.rows();
int p=X.cols();
int nY=Y.cols();
MatrixXd I(n,n);
I.setIdentity(n,n);
double SSE=(Y.transpose()*(I-X*(X.transpose()*X).inverse()*X.transpose())*Y).determinant();
if(Criteria=="k1")
return (wrap(n*log(SSE/n)+log(n)*p));
if(Criteria=="k2")
return (wrap(n*log(SSE/n)+(2*p*nY*n+nY*(nY+1))/n-2/n+n+2));
}
这是文件“R”中的R代码
RcppEigen.package.skeleton("CRTRCppEigen")
getwd()
#[1] "C:/Users/LJH/Documents"
CRTR <- function(Xr,Yr,criteriar) {
.Call('CRTRCppEigen',Xr,Yr,criteriar,PACKAGE = 'CRTRCppEigen')
}
prompt(CRTR)
Description文件是
Package: CRTRCppEigen
Type: Package
Title: RSS calculation based matrix operation
Version: 1.0
Date: 2014-10-22
Author: Junhui Li
Maintainer: Junhui Li<[email protected]>
Description: Residual sum of square
License: GPL (>= 2)
Imports: Rcpp (>= 0.11.3), RcppEigen (>= 0.3.2.2.0)
LinkingTo: Rcpp, RcppEigen
build CRTRCppEigen package Windows Binary.bat
文件是:Rcmd INSTALL --build CRTRCppEigen
PAUSE
build CRTRCppEigen package Linux Source Code.bat
文件是:Rcmd build CRTRCppEigen
pause
install CRTRCppEigen package.bat
文件是:Rcmd INSTALL CRTRCppEigen
PAUSE
check CRTRCppEigen package.bat
文件是:Rcmd check CRTRCppEigen
PAUSE
环境变量是
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Hewlett-Packard\HP ProtectTools Security Manager\Bin\;c:\Program Files\Intel\DMIX;C:\Program Files (x86)\Intel\Services\IPT\;C:\Program Files\SASHome\Secure;C:\Program Files\SASHome\x86\Secure;C:\Opt\MiKTeX2.9\miktex\bin\x64\;C:\Opt\MiKTeX2.9\miktex\bin\x64\;C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;C:\R\R-3.1.1\bin\x64;C:\R\R-3.1.1\bin\i386;
当我运行
build CRTRCppEigen package Windows Binary.bat
时,发生错误: * installing *source* package 'CRTRCppEigen' ...
** libs
*** arch - i386
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
g++ -m32 -I"C:/R/R-31~1.1/include" -DNDEBUG -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c CRTRCppEigen.cpp -o CRTRCppEigen.o
CRTRCppEigen.cpp:3:20: fatal error: Rcpp.h : No such file or directory
compilation terminated.
make: *** [CRTRCppEigen.o] Error 1
Warning: running command 'make -f "Makevars.win" -f "C:/R/R-31~1.1/etc/i386/Makeconf" -f "C:/R/R-31~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="CRTRCppEigen.dll" OBJECTS="CRTRCppEigen.o RcppExports.o rcppeigen_hello_world.o"' had status 2
cygwin warning:
MS-DOS style path detected: C:/R/R-31~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
g++ -m32 -I"C:/R/R-31~1.1/include" -DNDEBUG -I"C:/Users/LJH/Documents/R/win-library/3.1/Rcpp/include" -I"C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c CRTRCppEigen.cpp -o CRTRCppEigen.o
CRTRCppEigen.cpp:3:20: fatal error: Rcpp.h : No such file or directory
compilation terminated.
make: *** [CRTRCppEigen.o] Error 1
Warning: running command 'make -f "Makevars.win" -f "C:/R/R-31~1.1/etc/i386/Makeconf" -f "C:/R/R-31~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="CRTRCppEigen.dll" OBJECTS="CRTRCppEigen.o RcppExports.o rcppeigen_hello_world.o" symbols.rds' had status 2
ERROR: compilation failed for package 'CRTRCppEigen'
* removing 'C:/Users/LJH/Documents/CRTRCppEigen.Rcheck/CRTRCppEigen'
但是在这种情况下可以使用Rcpp软件包。所以我想C++代码有问题。
最佳答案
你的错误是
CRTRCppEigen.cpp:3:20:致命错误:Rcpp.h:没有这样的文件或目录
由于您似乎在LinkingTo: Rcpp
中具有DESCRIPTION
,因此不应发生这种情况。
要检查,我只是在Windows框上进行了此操作:
R> setwd("c:/temp")
R> RcppEigen.package.skeleton(name="quicktest")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './quicktest/Read-and-delete-me'.
Adding RcppEigen settings
>> added Imports: Rcpp, RcppEigen
>> added LinkingTo: Rcpp, RcppEigen
>> added useDynLib directive to NAMESPACE
>> added Makevars file
>> added Makevars.win file
>> added example src file using Eigen classes
>> invoked Rcpp::compileAttributes to create wrappers
R>
然后直接安装:
R> install.packages("quicktest", repos=NULL, type="source")
Installing package into ‘c:/opt/R-library’
(as ‘lib’ is unspecified)
* installing *source* package 'quicktest' ...
** libs
*** arch - i386
g++ -m32 -I"C:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m32 -I"C:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
g++ -m32 -shared -s -static-libgcc -o quicktest.dll tmp.def RcppExports.o rcppeigen_hello_world.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/i386 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/opt/R-CURR~1/bin/i386 -lR
installing to c:/opt/R-library/quicktest/libs/i386
*** arch - x64
g++ -m64 -I"C:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m64 -I"C:/opt/R-CURR~1/include" -DNDEBUG -I"c:/opt/R-library/Rcpp/include" -I"c:/opt/R-library/RcppEigen/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
g++ -m64 -shared -s -static-libgcc -o quicktest.dll tmp.def RcppExports.o rcppeigen_hello_world.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/opt/R-CURR~1/bin/x64 -lR
installing to c:/opt/R-library/quicktest/libs/x64
** R
** preparing package for lazy loading
** help
Warning: c:/TEMP/quicktest/man/quicktest-package.Rd:32: All text must be in a section
Warning: c:/TEMP/quicktest/man/quicktest-package.Rd:33: All text must be in a section
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (quicktest)
R>
如您所见,它可以按预期工作,因此我建议您将空的骨架包与您的骨架包进行比较。
FWIW我也通常在命令行上工作,因此您在那里所做的一切看起来也不错。就是说,您的R
PATH
条目位于结尾,并且手册在某些方面强烈建议将其放在开头。