从使用另一个共享库的源文件构建和使用共享库。 (R里面)

Building and using a shared library from source files that use another shared library. (RInside)

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

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

目录 shared_2: Test2.h , Test2.cc : class 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: 没有那个文件或目录 #include
^ 编译终止。

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

drive

我尝试在网上搜索但无法理解。我肯定想念一些东西。谁能帮忙。

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

不要Rinside.h包含在test1.h中,而是将其包含在test1.cc中,这样你就可以这样构建你的系统了唯一依赖于 Rinside 库的源文件是 test1.cc。使用 test1 中的功能的任何其他源将仅使用 test1.h 中的任何内容,有效地创建抽象 RInside 所需的层。