Rcpp code run with error "undefined reference" in clion

  c++, clion, r, rcpp

I tried to write Rcpp code in clion, the environment is set like below, and the demo can run rightly by sourceCpp() in R, however, in c++, it couldn’t run, the error tells me something about undefined reference. I have pointed the header files directories in cmakelists file(the last image), is there any solution? I don’t want to write c++ in Rstudio, in clion, I can get better tooltips and debug performance.
enter image description here


#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double rcpp_sum(NumericVector v){
    double sum = 0;
    for(int i=0; i<v.length(); ++i){
        sum += v[i];
    }
    return(sum);
}

enter image description here

cmake_minimum_required(VERSION 3.19)
project(learnRcpp)

set(CMAKE_CXX_STANDARD 14)

add_executable(learnRcpp
        rcpp_sum.cpp)
include_directories(
        "./ext/Rcpp/include",
        "C:/Program Files/R/R-4.0.5/include"
)

Source: Windows Questions C++

LEAVE A COMMENT