在 Rcpp 中使用标准包含

Use standard includes in Rcpp

我安装了 R 以及 RStudio 和 RTools,目前我正在尝试获取 Rcpp 运行。我尝试将此代码作为测试文件:

#include <RcppArmadillo.h>
#include <cmath.h>
//[[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;

// [[Rcpp::export]]
double Mutual_Information(
    arma::mat joint_dist
){
  joint_dist = joint_dist/sum(sum(joint_dist));
  double mutual_information = 0;
  int num_rows = joint_dist.n_rows;
  int num_cols = joint_dist.n_cols;
  arma::mat colsums = sum(joint_dist,0);
  arma::mat rowsums = sum(joint_dist,1);
  for(int i = 0; i < num_rows; ++i){
    for(int j = 0; j <  num_cols; ++j){
      double temp = log((joint_dist(i,j)/(colsums[j]*rowsums[i])));
      if(!std::isfinite(temp)){
        temp = 0;
      }
      mutual_information += joint_dist(i,j) * temp; 
    }
  } 
  return mutual_information;    
}

但我收到此错误消息:

c:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-34~1.2/include" -DNDEBUG -I../inst/include -fopenmp -I"C:/Users/root/Documents/R/win-library/3.4/Rcpp/include" -I"C:/Users/root/Documents/R/win-library/3.4/RcppArmadillo/include" -I"C:/Users/root/OneDrive/Uni/SEMEST~1/PROJEK~1/test/src" -I"C:/Users/root/OneDrive/Uni/Semester 3/Projektarbeit/test/inst/include"

-I"d:/Compiler/gcc-4.9.3/local330/include" -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o rcpp_hello_world.cpp:2:19: fatal error: cmath.h: No such file or directory #include ^ compilation terminated. make: *** [rcpp_hello_world.o]

Error 1 Warnmeldung: Ausführung von Kommando 'make -f "C:/PROGRA~1/R/R-34~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-34~1.2/share/make/winshlib.mk" CXX='$(CXX11) $(CXX11STD)' CXXFLAGS='$(CXX11FLAGS)' CXXPICFLAGS='$(CXX11PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX11LDFLAGS)' SHLIB_LD='$(SHLIB_CXX11LD)' SHLIB="sourceCpp_3.dll" WIN=64 TCLBIN=64 OBJECTS="rcpp_hello_world.o"' ergab Status 2 Error in Rcpp::sourceCpp("src/rcpp_hello_world.cpp") : Error 1 occurred building shared library. In addition: Warning message: In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="C:/Users/root/OneDrive/Uni/Semester 3/Projektarbeit/test/src/../inst/include": Das System kann den angegebenen Pfad nicht finden

库("Rcpp")和库("RcppArmadillo")加载成功...

据我所知,这个错误是找不到包含文件。编译器查找的路径不存在。它应该处理我假设的包含路径本身.. 在 QT 中包含此头文件或 Visual Studio 没有任何错误..

我需要调整一些 PATH 设置吗?我 运行 在 Windows 10 x64 上 我找不到任何使用 google 的解决方案,所以我希望你能帮助我解决这个问题。 非常感谢

没有错误信息所说的header cmath.h。你的意思可能是 cmath.

修复和简化(无需命名空间声明)文件通过:

#include <RcppArmadillo.h>
#include <cmath>

//[[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
double Mutual_Information(arma::mat joint_dist){
  joint_dist = joint_dist/sum(sum(joint_dist));
  double mutual_information = 0;
  int num_rows = joint_dist.n_rows;
  int num_cols = joint_dist.n_cols;
  arma::mat colsums = sum(joint_dist,0);
  arma::mat rowsums = sum(joint_dist,1);
  for(int i = 0; i < num_rows; ++i){
    for(int j = 0; j <  num_cols; ++j){
      double temp = log((joint_dist(i,j)/(colsums[j]*rowsums[i])));
      if(!std::isfinite(temp)){
        temp = 0;
      }
      mutual_information += joint_dist(i,j) * temp; 
    }
  } 
  return mutual_information;    
}

R> sourceCpp("/tmp/soQ.cpp")
R> 

这里没有错误。