Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;2“建筑用重复符号”;编译包Rcpp时,RcppProgress 我怀疑这个问题是因为对C++的误解,但是既然我使用RCPP,我就把这个问题标记出来了。我创建了两个函数,每个函数都在一个单独的.cpp文件中_C++_R_Ld_Rcpp - Fatal编程技术网

&引用;2“建筑用重复符号”;编译包Rcpp时,RcppProgress 我怀疑这个问题是因为对C++的误解,但是既然我使用RCPP,我就把这个问题标记出来了。我创建了两个函数,每个函数都在一个单独的.cpp文件中

&引用;2“建筑用重复符号”;编译包Rcpp时,RcppProgress 我怀疑这个问题是因为对C++的误解,但是既然我使用RCPP,我就把这个问题标记出来了。我创建了两个函数,每个函数都在一个单独的.cpp文件中,c++,r,ld,rcpp,C++,R,Ld,Rcpp,f1.cpp: // [[Rcpp::depends(RcppProgress)]] #include <progress.hpp> using namespace Rcpp; // [[Rcpp::export]] NumericVector f1(int n) { int i; Progress p(n, true); NumericVector x(n); for( i = 0 ; i < n ; i++ ) {

f1.cpp:

// [[Rcpp::depends(RcppProgress)]]
#include <progress.hpp>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector f1(int n)
{
    int i;
    Progress p(n, true);
    NumericVector x(n);

    for( i = 0 ; i < n ; i++ )
    {
      if (Progress::check_abort() )
        Rcpp::stop("Operation cancelled by interrupt.");

      p.increment(); // update progress

      x[i] = Rf_rnorm(0,1);
    }
    return x;
}

我认为这意味着我需要将
#include
放在一个单独的头文件中,以便从每个cpp文件中包括进来,但我尝试了,但没有成功。我也看过其他使用Rcpp/RcppProgress的软件包,但我不清楚应该更改什么。

这不是你的错。这是RcppProgress作者的错。他们的
进度。hpp

class Progress {
   // ...
private: // ===== INSTANCE VARIABLES
    static InterruptableProgressMonitor* _monitor_singleton;
};

InterruptableProgressMonitor* Progress::_monitor_singleton = 0;  // <-- this defines _monitor_singleton

当您将标题包含在两个单独的翻译单元(即,
.cpp
文件)中时,这两个文件都会导致违反单定义规则,表现为您得到的链接器错误。要解决此问题,您需要以某种方式重新排列代码,以便只有一个
.cpp
文件包含
progress.hpp
(例如,将两个文件合并为一个文件),或者对它们的头文件进行一些编辑。

这两个函数都称为
f1
。重命名其中一个。两个源文件中都有重复的id(相同的id
f1
)。这是复制/粘贴错误还是您真正的源代码?如果这是您的真实代码,为什么您会对出现重复符号链接时间错误感到惊讶?另外,查看函数,您可能应该将它们组合成一个函数,并使用参数指定分布。这是一个复制粘贴错误<第二个列表中的code>f1应该是
f2
。我从错误的窗口粘贴。@Roland:这是一个玩具问题,旨在演示这个问题。我的“真实”代码不适合结合这两个函数。谢谢!我将与RcppProgress的作者联系。或者重新安排代码,使其仅在其中一个文件中包含RcppProgress.hpp。
duplicate symbol __Z14checkInterruptv in:
    f1.o
    f2.o
duplicate symbol __ZN8Progress18_monitor_singletonE in:
    f1.o
    f2.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
class Progress {
   // ...
private: // ===== INSTANCE VARIABLES
    static InterruptableProgressMonitor* _monitor_singleton;
};

InterruptableProgressMonitor* Progress::_monitor_singleton = 0;  // <-- this defines _monitor_singleton
bool checkInterrupt() {
    return (R_ToplevelExec(chkIntFn, NULL) == FALSE);
}