Building C++ Apps

Dronecode SDK C++ apps are written in standard C++ (c++11) and can be built using your preferred build system, compiler and linker toolchain. The only requirement is that the build system must be able to locate the SDK C++ headers and libraries (installed as described here).

The SDK itself uses the cmake build system, and we recommend that you do too. CMake is an open-source, cross-platform toolchain that allows you to build your examples on macOS, Linux and Windows using the same build file definition.

Below we explain how to set up a minimal build setup (CMakeLists.txt) file for your application.

Build Definition File - CMakeLists.txt

Cmake uses a definition file named CMakeLists.txt to define the project. This specifies the name of the project, compiler flags for different platforms and targets, where to find dependencies (libraries and header files), source files to build, and the name of the generated binary. CMakeLists.txt is typically stored in the root directory of your app project.

The sections below show how you can set up the file for when the SDK is installed system wide (the default) or locally.

SDK system-wide installation is not supported on Windows (see #155). Instead build the app using a local SDK installation.

Windows gurus, we'd love your help to implement this).

SDK Installed System-wide

A "template" CMakeLists.txt is shown below. Most of file is boilerplate - the main things you need to change are your_project_name, your_executable_name and your_source_file. You should also make sure that any plugins used by your app are listed in the target_link_libraries section.

cmake_minimum_required(VERSION 2.8.12)

## Specify your project's name
project(your_project_name)

# Enable strict handling of warnings
if(NOT MSVC)
    add_definitions("-std=c++11 -Wall -Wextra -Werror")
else()
    add_definitions("-std=c++11 -WX -W2")
    include_directories(${CMAKE_SOURCE_DIR}/../../install/include)
    link_directories(${CMAKE_SOURCE_DIR}/../../install/lib)
endif()

# Specify your app's executable name, and list of source files used to create it.
add_executable(your_executable_name
    your_source_file.cpp
    # ... any other source files
)

# Specify your app's executable name and a list of linked libraries
target_link_libraries(your_executable_name
    dronecode_sdk          #All apps link against dronecode_sdk library
    dronecode_sdk_action      # If action plugin used by app ...
    dronecode_sdk_telemetry   #If telemetry plugin used by app ...
    # ... Any other linked libraries
)

The file format and required modifications are self-explanatory. If additional information is required see the cmake documentation.

SDK Installed Locally

Where possible install the SDK system wide and follow the instructions in the previous section. You will need to install locally on Windows.

CMakeLists.txt is more complicated when the SDK is installed locally, because you need to specify where the build should find both headers and library files relative to your current directory.

The changes to the file (with respect to the previous version) are shown below. You have to change the same information as before: your_project_name, your_executable_name and your_source_file.

The example file below assumes that the SDK was installed locally into the directory DronecodeSDK/install/ and that the application is nested two levels deep (at the same level as the SDK example code).

cmake_minimum_required(VERSION 2.8.12)

## Specify your project's name
project(your_project_name)

# Enable strict handling of warnings
if(NOT MSVC)
    add_definitions("-std=c++11 -Wall -Wextra -Werror")
else()
    add_definitions("-std=c++11 -WX -W2")
endif()

# Specify your app's executable name, and list of source files used to create it.
add_executable(your_executable_name
    your_source_file.cpp
    # ... any other source files
)

Remove/comment this section (Not using "system wide" SDK)

#target_link_libraries(your_executable_name
#    dronecode_sdk  #All apps link against dronecode_sdk library
    # ... any other linked libraries
#)

Add section specifying local path to include directories and the SDK library.

# Specify include directories
include_directories(
    ${CMAKE_SOURCE_DIR}/../../install/include
)

# Specify variable 'dronecode_sdk_lib' containing location of the SDK library.
if(MSVC)
    set(dronecode_sdk_lib "${CMAKE_SOURCE_DIR}/../../install/lib/dronecode_sdk.lib")
else()
    set(dronecode_sdk_lib "${CMAKE_SOURCE_DIR}/../../install/lib/libdronecode_sdk.so")
endif()

# Specify your app's executable name and a list of linked libraries
target_link_libraries(your_executable_name
    # Add  'dronecode_sdk_lib' variable defining where the SDK library can be found.
    ${dronecode_sdk_lib}
    # dronecode_sdk # Link against library named dronecode_sdk_lib in standard install location
    # ... add any other linked libraries
)

Building the App

This section assumes that you have already built and installed the SDK C++ Library and that your example has a CMakeLists.txt in its root directory.

Linux/macOS

To create and run the app on Linux/macOS:

  1. First create a build directory and run cmake:
    cd /path/to/your/application/   # Open a prompt and navigate to your application 
    mkdir build && cd build   # Create a build directory and navigate into it
    cmake ..   # Create make files for your current platform
    

    Make files for release binaries are created by default. Use the flag -DCMAKE_BUILD_TYPE=Debug to build debug binaries:

    cmake -DCMAKE_BUILD_TYPE=Debug ..
    
  2. Build the project
    make
    
  3. Execute the file (in your build directory):
    ./your_executable_name  # Run your new executable
    

Windows

To create and run the app on Windows:

  1. Open the VS2017 x64 Native Tools Command Prompt.
  2. Navigate to the app source directory and enter the commands as shown below:
    cd /path/to/your/application/   # Open a prompt and navigate to your application 
    mkdir build && cd build   # Create a build directory and navigate into it
    cmake .. -G "Visual Studio 15 2017 Win64" # Create make files for your current platform
    cmake --build .  # Build the file 
    .\Debug\your_executable_name.exe  # Run your new executable
    

We only show a small fraction of what Cmake is capable of! Check out the links below for more information and examples.

© Dronecode 2017. License: CC BY 4.0            Updated: 2018-07-12 03:09:32

results matching ""

    No results matching ""