“RcppArmadilloExtensions/sample.h"R 包构建步骤中的链接器错误 [英] "RcppArmadilloExtensions/sample.h" Linker Error in R Package Build Step

查看:47
本文介绍了“RcppArmadilloExtensions/sample.h"R 包构建步骤中的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 R 包,它在一些源文件中使用了 Rcpp::RcppArmadillo::sample() 函数.特别是,我在 4 个不同的 cpp 文件中使用了该函数,并且在每个文件中我添加了这一行 #include 以包含所需的头文件.

I am making an R package which uses the Rcpp::RcppArmadillo::sample() function in some of the source files. In particular, I use that function in 4 different cpp files and in each one of them I add this line #include <RcppArmadilloExtensions/sample.h> to include the required header file.

一切正常,直到我想通过命令 R CMD build mypackage 构建包.我收到 重复符号 错误,其中提到的头文件中定义的大多数函数都被列为重复符号.例如,像 ProbSampleNoReplaceFixProbSampleReplaceWalkerProbSampleReplace 之类的函数都被列为在这 4 个 cpp 文件之间重复.

Everything works fine until when I want to build the package by the command R CMD build mypackage. I get the duplicate symbol error where most of the functions defined in the mentioned header file are listed as the duplicated symbols. For instance, functions like ProbSampleNoReplace, FixProb, SampleReplace and WalkerProbSampleReplace are all listed as duplicated between those 4 cpp files.

我google了这个问题,解决办法是在.cpp文件而不是.h文件中定义变量或函数以防止重复,并使用extern 如果您想在任何其他 cpp 文件中使用这些变量.但我认为该解决方案不适用于这里,因为头文件不是我编写的,而且我不想对位于 这里.

I googled this problem and the solution is to define the variables or functions in the .cpp file instead of the .h file to prevent duplication, and use extern if you want to use those variables in any other cpp files. But I think that solution does not apply here as the header file is not written by me and I don't want to make any changes to the sample.h header file located here.

有什么建议可以解决这个问题吗?预先感谢您的帮助.

Any suggestion how to fix this problem? Thank you in advance for your help.

推荐答案

根据 Dirk 的评论,这最终对我有用:

This finally worked for me based on Dirk's comments:

我删除了每个文件顶部的多余标题并将其放在一个位置.这可以通过两种方式完成.一种是把它放在一个可以在任何需要的地方调用的访问函数中.

I removed redundant headers on top of each file and put it in one place. This can be done in two ways. One is to put it in one access function that can be called anywhere needed.

另一种选择是,如果您的整个项目不是那么大,您可以将标题 #include 放在主 .cpp 文件,其中包含您的具有多种功能的项目.我实际上为我自己的项目做了后一种选择.

Another option is that if your whole project is not that big, you can put the header #include <RcppArmadilloExtensions/sample.h> on top of the main .cpp file, which contains your project with multiple functions. I actually did the latter option for my own project.

这篇关于“RcppArmadilloExtensions/sample.h"R 包构建步骤中的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆