gpt4 book ai didi

c++ - 如何使用 RcppEigen

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:26 28 4
gpt4 key购买 nike

我是C++的初学者,我想在C++代码中添加一个新的包“CRRTRCppEigen”到RcppEigen,但是在运行一些.bat文件时出现了错误。任何帮助将不胜感激。

这是我想添加到文件“src”中的 RcppEigen 的名为“CRRTRCppEigen”的 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)

描述文件是

Package: CRTRCppEigen
Type: Package
Title: RSS calculation based matrix operation
Version: 1.0
Date: 2014-10-22
Author: Junhui Li
Maintainer: Junhui Li<junhuili@cau.edu.cn>
Description: Residual sum of square
License: GPL (>= 2)
Imports: Rcpp (>= 0.11.3), RcppEigen (>= 0.3.2.2.0)
LinkingTo: Rcpp, RcppEigen

构建 CRTRCppEigen 包 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

检查CRRTRCppEigen 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++ 代码有问题。

最佳答案

你的错误是

CRRTRCppEigen.cpp:3:20: fatal error : Rcpp.h: 没有那个文件或目录

这不应该发生,因为您在 DESCRIPTION 中似乎有 LinkingTo: Rcpp

为了检查,我刚刚在 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 条目在末尾,手册强烈建议将它们放在前面。

关于c++ - 如何使用 RcppEigen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543025/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com