重新定义值编译错误

时间:2017-12-28 06:32:25

标签: r rcpp

在我的MacBook Pro(OS High Sierra 10.13.2)上重新安装Xcode和命令行工具后,我在R中通过sourceCpp()进行编译遇到了困难。

具体来说,我收到了错误

>sourceCpp("cube-sample.cpp")
clang++  -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -    I../inst/include   -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppArmadillo/include" -I"/Users/jarrettphillips/Desktop/HAC simulation" -I/usr/local/include   -fPIC  -Wall -g -O2  -c cube-sample.cpp -o cube-sample.o
Error in sourceCpp("cube-sample.cpp") : 
Error 1 occurred building shared library.

有关可能发生的事情的任何想法?也许这是一个PATH问题?]

这包含在我的.cpp文件的顶部:

// [[Rcpp::depends(RcppArmadillo)]]
#define ARMA_DONT_PRINT_OPENMP_WARNING
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
#include <set>
using namespace Rcpp;

这是我完整的C ++代码

int sample_one(int n) {
return n * unif_rand();
} 

int sample_n_distinct(const IntegerVector& x, 
                      int k,
                      const int * pop_ptr) {

  IntegerVector ind_index = RcppArmadillo::sample(x, k, false); 
  std::set<int> distinct_container;

  for (int i = 0; i < k; i++) {
    distinct_container.insert(pop_ptr[ind_index[i]]);
  }

  return distinct_container.size();
 }

// [[Rcpp::export]]
arma::Cube<int> fillCube(const arma::Cube<int>& pop,
                         const IntegerVector& specs,
                         int perms,
                         int K) {

  int num_specs = specs.size();
  arma::Cube<int> res(perms, num_specs, K);

  IntegerVector specs_C = specs - 1;
  const int * pop_ptr;
  int i, j, k;

  for (i = 0; i < K; i++) {
    for (k = 0; k < num_specs; k++) {
      for (j = 0; j < perms; j++) {
        pop_ptr = &(pop(0, sample_one(perms), sample_one(K)));
        res(j, k, i) = sample_n_distinct(specs_C, k + 1, pop_ptr);
      }
    }
  }

  return res;
}

非常感谢任何帮助。

编辑:上面的代码改进,但现在我收到了错误:

ld: warning: directory not found for option '-    L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
ld: warning: directory not found for option '-L/usr/local/gfortran/lib'
Error in fillCube(pop, num.specs, perms, K) : 
Not compatible with requested type: [type=character; target=integer].

它可能与我的本机R代码中的数据类型有关吗?我真正想要的是“流行音乐”。变量包含字符值(而不是int)。

1 个答案:

答案 0 :(得分:1)

您确定Xcode CLI安装正确吗?

应用程序 - &gt;实用程序 - &gt;终端

xcode-select --install

然后获取 R CRAN clang4二进制文件的安装程序(免责声明:我构建了安装程序):

https://github.com/coatless/r-macos-clang

修改

发布代码后,这不是编译器错误。此错误源于重新定义输入向量以包含输入的 size

c.f。

toad3.cpp:32:7: error: redefinition of 'specs' with a different type: 'int' vs 'const IntegerVector &' (aka 'const Vector<13> &')   int specs = specs.size();
      ^ toad3.cpp:28:47: note: previous definition is here
                         const IntegerVector& specs,

toad3.cpp:33:19: error: no matching constructor for initialization of 'arma::Cube<int>'
arma::Cube<int> res(perms, specs, K);
                  ^   ~~~~~~~~~~~~~~~

toad3.cpp:40:17: error: value of type 'Rcpp::sugar::Comparator_With_One_Value<13, Rcpp::sugar::greater<13>, true, Vector<13, PreserveStorage> >' is not contextually convertible to 'bool'
    for (k = 0; k < specs; k++) {
                ^~~~~~~~~
3 errors generated.

fillCube中执行以下三项更改可修复错误:

// [[Rcpp::export]]
arma::Cube<int> fillCube(const arma::Cube<int>& pop,
                         const IntegerVector& specs,
                         int perms,
                         int K) {

  int num_specs = specs.size();             // change variable
  arma::Cube<int> res(perms, num_specs, K); // change variable

  IntegerVector specs_C = specs - 1;
  const int * pop_ptr;
  int i, j, k;

  for (i = 0; i < K; i++) {
    for (k = 0; k < num_specs; k++) { // change variable
      for (j = 0; j < perms; j++) {
        pop_ptr = &(pop(0, sample_one(perms), sample_one(K)));
        res(j, k, i) = sample_n_distinct(specs_C, k + 1, pop_ptr);
      }
    }
  }

  return res;
}