I get a linker error saying that symbol(s) cannot be found when I try to compile an Rcpp file in CLion. The file works fine compiling in R using the sourceCpp command. This would suggest that something is not right with my configuration in CLion. I have tried following the suggestions on this thread , including compiling Rcpp from source.
It would be nice to get this compiling in CLion IDE and use the debugging tools. If anybody could point me at a guide to get this working or provide additional, it would be greatly appreciated.
An simple example files is as follows:
#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;
}
And the CMakeLists.txt is
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)
The error messages produced by the linker are:
/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
Using Os X 10.10.5, R version 3.2.2 (2015-08-14), Apple LLVM version 6.1.0 (clang-602.0.53), Rcpp 0.12.0
Beside the immediate comment I made above, you seem to be starting in the wrong corner:
RcppTest.dir/main.cpp.o
and
int main() {
NumericVector v(2);
v[0] = 1;
v[1] = 2;
std::cout << sumC(v);
return 0;
}
That is simply not how it works. Rcpp is an R extension package, and you build it via R CMD ...
using the build
subcommand to create a tar archive, the INSTALL
subcommand to install etc pp
If you want to work with Rcpp in a C++ application, use RInside which is also on CRAN .
If you need Matrix classes in a C++ program, use Armadillo which is excellent. And we also have RcppArmadillo ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.