重新定义值编译错误

Redefinition of value compile error

在我的 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)的 'pop' 变量。

您确定 Xcode CLI 安装正确吗?

应用程序 -> 实用程序 -> 终端

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;
}