C++ Shared Library Examples

Navigate to examples under the ouster-sdk source directory, which should contain several linux example folders building against the sdk library. To run each use the example.bash script.

  1. Compiled-In Linking Example

  2. Shared Linking Example

  3. Static Linking Example

Compiled In Linking example

::

This is a simple example on how to build and link a cpp project with the ouster_pcap library.

Bash script to run the example in a container:

#! /bin/bash

set -ex
currentDir="$(cd $(dirname $0) && pwd)"
baseDir=$currentDir/../..
tempDir="$(mktemp -d)"
APT_PROXY=${APT_PROXY:-""}

baseImage="ubuntu:22.04"
if ! [ -z "$1" ]; then
  baseImage="$1"
fi

trap 'rm -rf $tempDir' EXIT
trap 'echo \*\*\* ERROR on line: $LINENO exit_code: $?' ERR

cd $baseDir

pwd

docker build -f $currentDir/Dockerfile --iidfile=$tempDir/iid \
       --network host \
       --build-arg APT_PROXY="$APT_PROXY" \
       --build-arg BASE=$baseImage .

docker run --rm $(cat $tempDir/iid)

Dockerfile:

ARG BASE="ubuntu:22.04"
FROM ${BASE}
ARG APT_PROXY=""

ENV WORKSPACE=/root

RUN /bin/sh -c "if ! [ -z \"$APT_PROXY\" ]; then \
        echo 'Using Proxy for APT'; \
        echo 'Acquire::http::Proxy \"$APT_PROXY\";' > /etc/apt/apt.conf.d/01proxy; \
    fi"

RUN apt-get update \
    && apt-get install -y python3 python3-venv python3-pip \
    && mkdir -p $WORKSPACE/scripts/dev_script_library

COPY ./scripts/dev.py ./scripts/requirements.txt \
    $WORKSPACE/scripts/
COPY ./scripts/dev.py ./scripts/requirements.txt \
    ./scripts/dev_script_library/build_libs.py \
    ./scripts/dev_script_library/context.py \
    ./scripts/dev_script_library/dev_dependencies.py \
    $WORKSPACE/scripts/dev_script_library/

RUN python3 -m venv $WORKSPACE/venv && \
    . $WORKSPACE/venv/bin/activate && \
    cd $WORKSPACE && \
    python3 ./scripts/dev.py utils install-system-packages

COPY . $WORKSPACE/sdk/
COPY tests/pcaps/OS-2-32-U0_v2.0.0_1024x10.pcap examples/compiled_in_linking_example/CMakeLists.txt \
     examples/compiled_in_linking_example/main.cpp  $WORKSPACE/

RUN cd $WORKSPACE && \
    cmake -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF . && \
    cmake --build . --parallel 4

CMD $WORKSPACE/pcap_test /root/OS-2-32-U0_v2.0.0_1024x10.pcap

Shared Linking example

::

This is a simple example on how to build a shared library and link a cpp project with the ouster_pcap library.

Bash script to run the example in a container:

#! /bin/bash

set -ex
currentDir="$(cd "$(dirname "$0")" && pwd)"
baseDir="$currentDir/../.."
tempDir="$(mktemp -d)"
VCPKG_BINARY_SOURCES=${VCPKG_BINARY_SOURCES:-""}
APT_PROXY=${APT_PROXY:-""}

baseImage="ubuntu:22.04"
if ! [ -z "$1" ]; then
  baseImage="$1"
fi

trap 'rm -rf $tempDir' EXIT
trap 'echo \*\*\* ERROR on line: $LINENO exit_code: $?' ERR

cd "$baseDir"
pwd
docker build -f "$currentDir/Dockerfile" --iidfile="$tempDir/iid" \
  --network host \
  --build-arg BASE="$baseImage" \
  --build-arg APT_PROXY="$APT_PROXY" \
  --build-arg VCPKG_BINARY_SOURCES="$VCPKG_BINARY_SOURCES" .

docker run --rm "$(cat "$tempDir/iid")"

Dockerfile:

# ============================== BASE CONTAINER ==============================
ARG BASE="ubuntu:22.04"
FROM ${BASE} AS BASE_IMAGE
ARG APT_PROXY=""
ENV WORKSPACE=/root

RUN /bin/sh -c "if ! [ -z \"$APT_PROXY\" ]; then \
        echo 'Using Proxy for APT'; \
        echo 'Acquire::http::Proxy \"$APT_PROXY\";' > /etc/apt/apt.conf.d/01proxy; \
    fi"

RUN apt-get update \
    && apt-get install -y python3 python3-venv python3-pip \
    && mkdir -p $WORKSPACE/scripts/dev_script_library

# ============================== BUILD STAGE ==============================
FROM BASE_IMAGE AS BUILD
ARG VCPKG_BINARY_SOURCES=""
ENV VCPKG_BINARY_SOURCES=$VCPKG_BINARY_SOURCES

COPY ./scripts/dev.py ./scripts/requirements.txt \
    $WORKSPACE/scripts/
COPY ./scripts/dev.py ./scripts/requirements.txt \
    ./scripts/dev_script_library/build_libs.py \
    ./scripts/dev_script_library/context.py \
    ./scripts/dev_script_library/dev_dependencies.py \
    $WORKSPACE/scripts/dev_script_library/

RUN python3 -m venv $WORKSPACE/venv && \
    . $WORKSPACE/venv/bin/activate && \
    cd $WORKSPACE && \
    python3 ./scripts/dev.py utils install-vcpkg-package-requirements && \
    python3 ./scripts/dev.py utils enable-local-vcpkg

ENV INSTALL_DIR="/usr/local"

COPY . $WORKSPACE/

RUN cd $WORKSPACE && \
    cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DBUILD_EXAMPLES=OFF \
        -DBUILD_SHARED_LIBRARY=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF \
        -DCMAKE_TOOLCHAIN_FILE=$WORKSPACE/.osdkv2/vcpkg/scripts/buildsystems/vcpkg.cmake . &&\
    cmake --build . --parallel 4 --target install

# ============================== FINAL STAGE ==============================
FROM BASE_IMAGE

RUN apt-get update && apt-get install -y \
    build-essential \
    cmake \
    libeigen3-dev \
    libflatbuffers-dev \
    && rm -rf /var/lib/apt/lists/*

ENV WORKSPACE=/root
ENV INSTALL_DIR="/usr/local"

COPY --from=BUILD $INSTALL_DIR $INSTALL_DIR
COPY tests/pcaps/OS-2-32-U0_v2.0.0_1024x10.pcap examples/shared_linking_example/CMakeLists.txt \
     examples/shared_linking_example/main.cpp  $WORKSPACE/

RUN export CMAKE_PREFIX_PATH="$INSTALL_DIR" &&\
    mkdir -p $WORKSPACE/build &&\
    cd $WORKSPACE/build &&\
    cmake $WORKSPACE && cmake --build . --parallel 4

CMD $WORKSPACE/build/pcap_test /root/OS-2-32-U0_v2.0.0_1024x10.pcap

Static Linking example

::

This is a simple example on how to build a static shared library and link a cpp project with the ouster_pcap library.

Bash script to run the example in a container:

#! /bin/bash

set -ex
currentDir="$(cd $(dirname $0) && pwd)"
baseDir=$currentDir/../..
tempDir="$(mktemp -d)"
VCPKG_BINARY_SOURCES=${VCPKG_BINARY_SOURCES:-""}
APT_PROXY=${APT_PROXY:-""}

trap 'rm -rf $tempDir' EXIT
trap 'echo \*\*\* ERROR on line: $LINENO exit_code: $?' ERR

cd "$baseDir"
pwd

docker build -f $currentDir/Dockerfile --iidfile=$tempDir/iid \
       --network host \
       --build-arg VCPKG_BINARY_SOURCES="$VCPKG_BINARY_SOURCES" \
       --build-arg APT_PROXY="$APT_PROXY" .

docker run --rm $(cat $tempDir/iid)

Dockerfile:

# ============================== BASE CONTAINER ==============================
FROM ubuntu:22.04 AS BASE_IMAGE
ENV INSTALL_DIR="/usr/local"
ENV WORKSPACE=/root
ARG APT_PROXY=""

RUN /bin/sh -c "if ! [ -z \"$APT_PROXY\" ]; then \
        echo 'Using Proxy for APT'; \
        echo 'Acquire::http::Proxy \"$APT_PROXY\";' > /etc/apt/apt.conf.d/01proxy; \
    fi"

RUN apt-get update \
    && apt-get install -y python3 python3-venv

COPY ./scripts/dev.py ./scripts/requirements.txt \
    $WORKSPACE/scripts/
COPY ./scripts/dev.py ./scripts/requirements.txt \
    ./scripts/dev_script_library/build_libs.py \
    ./scripts/dev_script_library/context.py \
    ./scripts/dev_script_library/dev_dependencies.py \
    $WORKSPACE/scripts/dev_script_library/

RUN python3 -m venv $WORKSPACE/venv \
    && . $WORKSPACE/venv/bin/activate \
    && python3 -m pip install -r $WORKSPACE/scripts/requirements.txt \
    && cd $WORKSPACE \
    && python3 ./scripts/dev.py utils install-system-packages


# ============================== BUILD WITH DEV SCRIPTS STAGE ==============================
FROM BASE_IMAGE AS BUILD_W_DEV_SCRIPTS
COPY . $WORKSPACE/
RUN cd $WORKSPACE \
    && rm -rf build \
    && python3 -m venv $WORKSPACE/venv \
    && . $WORKSPACE/venv/bin/activate \
    && $WORKSPACE/scripts/dev.sh build cpp --install-dir $INSTALL_DIR --no-examples \
    --build-type Release --threads 4 --use-system-libs


# ============================== BUILD WITHOUT DEV SCRIPTS STAGE ==============================
FROM BASE_IMAGE AS BUILD_WO_DEV_SCRIPTS

COPY . $WORKSPACE/

RUN python3 -m venv $WORKSPACE/venv \
    && . $WORKSPACE/venv/bin/activate \
    && cd $WORKSPACE


ENV INSTALL_DIR="/usr/local"

COPY . $WORKSPACE/

RUN cd $WORKSPACE \
    && cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DBUILD_EXAMPLES=OFF \
        -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF . \
    && cmake --build . --parallel 4 --target install


# ============================== BUILD TESTER (WITH DEV SCRIPTS) ==============================
FROM BASE_IMAGE AS BUILD_TESTER_W_DEV
ENV INSTALL_DIR="/usr/local"
COPY --from=BUILD_W_DEV_SCRIPTS $INSTALL_DIR $INSTALL_DIR
COPY tests/pcaps/OS-2-32-U0_v2.0.0_1024x10.pcap examples/static_linking_example/CMakeLists.txt \
     examples/static_linking_example/main.cpp  $WORKSPACE/

RUN export CMAKE_PREFIX_PATH="$INSTALL_DIR" \
    && mkdir -p $WORKSPACE/build \
    && cd $WORKSPACE/build \
    && cmake $WORKSPACE -DCMAKE_BUILD_TYPE=Release \
    && cmake --build . --parallel 4


# ============================== BUILD TESTER (WITHOUT DEV SCRIPTS) ==============================
FROM BASE_IMAGE AS BUILD_TESTER_WO_DEV
ENV INSTALL_DIR="/usr/local"
COPY --from=BUILD_WO_DEV_SCRIPTS $INSTALL_DIR $INSTALL_DIR
COPY tests/pcaps/OS-2-32-U0_v2.0.0_1024x10.pcap examples/static_linking_example/CMakeLists.txt \
     examples/static_linking_example/main.cpp  $WORKSPACE/

RUN export CMAKE_PREFIX_PATH="$INSTALL_DIR" \
    && mkdir -p $WORKSPACE/build \
    && cd $WORKSPACE/build \
    && cmake $WORKSPACE -DCMAKE_BUILD_TYPE=Release \
    && cmake --build . --parallel 4


# ============================== FINAL RUNNER STAGE ==============================
FROM BASE_IMAGE AS FINAL_RUNNER
WORKDIR /test

COPY tests/pcaps/OS-2-32-U0_v2.0.0_1024x10.pcap .

COPY --from=BUILD_TESTER_W_DEV /root/build/pcap_test ./pcap_test_with_dev_scripts

COPY --from=BUILD_TESTER_WO_DEV /root/build/pcap_test ./pcap_test_without_dev_scripts

CMD echo "--- RUNNING TEST BUILT WITH DEV SCRIPTS ---" \
    && ./pcap_test_with_dev_scripts ./OS-2-32-U0_v2.0.0_1024x10.pcap \
    && echo "\n--- RUNNING TEST BUILT WITHOUT DEV SCRIPTS ---" \
    && ./pcap_test_without_dev_scripts ./OS-2-32-U0_v2.0.0_1024x10.pcap