首页 > 解决方案 > 无法在 RStudio 中使用头文件编译 RcppArmadillo 脚本

问题描述

我有一个 Rcpp 脚本,它由两个文件组成,一个main.cpp包含主函数,另一个functions.cpp文件包含在主函数中调用的一些其他函数。

main.cpp

#include "functions.h"

// [[Rcpp::export]]
... some function

头文件functions.h

#ifndef FUN_H
#define FUN_H

#include <RcppArmadillo.h>

double pmvnormArma(int n, const arma::mat& Sigma ) ;

#endif

和包含函数的脚本:functions.cpp

double pmvnormArma(int n, const arma::mat& Sigma)
{
  ...
  return(p) ;
}

我通常使用“源”按钮从 RStudio 编译我的 Rcpp 脚本。但是,当我将脚本分成多个文件时,如上所述,我得到一个错误:

make: *** No rule to make target 'p2/rcpp/prova/functions.o', needed by 'sourceCpp_4.so'.  Stop.

Error in Rcpp::sourceCpp("main.cpp") : 
  Error 1 occurred building shared library.

然后我尝试直接从终端编译代码,但是,由于我在 R 中安装了 Rcpp,编译器找不到所需的库:

$ g++ -c -o functions.o functions.cpp 
functions.cpp:2:10: fatal error: RcppArmadillo.h: No such file or directory
    2 | #include <RcppArmadillo.h>
      |          ^~~~~~~~~~~~~~~~~
compilation terminated.

即使我包含了犰狳的路径,我也会获得其他库(RcppCommon.h、Rh 等)丢失文件的新消息。我怎么解决这个问题?有没有一种简单的方法可以从 RStudio 编译大型项目,或者将 g++ 编译器链接到所有需要的包(位于 R 文件夹中)?

标签: c++rrstudiorcpprcpparmadillo

解决方案


推荐阅读