【发布时间】: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)我在这里做错了什么。
我在谷歌驱动器中包含了一些文件。
我尝试在线搜索但无法理解。我肯定想念一些东西。任何人都可以请帮助。
【问题讨论】:
-
你的问题比较混乱,但是头文件和库没有任何关系
-
我想我明白了。我正在尝试使用 RInside 给出的示例。在几乎所有示例中,#include
都放在 .cpp 文件而不是 .h 文件中。我一直将此包含行放在 Test1.h 中。现在我替换了该行并将其保存在 Test1.cpp 中并重建了所有内容。它刚刚奏效。从几乎一天开始,我一直在尝试这个。我真的不知道有什么区别。我修改了更改并再次重建相同的问题。所以它一定是这样的。