Part 2: Adding the Marlin Multicast SDK to the project

Step 1 - Add CMake module to find the SDK and download if not present

Create a folder called cmake in the tutorial directory by running the folling command from inside the build directory.

$ mkdir ../cmake

Create a file called marlin-multicastsdk.cmake inside the cmake folder with the following contents.

find_package(marlinMulticastSDK QUIET)
if(NOT marlinMulticastSDK_FOUND)
    message("-- marlinMulticastSDK not found. Using internal marlinMulticastSDK.")
    include(FetchContent)
    FetchContent_Declare(marlinMulticastSDK
        GIT_REPOSITORY https://gitlab.com/marlinprotocol/marlin.cpp.git
        GIT_TAG master
    )

    # Check if population has already been performed
    FetchContent_GetProperties(marlinMulticastSDK)
    string(TOLOWER "marlinMulticastSDK" lcName)
    if(NOT ${lcName}_POPULATED)
        # Fetch the content using previously declared details
        FetchContent_Populate(marlinMulticastSDK)

        # Bring the populated content into the build
        add_subdirectory(${${lcName}_SOURCE_DIR} ${${lcName}_BINARY_DIR} EXCLUDE_FROM_ALL)
    endif()
else()
    message("-- marlinMulticastSDK found. Using system marlinMulticastSDK.")
endif()

This snippet first checks if the SDK is installed already in the system or is downloaded already. If not, it uses the CMake FetchContent module to download the SDK from version control.

Step 2 - Include the SDK in the build

Add the following line to CMakeLists.txt:

include("${CMAKE_CURRENT_LIST_DIR}/cmake/marlin-multicastsdk.cmake")

This will include the above module in the CMake build process.

To use the SDK in our code, we must first link it to our original executable.

target_link_libraries(tutorial PUBLIC marlin::multicastsdk)

Once you have followed the above instructions, you should have a CMakeLists.txt that looks like this:

cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(tutorial VERSION 0.0.1 LANGUAGES CXX)

add_executable(tutorial
    main.cpp
)

# marlinMulticastSDK
include("${CMAKE_CURRENT_LIST_DIR}/cmake/marlin-multicastsdk.cmake")
target_link_libraries(tutorial PUBLIC marlin::multicastsdk)

Step 4 - Build and run executable

Build using

$ cmake .. -DCMAKE_BUILD_TYPE=Release
$ make

and run using

$ ./tutorial

Building should have worked and the executable should run (and still do nothing for now).

Conclusion

In this part, we learnt how to add the Marlin Multicast SDK to the project using CMake. In the next part we will learn how to use the Marlin Multicast SDK to connect to the Marlin Network.