Linux
- Prerequisites
- Build C++ SDK
- Other ways to build C++ SDK
- Creating Kaa applications
This section describes how to build Kaa C++ SDK on a Linux-based machine and install a Kaa application.
NOTE: This guide is verified against:
- Ubuntu 14.04 LTS Desktop 64-bit
- Ubuntu 16.04 LTS Desktop 64-bit
Prerequisites
To build Kaa C++ SDK, make sure to install the following components:
sudo apt-get install libboost1.55-all-dev
wget tar -xf boost_1_55_0.tar.gz cd boost_1_55_0 ./bootstrap.sh sudo ./bjam cxxflags=-std=c++11 install
Install the Avro C++ library manually.
wget tar -zxf avro-cpp-1.7.5.tar.gz cd avro-cpp-1.7.5/ cmake . sudo make install
Install the Botan 1.11 library. The stock 1.10 version is not recommended for C++11 projects, so the newer 1.11 version is used in Kaa C++ SDK.
wget tar -zxf 1.11.28.tar.gz cd botan-1.11.28/ ./configure.py sudo make install
After dependencies are installed, dynamic loader’s links (and, optionally, its cache) will be updated. This is required so that loader knows where the libraries are located. Run the command below to update the links.
sudo ldconfig
Build C++ SDK
To build the C++ endpoint SDK, do the following:
-
Unpack the C++ SDK archive.
tar xfv cpp-sdk-archive-name.tar.gz
Create a directory where the SDK will be built.
mkdir build cd build
Configure the build via CMake.
cmake ..
Perform build.
make
Other ways to build C++ SDK
There are alternative ways to build Kaa C++ SDK, such as using Docker container and Nix shell.
Docker container
You can use Docker containers to build your C++ SDK and run Kaa C/C++ demo applications with all necessary environment pre-installed.
NOTE: Docker natively supports x86-64 architecture only.
To build Kaa C++ SDK using Docker:
Follow the official docker installation guide for your operating system.
Download the Docker container.
docker pull kaaproject/demo_c_cpp_environment
Compile SDK, demo applications, etc. for your container.
docker run -it kaaproject/demo_c_cpp_environment bash
To mount a host directory to the container file system, add this flag to the previous command:
-v FOLDER_WITH_DEMO:FOLDER_INSIDE_CONTAINER.
For example, the following command will build a demo project and direct you to the container shell where you can run tests immediately.
docker run -v FOLDER_WITH_DEMO:/opt/demo -it kaaproject/demo_c_cpp_environment bash -c 'cd /opt/demo/ && chmod +x build.sh && ./build.sh clean build && bash'
After the compilation, launch the demo binary located in the
/opt/demo/build/directory of the container file system.
NOTE: To run a compiled binary on some other host, you need to have all third-party libraries like
boost, etc. pre-installed.
See also Docker deployment.
Nix shell
Kaa C and C++ SDK build environments use the Nix package manager for CI purposes.
To build Kaa C++ SDK, install Nix as described in the Nix guide.
After you installed Nix on your system, run the following command from the root directory of Kaa C++ SDK.
nix-shell
Nix will download and compile all SDK dependencies and prepare your environment for development.
Creating Kaa applications
After you installed the required dependencies and built the C++ SDK, you can build and run your Kaa application.
Directory structure
The recommended directory structure for applications using Kaa C++ SDK is as follows.
CMakeLists.txt src/ KaaDemo.cpp kaa/
CMakeLists.txt– file describing your application for the CMake build system.
src/KaaDemo.cpp– file containing the application source code.
kaa/– directory where you unpack the Kaa SDK archive.
Build system
Although you can use other build systems, it is recommended that you use CMake to tie application code together with the C++ SDK.
Below is an example how to configure the
CMakeLists.txt file for the application.
Specify minimum CMake version required.
cmake_minimum_required(VERSION 2.8.12)
Specify the project name and enable C++11 standard.
project(kaa-demo CXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Add
kaa-demoexecutable file and specify the source file
src/KaaDemo.cpp.
add_executable(kaa-demo src/KaaDemo.cpp)
Add Kaa SDK subdirectory and specify Kaa SDK as a dependency for your application. As a result, the SDK will be built before building the application.
add_subdirectory(kaa) target_link_libraries(kaa-demo kaacpp)
Code
Below is a simple and straightforward example of the application code.
The application will initialize and start Kaa client, which involves connecting to Kaa server.
After the
kaaClient->start(); line, Kaa client is up and running in a dedicated thread.
Finally, the endpoint access token is printed to
stdout and stop running.
#include <iostream> #include <kaa/Kaa.hpp> #include <kaa/IKaaClient.hpp> int main() { //Initialize the Kaa endpoint. auto kaaClient = kaa::Kaa::newClient(); // Run the Kaa endpoint. kaaClient->start(); //Print access token std::cout << "Endpoint access token: " << kaaClient->getEndpointAccessToken() << std::endl; // Stop the Kaa endpoint. kaaClient->stop(); return 0; }
Build application
To build the example application above, run these commands.
mkdir build cd build cmake -DKAA_MAX_LOG_LEVEL=3 .. make
As a result, CMake will generate
Makefile in the
build/ directory.
Using the
KAA_MAX_LOG_LEVEL=3 will prevent mixing up the application output with Kaa SDK info level messages.
The
make command builds the application.