0

I am beginner with building c++ applications and RInside (A library that provides embedded R interpreter access from C++ program) and need some help. I want to keep all code using RInside separated in a class/module and provide just a function (take some input, do some task using RInside and return the output) to other programs who need these data. I am experimenting if i can use this functionality in another project/module (Omnet++ veins if anyone wants specifically) which has so many other source files and separate makefiles. if possible i would like to not touch that modules and their compilation process. So i tried a simple example and have a doubt regarding building. I have RInside code in Test1 class and want to use it in Test2 class. As far as i understand i am using RInside as a shared library. So i need to build a shared library using Test1 and include or reference it in Test2.

Directory Shared_1:Test1.h , Test1.cc : class Test1

Directory 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()   

I Have created libTest1_1.so from Test1.cc like this.

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

I want to use test1Function1() from Test1.cc in Test2.cc and if possible compile Test2.cc with different options to g++. When i compile Test2.cc using All libraries(Libraries used for building Test1.cc and also libTest1_1.so), it works well.

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

But when i compile it by just compiling it by providing include dir of Test1.h and the libTest1.so, it gives me error. If i understand correctly, g++ is trying to compile Test1 when i am building Test2. So it says it can not find RInside.h even though i am not including it directly in Test2.

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

In file included from ./shared_2/Test2.h:11:0, from ./shared_2/Test2.cc:8: ./shared_1/Test1.h:12:74: fatal error: RInside.h: No such file or directory #include
^ compilation terminated.

What i want to understand: 1) how to separate the code using RInside from another project/ module. 2) What i am doing wrong here.
I Have included some file here in google drive.

drive

I tried searching online but could not understand. I am definitely missing to understand something. Can anyone please help.

rajeswar
  • 77
  • 5
  • 1
    Your question is quite confusing, but header files don't have anything to do with libraries – xaxxon Feb 16 '18 at 13:35
  • I think i got it. I was experimenting from examples given with RInside. In Almost all examples #include is placed in .cpp files rather than .h files. I have been placing this include line in Test1.h . Now i replaced that line and kept it in Test1.cpp and did a rebuild of everything. it has just worked. I have been trying this from almost a day. I really dont know the difference. I modified the changes and rebuild same issue again. So it must be something with that. – rajeswar Feb 16 '18 at 13:39

1 Answers1

2

When you include test1.h from test2.h, you also include Rinside.h because test1.h includes it.

Do not include Rinside.h in test1.h but include it in test1.cc, this way you will have constructed your system in such a way that the only source file that is dependent on the Rinside library is test1.cc. Any other source that uses functionality from test1 will only be using whatever is inside test1.h, effectively creating the layer you need to abstract RInside.

simurg
  • 1,208
  • 7
  • 14
  • 1
    Thank you very much simurg and xaxxon. Now i understand why it worked second time. What ever programs i have read mostly included external libraries in .h files, so i followed that. A simple detail is also very important. Sorry for posting big question. I really has trouble by not knowing that. Good thing is i learnt something new. Really Thanks Again. – rajeswar Feb 16 '18 at 13:52