gpt4 book ai didi

c++ - 带有Rcpp头文件的c++文件的编译步骤

转载 作者:行者123 更新时间:2023-11-30 01:36:51 25 4
gpt4 key购买 nike

我正在 gedit 上尝试这段代码,并在终端上通过 g++ 编译器进行编译。

#include <Rcpp.h>

using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
int n = x.size();
double total = 0;

for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}

// [[Rcpp::export]]
double meanC(NumericVector x) {
return sumC(x) / x.size();
}

头文件发生错误。

fatal error: Rcpp.h: No such file or directory

我是这样编译的:g++ -I/usr/r1.cpp -o c0 -L/usr/-lRcpp我也试过:g++ -I/usr/lib/R/site-library/Rcpp/include/r1.cpp -o c0 -L/usr/lib/R/site-library/Rcpp/libs/-lRcpp 。然后得到像致命的错误

error: R.h: No such file or directory #include <R.h>

Locations:
locate Rcpp.h:/usr/lib/R/site-library/Rcpp/include/Rcpp.h
locate R.h:/usr/share/R/include/R.h

我也尝试过使用 make 文件。我的制作文件:

all:
g++ rcpp.cpp -o obj
compile:

我已将所有依赖的头文件附加到一个文件夹中。仍然收到 Rcpp 的错误。

有人知道如何通过终端编译这个吗?

最佳答案

你可以编译这个文件

g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -o rcpp.o

但是,我不明白你为什么要这样做。为了使此类 C++ 函数可从 R 调用,需要几个额外的步骤:

  • 转换为基于 R 的 SEXP 接口(interface)的 C++ 包装函数。
  • 通过 .Call() 调用 C++ 包装函数的 R 包装函数。
  • 将所有目标文件链接到 R 可以加载的动态库中。
  • 将库和 R 包装函数加载到 R 中。

所有这些都是通过 sourceCpp() 或在使用 Rcpp 的包的上下文中使用 Rcpp::compileAttributes() 时自动完成的,c.f. attributes 上的小插曲和 packages .

关于c++ - 带有Rcpp头文件的c++文件的编译步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51094229/

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