include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig
getting started
The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in ThriftInstallationWin32.
Requirements
Make sure that your system meets the requirements as noted in ThriftRequirements
- Thrift library files
- Thrift header files.
Installing the Thrift library
Installing the Thrift library is trivial to link with the generated code.
-
Download a snapshot of Thrift and extract if you haven't done so already - Direct Link
-
Navigate to lib/cpp using the terminal of your choice
-
Run make to compile
-
Run make install to install the library. Your user needs root permissions.
Generating the server code
In this example we use an imaginary file called your_thrift_file.thrift:
#!/usr/local/bin/thrift --gen cpp namespace cpp Test service Something { i32 ping() }
Now run:
thrift --gen cpp your_thrift_file.thrift
Exploring the generated code - The Server
The generated code should be under the gen-cpp directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).
- Something.cpp
- Something.h
-
Something_server.skeleton.cpp
- your_thrift_file_constants.cpp
- your_thrift_file_constants.h
- your_thrift_file_types.cpp
- your_thrift_file_types.h
Implementing the Server
Copy the generated server skeleton to a file named Something_server.cpp and keep the original:
cp Something_server.skeleton.cpp Something_server.cpp
When this server is run in console it prints "ping" on the console window each time the function is called from a client.
Here's the autogenerated skeleton file to illustrate how to write a server: Something_server.cpp