We will create a minimal C++ program that uses LightlyEdge C++ SDK. We will use CMake to build it. CMake is a popular cross-platform tool that allows us to add the library easily to our C++ project.
Prerequisites
Verify CMake is installed with cmake --version
. Minimum required version: 3.13. If not, install it.
Code Walkthrough
Our minimal program will instantiate a LightlyEdge object and exit. Code for this tutorial is provided in examples/02_first_run
directory. Before starting this tutorial, copy the model file to examples/lightly_model.tar
and verify that your project layout is as follows:
lightly_edge_sdk_cpp
├── ...
└── examples
├──02_first_run
│ ├── CMakeLists.txt
│ └── main.cpp
└── lightly_model.tar
The 02_first_run
subfolder in examples
contains two files. There is CMakeLists.txt
with build instructions:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
# Set the project name.
project(main)
# Add search paths for FindLightlyEdgeSDK.cmake module.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../dist/cmake/LightlyEdgeSDK")
# Specify source files to build an executable.
add_executable(main main.cpp)
# Specify the C++ standard.
target_compile_features(main PUBLIC cxx_std_11)
# Link LightlyEdge SDK.
find_package(LightlyEdgeSDK REQUIRED)
target_link_libraries(main LightlyEdgeSDK::lightly_edge)
if(WIN32)
# Copy dlls from /bin to the executable folder after building the target.
file(GLOB DLL_FILES "${CMAKE_SOURCE_DIR}/../lightly_edge_sdk_cpp/bin/*.dll"
"${CMAKE_SOURCE_DIR}/../../bin/*.dll")
add_custom_command(
TARGET main
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILES}
$<TARGET_FILE_DIR:main>
COMMENT
"Copying .dll files from lightly_edge_sdk_cpp/bin to the executable output folder."
)
endif()
The second file is main.cpp
with the program logic. This code uses the TAR model to initialize a lightly_edge_sdk::LightlyEdge
, which is the central object in LightlyEdge C++ SDK. It throws an exception if something goes wrong.
#include <iostream>
#include "lightly_edge_sdk.h"
int main() {
std::cout << "Initializing LightlyEdge..." << std::endl;
auto lightly_edge = LightlyEdge::new_from_tar("../lightly_model.tar", config);
std::cout << "LightlyEdge successfully initialized!" << std::endl;
return 0;
}
Namespace with core LightlyEdge SDK functionality.
Definition lightly_edge_error.h:15
Definition lightly_edge_rs_bindings.h:43
Build and Run
We are ready to build and run the program! Execute the following commands in a terminal:
cd 02_first_run
cmake -B build
cmake --build build
./build/main
.\build\[build_type]\main.exe
If you see the following output, congratulations! Everything is set up correctly and you can start using LightlyEdge.
Initializing LightlyEdge...
LightlyEdge successfully initialized!
- Note
- If you see the following uncaught exception please check that you are in the correct directory and that
lightly_model.tar
is at the correct location: IO Error: No such file or directory (os error 2): "../lightly_model.tar"
Next Steps
Next, we will use LightlyEdge for our first real-world application, Similarity Search.
Note that the code above uses exceptions, which should be handled in a real-world application. LightlyEdge also provides a no throw interface. For more details on the topic see Error Handling.