Dear Wiki user, You have subscribed to a wiki page or wiki category on "Thrift Wiki" for change notification.
The "ThriftUsageC++" page has been changed by TimWilson-Brown. The comment on this change is: added link to Win32 instructions for C++. http://wiki.apache.org/thrift/ThriftUsageC%2B%2B?action=diff&rev1=18&rev2=19 -------------------------------------------------- = Getting started = - The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems. + 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 @@ -18, +18 @@ 1. 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}}}: - In this example we use an imaginary file called {{{your-thrift-file.thrift}}}: - {{{ - #!/usr/local/bin/thrift --gen cpp + {{{#!/usr/local/bin/thrift --gen cpp namespace cpp Test service Something { - i32 ping() - } - }}} - Now run: + . 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 @@ -48, +45 @@ * your-thrift-file_types.h === Implementing the Server === + Copy the generated server skeleton to a file named Something_server.cpp and keep the original: - 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 <protocol/TBinaryProtocol.h> @@ -100, +96 @@ return 0; } }}} + == Compiling/Building the server == + To quickly build a binary using a single command use: - == Compiling/Building the server == - - To quickly build a binary using a single command use: {{{ g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something }}} - === Compiling === - You need to point your compiler to the thrift include path (CXX flag: {{{ -I/usr/local/include/thrift}}}) {{{ @@ -118, +111 @@ 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 }}} - === Linking === You need to point your linker to the thrift library. (Linker flag: {{{ -lthrift }}} or {{{ -l/usr/local/lib/libthrift.so }}} {{{ ld -L/usr/local/lib -lthrift *.o -o Something_server }}} - == Generating the client code == - A client is a bit simpler to generate. {{{ @@ -159, +149 @@ return 0; } - }}}
