繁体   English   中英

ld 找不到 x86_64 架构的 Rcpp 符号

[英]ld can't find Rcpp symbols for x86_64 architecture

当我尝试在 CLion 中编译 Rcpp 文件时,出现链接器错误,提示找不到符号。 该文件可以使用 sourceCpp 命令在 R 中正常编译。 这表明我在 CLion 中的配置有问题。 我已尝试遵循此线程上的建议,包括从源代码编译 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

除了我上面的直接评论,你似乎从错误的角落开始:

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 ...构建它R CMD ...使用build子命令创建 tar 存档,使用INSTALL子命令安装等 pp

如果您想在 C++ 应用程序中使用Rcpp ,请使用也在 CRAN 上的 RInside

如果您需要在 C++ 程序中使用 Matrix 类,请使用非常棒的Armadillo 我们还有RcppArmadillo ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM