Principle:Tencent Ncnn Dependency Free Build
| Knowledge Sources | |
|---|---|
| Domains | Embedded_Systems, Build_Engineering |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Self-contained replacement implementations of external libraries and runtime facilities, enabling a fully statically-linked build with zero third-party dependencies for embedded deployment.
Description
Dependency-free building is an engineering strategy in which every external library that the framework would normally link against is replaced with a minimal, built-in implementation that provides just enough functionality for the framework's needs. This is critical for deeply embedded targets (bare-metal microcontrollers, custom SoCs, RTOS environments) where standard system libraries, package managers, or dynamic linkers may not exist.
The replacement libraries include:
SimpleOCV -- A minimal subset of OpenCV's image I/O and data structures (cv::Mat, cv::imread, cv::imwrite, cv::resize, Scalar, Rect, Point) built on top of stb_image and stb_image_write. It provides just enough image handling for loading test images and visualizing results without the full 50+ MB OpenCV dependency.
SimpleOMP -- A minimal OpenMP runtime that implements the LLVM OpenMP ABI for the most common usage pattern: #pragma omp parallel for num_threads(N). It provides omp_get_max_threads(), omp_set_num_threads(), omp_get_thread_num(), and the internal __kmpc_* functions the compiler emits.
SimpleSTL -- A minimal C++ Standard Template Library replacement providing std::vector, std::string, std::pair, std::sort, std::swap, operator new/delete, and other essentials. This removes the dependency on libstdc++ or libc++.
SimpleMath -- Pure software implementations of standard math functions (sinf, cosf, sqrtf, expf, logf, powf, tanhf, erf, etc.) for platforms that lack a floating-point math library.
stb_image / stb_image_write -- Single-header public-domain libraries for decoding and encoding JPEG, PNG, BMP, and other image formats, embedded directly in the source tree.
Vulkan Header Fix -- Compatibility shims that define newer Vulkan API structures and function pointer types when building against an older Vulkan SDK, enabling the same source code to compile against a wide range of SDK versions.
Usage
Enable these replacements via CMake options (NCNN_SIMPLEOCV, NCNN_SIMPLEOMP, NCNN_SIMPLESTL, NCNN_SIMPLEMATH) when targeting platforms where the full external libraries are unavailable or undesirable due to size constraints. On standard desktop/mobile platforms, the real external libraries are preferred for full functionality and performance.
Theoretical Basis
Compile-time substitution pattern:
// Each replacement is guarded by a platform flag
#if NCNN_SIMPLEOCV
#include "simpleocv.h" // minimal OpenCV replacement
#else
#include <opencv2/core/core.hpp> // real OpenCV
#endif
#if NCNN_SIMPLEOMP
#include "simpleomp.h" // minimal OMP runtime
// implements omp_get_max_threads(), omp_set_num_threads(), etc.
#endif
#if NCNN_SIMPLESTL
#include "simplestl.h" // minimal STL (vector, string, ...)
#else
#include <vector>
#include <string>
#endif
#if NCNN_SIMPLEMATH
#include "simplemath.h" // software float math
// float sinf(float), cosf(float), expf(float), ...
#endif
Minimal operator new/delete (SimpleSTL):
void* operator new(size_t size) { return malloc(size); }
void operator delete(void* ptr) { free(ptr); }
void* operator new[](size_t size) { return malloc(size); }
void operator delete[](void* ptr){ free(ptr); }
Binary size impact:
Full build with OpenCV + libstdc++ + libm: ~50 MB+
Dependency-free build (all Simple* enabled): < 2 MB