c++ - 当我在 C++ 程序中包含 RInside header 时,如何让链接器工作?

标签 c++ linker include rcpp rinside

我正在尝试编译 RInside 包的标准示例存储库中的示例。我已经尝试运行存储库中已有的 Makefile,但没有用。到目前为止,我遇到了几个错误,其中一些已经解决了。问题是,每次我解决一个错误时,都会发生以下两种情况之一,它会出现一个新错误或一个旧错误再次出现。这是具有相应错误的不同 makefile 的代码:

1)没有-L的Makefile

    all:
        g++ -I/usr/share/R/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/include rinsidetest.cpp

正如预期的那样,将 make 应用于此文件会返回有关对 RInside::的 undefined reference 的错误

undefined reference to `RInside::RInside(int, char const* const*, bool, bool, bool)'

2)带有-L链接的Makefile

all:
g++ -I/usr/share/R/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -L/usr/lib/R/site-library/RInside/lib -L/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/libs -lRInside -lRcpp -L/usr/lib/R/lib -lR -Wl,-rpath,/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/libs -lRInside -Wl,-rpath,/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib rinsidetest.cpp

错误是这样的:

/usr/bin/ld: cannot find -lRcpp

原来libs仓库中的.so文件名为Rcpp.so,所以我将其重命名为libRcpp.so错误消失了。

3)重命名后Rcpp.so作为libRcpp.so我对第 2 点的同一个 makefile 应用了 make,错误再次是关于未定义的对 RInside:::

的引用
rinsidetest.cpp:(.text+0x100): undefined reference to `RInside::RInside(int, char const* const*, bool, bool, bool)

4)将make应用到文件夹中已有的makefile

make -f Makefile

错误(将 R_LIBS_USER 更改为

R_LIBS_USER :=      "/home/manuel/R/x86_64-pc-linux-gnu-library/3.4"

是:

fatal error: RInside.h: No such file or directory compilation terminated.

到目前为止,我已经阅读了更多关于在 C++ 中包含 header 的一般性问题,以及一些专门关于 RInside 的问题,其中一些问题由包的编写者 Dirk Eddelbuettel 回答,但所有答案都与使用链接器 -L<path>/include我几乎可以肯定我使用的是正确的。

这是我到目前为止读过的一些问题:

https://stackoverflow.com/questions/11537313/compiling-rinside-program-with-g-on-linux[https://stackoverflow.com/questions/11537313/compiling-rinside-program-with-g-on-linux][1]

Compiling the Rcpp package

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2012-May/003829.html

http://rcpp-devel.r-forge.r-project.narkive.com/A70U2nVw/problem-with-rinside-hello-world-example

我使用的是 Ubuntu 16.04 和 R 版本 3.4.4。 任何帮助将不胜感激!

最佳答案

我不确定你在哪里迷路了,但基本的想法是这样说:

  1. 进入目录。
  2. 清理干净以防万一。
  3. make(即 make all)或仅一个。

这仍然有效——我自己使用 Debian/Ubuntu。

这里做make rinside_sample0来证明这一点,然后运行它:

edd@rob:~$ cd git/rinside/inst/examples/standard/
edd@rob:~/git/rinside/inst/examples/standard$ make -f GNUmakefile clean
rm -vf rinside_sample9 [....stuff remove to keep it shorter...]  rinside_sample16
rm -vrf *.dSYM
edd@rob:~/git/rinside/inst/examples/standard$ make -f GNUmakefile rinside_sample0
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
           -I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
           -Wno-misleading-indentation -Wno-unused \
           -Wno-ignored-attributes -Wno-deprecated-declarations \
           -march=native -Wall  rinside_sample0.cpp  -Wl,--export-dynamic \
           -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR \
           -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -licuuc -licui18n \
           -lblas -llapack  -L/usr/local/lib/R/site-library/RInside/lib \
           -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
           -o rinside_sample0
edd@rob:~/git/rinside/inst/examples/standard$ ./rinside_sample0 s
Hello, world!
edd@rob:~/git/rinside/inst/examples/standard(master)$ 

我手动缩进了这个,我可能在 ~/.R/Makevars 中有一些本地设置显示在这里——这无关紧要。

重要的是它开箱即用,如果您不理会它。如果您更改了设置,但它坏了,您就可以解决一个原本不必创建的问题。

关于c++ - 当我在 C++ 程序中包含 RInside header 时,如何让链接器工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50008252/

相关文章:

c++ - stringstream 在 4GB 处溢出

c++ - Unresolved external 尚未定义的构造函数?

c++ - 包含链如何在 C++ 中运行?

C++,如何在头文件中声明结构

c++ - 何时以及如何在 C++ 中初始化静态数据?

c++ - 如何在不使用系统复制命令的情况下将可执行文件的二进制代码复制到新文件中?

C++:为什么我不能使用初始值设定项 char foo[] 将字符串转换为 C 字符串?

c - 检测静态库是否为瘦存档

c++ - 跨库的模板函数特化符号匹配

jsp - 一个大的包含文件还是几个较小的文件?