【问题标题】:Building and using a shared library from source files that use another shared library. (RInside)从使用另一个共享库的源文件构建和使用共享库。 (R内)
【发布时间】:2018-02-16 13:17:48
【问题描述】:

我是构建 c++ 应用程序和 RInside(一个从 C++ 程序提供嵌入式 R 解释器访问的库)的初学者,需要一些帮助。我想将所有使用 RInside 的代码分隔在一个类/模块中,并仅向需要这些数据的其他程序提供一个函数(获取一些输入,使用 RInside 执行一些任务并返回输出)。我正在试验是否可以在另一个项目/模块中使用此功能(如果有人特别需要,Omnet++ 静脉),它有很多其他源文件和单独的 makefile。如果可能的话,我不想触及这些模块及其编译过程。所以我尝试了一个简单的例子,并对构建有疑问。 我在 Test1 类中有 RInside 代码,想在 Test2 类中使用它。据我了解,我使用 RInside 作为共享库。所以我需要使用 Test1 构建一个共享库,并在 Test2 中包含或引用它。

目录 Shared_1:Test1.h , Test1.cc : 类 Test1

目录 shared_2: Test2.h , Test2.cc : 类 Test2

Test1.h -> Includes “Rinside.h”     
Test1.cc -> includes “Test1.h”  
-> main(): Create a Test1 object and call test1Function1() 
->test1Function1() : Creates an embedded R instance and does some functionality

Test2.h -> Includes “Test1.h”         
Test2.cc -> includes “Test2.h”   
-> main() : Create a Test2 object and call  test2Function1()
-> test2Function1() : Create a Test1 object and call  test1Function1()   

我已经像这样从 Test1.cc 创建了 libTest1_1.so。

g++ -I/usr/share/R/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -Wall ./shared_1/Test1.cc  -Wl,--export-dynamic -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm  -lblas -llapack  -L/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -lRInside -Wl,-rpath,/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -fPIC -shared -o ./shared_1/libTest1_1.so

我想在 Test2.cc 中使用来自 Test1.cc 的 test1Function1(),如果可能的话,用 g++ 的不同选项编译 Test2.cc。当我使用所有库(用于构建 Test1.cc 和 libTest1_1.so 的库)编译 Test2.cc 时,它运行良好。

g++ -I ./shared_1/ -I/usr/share/R/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -Wall ./shared_2/Test2.cc  -Wl,--export-dynamic -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm  -lblas -llapack -L/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -lRInside -Wl,-rpath,/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -L./shared_1/ -lTest1_1 -fPIC -o ./shared_2/Test2.o

但是当我通过提供 Test1.h 和 libTest1.so 的包含目录来编译它时,它给了我错误。如果我理解正确,g++ 会在我构建 Test2 时尝试编译 Test1。所以它说它找不到 RInside.h,即使我没有直接将它包含在 Test2 中。

 g++ -I ./shared_1/ ./shared_2/Test2.cc -L./shared_1/ -lTest1_1 -fPIC -shared -o ./shared_2/Test2_3.o

在 ./shared_2/Test2.h:11:0 包含的文件中, 来自 ./shared_2/Test2.cc:8: ./shared_1/Test1.h:12:74:致命错误:RInside.h:没有这样的文件或目录 #包括
^ 编译终止。

我想了解的内容:1)如何使用 RInside 将代码与另一个项目/模块分开。 2)我在这里做错了什么。
我在谷歌驱动器中包含了一些文件。

drive

我尝试在线搜索但无法理解。我肯定想念一些东西。任何人都可以请帮助。

【问题讨论】:

  • 你的问题比较混乱,但是头文件和库没有任何关系
  • 我想我明白了。我正在尝试使用 RInside 给出的示例。在几乎所有示例中,#include 都放在 .cpp 文件而不是 .h 文件中。我一直将此包含行放在 Test1.h 中。现在我替换了该行并将其保存在 Test1.cpp 中并重建了所有内容。它刚刚奏效。从几乎一天开始,我一直在尝试这个。我真的不知道有什么区别。我修改了更改并再次重建相同的问题。所以它一定是这样的。

标签: c++ rinside


【解决方案1】:

当您从test2.h 包含test1.h 时,您还包含Rinside.h,因为test1.h 包含它。

不要在test1.h 中包含Rinside.h,但在test1.cc 中包含它,这样您将构建系统,使得唯一依赖于Rinside 库是test1.cc。任何其他使用来自test1 的功能的源将只使用test1.h 内部的任何内容,从而有效地创建您需要抽象 RInside 的层。

【讨论】:

  • 非常感谢 simurg 和 xaxxon。现在我明白为什么它第二次起作用了。我读过的程序大多包括 .h 文件中的外部库,所以我遵循了这一点。一个简单的细节也很重要。抱歉发布大问题。我真的有麻烦,因为我不知道。好在我学到了一些新东西。真的再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 2019-04-17
相关资源
最近更新 更多