Building the Host Program

The host program, written in C/C++ using OpenCL™ API calls, is built using the GNU C++ compiler (g++) which is based on GNU compiler collection (GCC). Each source file is compiled to an object file (.o) and linked with the Xilinx runtime (XRT) shared library to create the executable which runs on the host CPU.

TIP: g++ supports many standard GCC options which are not documented here. For information refer to the GCC Option Summary.

Compiling and Linking for x86

TIP: Setup the command shell or window as described in Setting up the Vitis Environment prior to running the tools.
Each source file of the host application is compiled into an object file (.o) using the g++ compiler.
g++ ... -c <source_file1> <source_file2> ... <source_fileN>
The generated object files (.o) are linked with the Xilinx Runtime (XRT) shared library to create the executable host program. Linking is performed using the -l option.
g++ ... -l <object_file1.o> ... <object_fileN.o>

Compiling and linking for x86 follows the standard g++ flow. The only requirement is to include the XRT header files and link the XRT shared libraries.

When compiling the source code, the following g++ options are required:

  • -I$XILINX_XRT/include/: XRT include directory.
  • -I$XILINX_VIVADO/include: Vivado tools include directory.
  • -std=c++11: Define the C++ language standard.

When linking the executable, the following g++ options are required:

  • -L$XILINX_XRT/lib/: Look in XRT library.
  • -lOpenCL: Search the named library during linking.
  • -lpthread: Search the named library during linking.
  • -lrt: Search the named library during linking.
  • -lstdc++: Search the named library during linking.
Note: In the Vitis Examples you may see the addition of xcl2.cpp source file, and the -I../libs/xcl2 include statement. These additions to the host program and g++ command provide access to helper utilities used by the example code, but are generally not required for your own code.

Compiling and Linking for Arm

TIP: Setup the command shell or window as described in Setting up the Vitis Environment prior to running the tools.

The host program (host.exe), is cross-compiled for an Arm processor, and linked using the following two step process:

  1. Compile the host.cpp into an object file (.o) using the GNU Arm cross-compiler version of g++:
    $XILINX_VITIS/gnu/aarch64/lin/aarch64-linux/bin/aarch64-linux-gnu-g++ \
    -D__USE_XOPEN2K8 -I$SYSROOT/usr/include/xrt -I$XILINX_VIVADO/include \
    -I$SYSROOT/usr/include -c -fmessage-length=0 -std=c++14 \
    --sysroot=$SYSROOT -o src/host.o ../src/host.cpp
  2. Link the object file with required libraries to build the executable application.
    $XILINX_VITIS/gnu/aarch64/lin/aarch64-linux/bin/aarch64-linux-gnu-g++ \
    -o host.exe src/host.o -lxilinxopencl -lpthread -lrt -lstdc++ -lgmp -lxrt_core \
    -L$SYSROOT/usr/lib/ --sysroot=$SYSROOT

When compiling the application for use with an embedded process, you must specify the sysroot for the application. The sysroot is part of the platform where the basic system root file structure is defined.

IMPORTANT: The above examples rely on the use of $SYSROOT environment variable that must be used to specify the location of the sysroot for your embedded platform.

The following are key elements to compiling the host code for an edge platform:

Compilation
  • The cross compiler needed is the aarch64-linux-gnu-g++ found in the Vitis installation hierarchy.
  • The required include paths are:
    • $SYSROOT/usr/include/xrt
    • $SYSROOT/usr/include
    • $XILINX_VIVADO/include
Linking
  • $SYSROOT/usr/lib: Library paths location.
  • $SYSROOT/usr/lib/xrt: XRT library path.
  • xilinxopencl: XRT required library.
  • pthread: XRT required library.
  • rt: XRT required library.
  • stdc++: XRT required library.
  • gmp: XRT required library.
  • xrt_core: XRT required library.