当我尝试在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
答案 0 :(得分:1)
除了我上面的直接评论之外,你似乎从错误的角落开始:
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
子命令等。
如果您想在C ++应用程序中使用Rcpp,请使用RInside also on CRAN。
如果您需要在C ++程序中使用Matrix类,请使用非常好的Armadillo。我们还有RcppArmadillo ...