Developing C++ Programs on Linux

This section describes how to build and run a native client application on Linux.

Note: When compiling external projects or applications that are used or referenced by the GemFire native client, make sure that you compile them for the same target architecture as your native client installation. For example, if you installed the 32-bit (x86) version of the native client, compile your external projects for 32-bit (x86) architecture.

Step 1: Set Environment Variables

Set the native client environment variables on each Linux host. For each case, productDir is the path to the native client product directory.

For Bourne and Korn shells (sh, ksh, bash)
GFCPP=<productDir>;export GFCPP
PATH=$GFCPP/bin:$PATH;export PATH
LD_LIBRARY_PATH=$GFCPP/lib:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH

Step 2: Compile C++ Clients and Dynamically Link Them to the GemFire Library

On Linux, the g++ compiler is supported. To build and link a C++ client to GemFire on Linux, the compilation command line must include the arguments listed in the following table.
Table 1. Compiler Arguments (Linux)
Argument Explanation
-D_REENTRANT Required to compile Linux programs in a thread-safe way.
-m32 Enables 32-bit compilation.
-m64 Enables 64-bit compilation.
-I$GFCPP/include Specifies the native client include directory.
The following table lists the linker switches that must be present on the command line when dynamically linking to the GemFire library.
Table 2. Linker Switches (Dynamically Linking on Linux)
Argument Explanation
-rpath $GFCPP/lib Tells the linker to look in $GFCPP/lib for libraries on which the GemFire library depends.
-L$GFCPP/lib Tells the linker where to find the named libraries.
-o durableclient Tells the linker to output an object file named 'durableclient'.
-lgfcppcache Links the GemFire C++ cache library to the compiled executable.

The following examples compile and link the $GFCPP/SampleCode/quickstart/cpp/DurableClient.cpp client to the durableclient output file.

Compiling and Dynamically Linking on Linux for 32-bit
g++ \
-D_REENTRANT \
-03 \
-Wall \
-m32 \
-I$GFCPP/include \
cpp/DurableClient.cpp \
cpp/plugins/DurableCacheListener.cpp \
-o cpp/DurableClient \
-L$GFCPP/lib \
-Wl,-rpath,$GFCPP/lib \
-lgfcppcache
Compiling and Dynamically Linking on Linux for 64-bit
g++ \
-D_REENTRANT \
-03 \
-Wall \
-m64 \
-I$GFCPP/include \
cpp/DurableClient.cpp \
cpp/plugins/DurableCacheListener.cpp \
-o cpp/DurableClient \
-L$GFCPP/lib \
-Wl,-rpath,$GFCPP/lib \
-lgfcppcache

Step 3: Make Sure the GemFire Library Can Be Loaded

When the C++ application is dynamically linked to the GemFire library, the library must be dynamically loadable. To verify that the GemFire library is available for loading, make sure you have added the path GemFireDir/lib to the LD_LIBRARY_PATH environment variable, where GemFireDir is the path to the GemFire product directory.