ld 找不到 x86_64 架构的 Rcpp 符号

ld can't find Rcpp symbols for x86_64 architecture

当我尝试在 CLion 中编译 Rcpp 文件时,出现链接器错误,指出无法找到符号。该文件使用 sourceCpp 命令在 R 中编译时运行良好。这表明我在 CLion 中的配置有问题。我已经尝试遵循关于此 thread 的建议,包括从源代码编译 Rcpp。

最好能在 CLion IDE 中进行编译并使用调试工具。如果有人可以指点我一个指南来让这个工作或提供额外的,我将不胜感激。

一个简单的示例文件如下:

#include <Rcpp.h>

// Enable C++11 via this plugin (Rcpp 0.10.3 or later)
// [[Rcpp::plugins(cpp11)]]

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

int main() {
  NumericVector v(2);
  v[0] = 1;
  v[1] = 2;
  std::cout << sumC(v);
  return 0;
}

而 CMakeLists.txt 是

cmake_minimum_required(VERSION 3.3)
project(RcppTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(RcppTest ${SOURCE_FILES})

include_directories(/Library/Frameworks/R.framework/Headers)
include_directories(/Library/Frameworks/R.framework/Resources/library/Rcpp/include)

链接器产生的错误消息是:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/username/Library/Caches/clion11/cmake/generated/60a4b8d1/60a4b8d1/Debug --target RcppTest -- -j 8
Scanning dependencies of target RcppTest
[ 50%] Building CXX object CMakeFiles/RcppTest.dir/main.cpp.o
[100%] Linking CXX executable RcppTest
Undefined symbols for architecture x86_64:
  "_REprintf", referenced from:
      Rcpp::Rstreambuf<false>::xsputn(char const*, long) in main.cpp.o
      Rcpp::Rstreambuf<false>::overflow(int) in main.cpp.o
  "_R_FlushConsole", referenced from:
      Rcpp::Rstreambuf<false>::sync() in main.cpp.o
      Rcpp::Rstreambuf<true>::sync() in main.cpp.o
  "_R_GetCCallable", referenced from:
      dataptr(SEXPREC*) in main.cpp.o
  "_R_NilValue", referenced from:
      Rcpp::PreserveStorage<Rcpp::Vector<14, Rcpp::PreserveStorage> >::PreserveStorage() in main.cpp.o
      Rcpp::PreserveStorage<Rcpp::Vector<14, Rcpp::PreserveStorage> >::~PreserveStorage() in main.cpp.o
      Rcpp::Rcpp_ReleaseObject(SEXPREC*) in main.cpp.o
      Rcpp::Rcpp_PreserveObject(SEXPREC*) in main.cpp.o
  "_R_PreserveObject", referenced from:
      Rcpp::Rcpp_PreserveObject(SEXPREC*) in main.cpp.o
  "_R_ReleaseObject", referenced from:
      Rcpp::Rcpp_ReleaseObject(SEXPREC*) in main.cpp.o
  "_Rf_allocVector", referenced from:
      Rcpp::Vector<14, Rcpp::PreserveStorage>::Vector(int const&) in main.cpp.o
  "_Rf_isNull", referenced from:
      Rcpp::Rcpp_ReplaceObject(SEXPREC*, SEXPREC*) in main.cpp.o
  "_Rf_xlength", referenced from:
      Rcpp::Vector<14, Rcpp::PreserveStorage>::size() const in main.cpp.o
      void Rcpp::internal::r_init_vector<14>(SEXPREC*) in main.cpp.o
  "_Rprintf", referenced from:
      Rcpp::Rstreambuf<true>::xsputn(char const*, long) in main.cpp.o
      Rcpp::Rstreambuf<true>::overflow(int) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [RcppTest] Error 1
make[2]: *** [CMakeFiles/RcppTest.dir/all] Error 2
make[1]: *** [CMakeFiles/RcppTest.dir/rule] Error 2
make: *** [RcppTest] Error 2

使用Os X 10.10.5、R 版本 3.2.2 (2015-08-14)、Apple LLVM 版本 6.1.0 (clang-602.0.53)、Rcpp 0.12.0

除了我上面的直接评论外,您似乎tar站错了角落:

RcppTest.dir/main.cpp.o

int main() {
  NumericVector v(2);
  v[0] = 1;
  v[1] = 2;
  std::cout << sumC(v);
  return 0;
}

这根本不是它的工作原理。 Rcpp 是一个 R 扩展包,你可以通过 R CMD ... 构建它,使用 build 子命令创建一个 tar 存档,INSTALL 子命令安装 etc pp

如果您想在 C++ 应用程序中使用 Rcpp,请使用 RInside which is also on CRAN

如果在 C++ 程序中需要 Matrix 类,请使用 Armadillo which is excellent. And we also have RcppArmadillo ...