构建并使用使用另一个共享库的源文件中的共享库.(Rinside)

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

本文关键字:共享 Rinside 源文件 构建 另一个      更新时间:2023-10-16

我是构建C 应用程序和Rinside(提供嵌入式R解释器访问的库中的库(的初学者,并且需要一些帮助。我想使用在类/模块中分离的Rintide将所有代码保留,然后提供一个函数(获取一些输入,使用RINSIDE执行一些任务并将输出返回(到需要这些数据的其他程序。我正在尝试是否可以在另一个项目/模块中使用此功能(如果有人需要的话,Omnet 静脉(,该功能具有许多其他源文件和单独的makefiles。如果可能的话,我希望不触摸该模块及其编译过程。因此,我尝试了一个简单的例子,并对构建有疑问。我在test1类中有Rintide代码,并希望在Test2类中使用它。据我了解,我将Rintide用作共享库。因此,我需要使用Test1构建共享库,然后将其包括或将其引用到Test2。

目录共享_1:test1.h,test1.cc:class test1

目录共享_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((,并在可能的情况下compile test2.cc,具有不同选项的g 。当我使用所有库(用于构建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的DIR来编译它时,它给了我错误。如果我正确理解,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包括。

不要test1.h中包括Rinside.h,但将其包含在test1.cc中,这样您就可以以唯一依赖Rinside库的源文件为 test1.cc的方式构造了系统。使用test1功能的任何其他来源都只能使用test1.h中的任何内容,有效地创建了您需要抽象Rinside的图层。