Building the sample application

The P4 API for C/C++ distribution contains the necessary header files (*.h) and libraries (libclient.a, librpc.a, libsupp.a, libp4sslstub.a) required to compile and link a client application. The distribution also includes a sample application in C++, p4api.cc.

In general, the process is similar to most APIs: compile your application sources and then link them with the API libraries. The precise steps needed vary from platform to platform.

The sample application, p4api.cc, is a portable, minimal P4 Server application, which we can use as an example. For purposes of this example, assume a Linux system.

Compile and link p4api.cc as follows:

cc -c -o p4api.o -D_GNU_SOURCE -O2 -DOS_LINUX -DOS_LINUX24 \
> -DOS_LINUXX86 -DOS_LINUX24X86 -I. -Imsgs -Isupport -Isys p4api.cc

gcc -o p4api p4api.o libclient.a librpc.a libsupp.a libp4sslstub.a

The preprocessor definitions (-D definition) vary from platform to platform.

Run the generated build_sample:

/bin.sh build_sample

Test the sample by running p4api info or most other P4 Server commands:

p4api changes -m 1

Expected output:

Change 372 on 2002/09/23 by you@you:home:sunflower 'Building API'

The sample’s functionality comes from the default implementation of the ClientUser class, linked from the libclient.a library and the rest of the library code, for which source code is not included. The source for the default implementation is provided in the P4 API for C/C++ distribution as clientuser.cc.