If there is an error in above command please check due to which dependency error is occurring.
and extract it. After extracting open terminal and go to the path where thrift is extracted and give the following commands
You can check whether apache thrift is installed properly or not by giving command below. If you are able to see the version of thrift then your installation is completed successful.
As explain before we can create source codes for many languages
without installation of that language, but to run created source code you will require to install those language. To create remote server you will require to create a file with extension thrift. This thrift file contain thrift interface description language we will explain this in short for detail refer
link.
Lets create one example which performs basic arithmetic operations
Thrift_IDL.thrift.
#this is comment
#!/usr/local/bin/thrift --gen cpp
namespace cpp Test #this indicates that given thrift is applicable to cpp
service arithmetic_op #create class arithmetic_opHandler : virtual public arithmetic_opIf with following public function
{
i32 add(1:i32 num1, 2:i32 num2), #this create function with int32_t add(const int32_t num1, const int32_t num2)
i32 sub(1:i32 num1, 2:i32 num2), #this create function with int32_t sub(const int32_t num1, const int32_t num2)
i32 mul(1:i32 num1, 2:i32 num2), #this create function with int32_t mul(const int32_t num1, const int32_t num2)
i32 div(1:i32 num1, 2:i32 num2), #this create function with int32_t div(const int32_t num1, const int32_t num2)
}
service arithmetic_Demo #this is second service for dispay
{
i32 display(1:i32 num1, 2:i32 num2)
}
To generate server for this file you require to give the command
thrift --gen cpp Thrift_IDL.thrift
This command will generate one folder with name "
gen-cpp" which contain 10 files as shown in the figure below. This number of file and the format of files changes as the language changes, here we are considering the files for
cpp.
Since we don't define any struct or enum declared in thrift file, the file Thrift_IDL_types.h, Thrift_IDL_types.cpp are only initialized but not used.
Modify Functions declared in Services(.thrift file):
Functions declared in the services of thrift file are only initialized by thrift. You require to add the contain to perform some action you require.
We will take example of arithmetic_op service: thrift will create
arithmetic_op_server.skeleton.cpp file which contain main function and thrift will create
arithmetic_opHandler class which contain constructor and the number of functions declared in the thrift file as shown below.
class arithmetic_opHandler : virtual public arithmetic_opIf {
public:
arithmetic_opHandler() {
// Your initialization goes here
}
int32_t add(const int32_t num1, const int32_t num2) {
// Your implementation goes here
printf("add\n");
}
int32_t sub(const int32_t num1, const int32_t num2) {
// Your implementation goes here
printf("sub\n");
}
int32_t mul(const int32_t num1, const int32_t num2) {
// Your implementation goes here
printf("mul\n");
}
int32_t div(const int32_t num1, const int32_t num2) {
// Your implementation goes here
printf("div\n");
}
};
You can add logic of addition, subtraction, multiplication and division in the functions add(), sub(), mul(), div() respectively. This functions are taking two inputs from client and printing the function name, instead of this you can add the logic of arithmetic operation.
Compiling:
- g++ -I/usr/local/include/thrift -c arithmetic_Demo.cpp
- g++ -I/usr/local/include/thrift -c arithmetic_Demo_server.skeleton.cpp
- g++ -I/usr/local/include/thrift -c arithmetic_op.cpp
- g++ -I/usr/local/include/thrift -c arithmetic_op_server.skeleton.cpp
- g++ -I/usr/local/include/thrift -c Thrift_IDL_constants.cpp
- g++ -I/usr/local/include/thrift -c Thrift_IDL_types.cpp
To compile all files in one stroke you can use the command given below
g++ -I/usr/local/include/thrift -c *.cpp
Here *cpp represent all files with extension .cpp.
Explanation:
-I: option which is used by gcc compiler to locate the header files. In this compilation we are suggesting the gcc compiler to search for the header files in the "/usr/local/include/thrift/" folder also.
Since it is only compilation gcc compiler won't link the thrift libraries which eliminates the requirement of including thrift library with
-lthrift.
-c: option in gcc compiler is used to
create object files if you don't provide
-o followed by your custom name of object file it will create object file with the same name of your
c or
cpp file with
.o extension.
Linking:
Since there are two services in our example we get two
Service_name_server_skeleton.cpp files which contain main function in it. Due to this we require to link this two service_skeleton.cpp files separately, which will create two executables with two different service.
You can use the command given below for linking the services
Link "arithmetic_op" service:
g++ -L/usr/local/lib/ arithmetic_op.o arithmetic_op_server.skeleton.o Thrift_IDL_constants.o Thrift_IDL_types.o -lthrift -o arithmetic_op
Link "arithmetic_Demo" service:
g++ -L/usr/local/lib/ arithmetic_Demo.o arithmetic_Demo_server.skeleton.o Thrift_IDL_constants.o Thrift_IDL_types.o -lthrift -o arithmetic_Demo
Explanation:
-L: This option is used when you want compiler to link with libraries present in directory other than LD_LIBRARY_PATH environment variable. Here we have used to link with thrift library named "libthrift.a" or "libthrift.so". Here
.a represent static library where as
.so represent dynamic library.
-o: This option is used to give custom name to your executable by default it is a.out