如何通过CMake构建包含OpenCV、dlib的静态可执行文件
How to Build a Static, Self-Contained Executable with OpenCV and dlib
Hey there! Let's get your executable set up so it runs on any machine without needing to pre-install OpenCV or dlib. Your current CMakeLists.txt is a solid starting point, but we need to tweak it to enforce static linking for all dependencies—including the system libraries that dlib and OpenCV rely on.
Here's a revised, complete CMakeLists.txt that should do the trick:
cmake_minimum_required(VERSION 3.5.1) project(adc_fr) # Set C++ standard and enforce it set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Configure platform-specific static linking settings if(UNIX) # Linux: Force static linking of standard libraries and prioritize .a files set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++") set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") endif() if(MSVC) # Windows: Use static MSVC runtime to avoid VC redistributable requirements set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib") endif() # Build dlib as a static library set(DLIB_USE_STATIC_LIBS ON CACHE BOOL "Use static dlib library" FORCE) add_subdirectory(/media/sf_dlib-19.9/dlib dlib_build) # Find static OpenCV libraries set(OpenCV_STATIC ON CACHE BOOL "Use static OpenCV libraries" FORCE) find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui) # List your source files set(SOURCE_FILES FDetect.cpp FDetect.h FExtract.cpp FExtract.h main.cpp ) # Create the executable add_executable(adc_fr ${SOURCE_FILES}) # Link all static dependencies target_link_libraries(adc_fr PRIVATE dlib::dlib ${OpenCV_LIBS} ) # Optional: Add release-mode optimizations target_compile_options(adc_fr PRIVATE $<$<CONFIG:Release>:-O3> )
Key Changes Explained:
- Platform-Specific Static Flags: On Linux, we add flags to link static versions of the C/C++ standard libraries (prevents runtime dependency errors). On Windows, we force the static MSVC runtime so users don't need to install VC redistributables.
- Static dlib Build: The
DLIB_USE_STATIC_LIBSflag ensures dlib compiles as a static library instead of a shared one before we add its subdirectory. - Static OpenCV Search:
OpenCV_STATIC ONtells CMake to look for OpenCV's static library files instead of dynamic.so/.dllfiles. - Library Prioritization: We set
CMAKE_FIND_LIBRARY_SUFFIXESto prioritize static library extensions (.afor Linux,.libfor Windows) when CMake searches for dependencies. - Clean Linking: We use
target_link_librariesto connect your executable to dlib's exported static target and all required OpenCV static libraries.
Critical Notes:
- Ensure you have static builds of OpenCV on your system. If you compiled OpenCV yourself, enable
BUILD_SHARED_LIBS=OFFduring its CMake configuration. - For dlib, the static build is handled automatically via the flag we added—no pre-built dlib is needed unless you have custom configurations.
- On Linux, you may need to install static versions of system libraries OpenCV depends on (like
libpng-dev,libjpeg-devin their static package variants).
Once you run cmake and make (or build via your IDE), the resulting adc_fr executable will be fully self-contained and run on any compatible machine without requiring pre-installed OpenCV or dlib.
内容的提问来源于stack exchange,提问作者D R
相关产品推荐
相关产品推荐

