分类: LINUX
2010-09-25 15:31:46
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 .
Make sure that your system meets the requirements as noted in
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 -
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.
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
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_server.skeleton.cpp
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
#include "Something.h"
#include
#include
#include
#include
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using boost::shared_ptr;
using namespace Test;
class SomethingHandler : virtual public SomethingIf {
public:
SomethingHandler() {
// Your initialization goes here
}
int32_t ping() {
// Your implementation goes here
printf("ping\n");
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptrhandler(new SomethingHandler());
shared_ptrprocessor(new SomethingProcessor(handler));
shared_ptrserverTransport(new TServerSocket(port));
shared_ptrtransportFactory(new TBufferedTransportFactory());
shared_ptrprotocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}
To quickly build a binary using a single command use:
g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something
You need to point your compiler to the thrift include path (CXX flag: -I/usr/local/include/thrift)
g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o
g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o
You need to point your linker to the thrift library. (Linker flag: -lthrift or -l/usr/local/lib/libthrift.so
g++ -L/usr/local/lib -lthrift *.o -o Something_server
thrift generates a client interface, but you have to hook it up a bit on your own.
#include "Something.h" // As an example
#include
#include
#include
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace Test;
int main(int argc, char **argv) {
boost::shared_ptrsocket(new TSocket("localhost", 9090));
boost::shared_ptrtransport(new TBufferedTransport(socket));
boost::shared_ptrprotocol(new TBinaryProtocol(transport));
SomethingClient client(protocol);
transport->open();
client.ping();
transport->close();
return 0;
}
g++ -Wall -I/usr/local/include/thrift -c something_client.cpp -o client.o
g++ -L/usr/local/lib -lthrift client.o Something.o constants.o types.o -o Something_client
GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cpp
GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))
THRIFT_DIR := /usr/local/include/thrift
BOOST_DIR := /usr/local/include
INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)
.PHONY: all clean
all: something_server something_client
%.o: %.cpp
$(CXX) -Wall $(INC) -c $< -o $@
something_server: Something_server.o $(GEN_OBJ)
$(CXX) -L/usr/local/lib -lthrift $^ -o $@
something_client: Something_client.o $(GEN_OBJ)
$(CXX) -L/usr/local/lib -lthrift $^ -o $@
clean:
$(RM) *.o something_server something_client