【问题标题】:Undefined reference errors when including Rcpp.h包含 Rcpp.h 时未定义的引用错误
【发布时间】:2014-05-22 10:41:48
【问题描述】:

我正在使用 64 位 Ubuntu,并且正在尝试编写 C++。

我发现如果我使用#include <Rcpp.h>,我什至不需要调用 R 命名空间中的任何函数,而且我已经收到未触发的引用错误:

obj/x.o: In function `Rcpp::Rstreambuf<true>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:61: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:65: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:70: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:74: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:79: undefined reference to `R_FlushConsole'
obj/x.o: In function `Rcpp::Rstreambuf<false>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:83: undefined reference to `R_FlushConsole'

我已经安装了 r-base 和 r-base-dev。我通过以 root 身份运行 R 来安装 Rcpp 并执行了 install.package("Rcpp")

我使用带有-I/usr/local/lib/R/site-library/Rcpp/include的g++编译C++程序

我在这里缺少什么?感谢您的任何回复。

【问题讨论】:

  • 您缺少-l&lt;library&gt;(对于需要的任何库)和可能的-L&lt;path-to-directory-containing-library&gt;
  • R 为此提供了R CMD SHLIB
  • @jsantander:不只是一个。他错过了整个 R 生态系统。罗曼的回答和评论解决了这个问题。

标签: c++ r g++ rcpp include-path


【解决方案1】:

仅拉动Rcpp 标头是不够的。您还需要 R 标头并链接到 R 的库。你可以使用例如R CMD SHLIB 为您执行此操作。

但是,我建议您:

  • 创建一个包含LinkingTo: Rcpp 等的包...(参见Rcpp 的文档)。
  • 在您的.cpp 文件上使用sourceCpp。见?sourceCpp

【讨论】:

  • 你所描述的,尤其是sourceCpp部分听起来像网上很多例子,以加速使用C++执行R,并从R调用C++代码。有没有办法只在C++中使用R库而不执行它在 R 中?
  • 不,没有。你想使用 R:使用 R。
  • RInside 允许您将 R 嵌入到您的 C++ 应用程序中,但这也仍然涉及 R,因为确实没有运行 R 就没有运行 R。当然,您可以剥离相应的算法和实现,因为它是开源的(但请尊重许可条款)。
【解决方案2】:

正如 Romain Francois 所指出的,您不能只拥有标头(声明),还需要实现。

我的建议是制作一个生成 .so 对象的 Makefile。 R CMD SHLIB 命令是确定需要哪些标志的良好起点,但它无法处理设计为在 Matlab 之外执行的函数。

然后,您需要找到 Rcpp.so 和 libR.so 并在您的 g++ 调用中链接它们。

因此,“在 R 之外使用 R”是有希望的——就我而言,我能够将 VineCopula 包中的一些内容编译成 Matlab 能够读取的 .so 文件。

请参阅下面的 Makefile(仅作为示例):

CFLAGS=-I/usr/share/R/include/ -I/usr/local/lib/R/site-library/Rcpp/include/ -I/usr/local/lib/R/site-library/VineCopula/include -dynamiclib -Wl,-headerpad_max_install_names -shared -L/usr/lib/R/lib -lR 
CFLAGS2=-I/usr/share/R/include/ -I/usr/local/lib/R/site-library/Rcpp/include/ -I/usr/local/lib/R/site-library/VineCopula/include 
LDFLAGS=-DNDEBUG -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g

all: RVinePDF.so

RVinePDF.so: RVinePDF.o Rcpp.so libR.so
  g++ $(CFLAGS) -o RVinePDF.so RVinePDF.o Rcpp.so libR.so $(LDFLAGS) 
  \rm *.o 

RVinePDF.o: RVinePDF.cpp 
  g++ $(CFLAGS2) -o RVinePDF.o -c RVinePDF.cpp $(LDFLAGS)

[other .o files defined similarly]

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多