如何为CMakeLists.txt设置特定包路径并使用指定OpenCV3库文件
Got it, let's work through this. Since you only have the required shared libraries and no full OpenCV package (which includes the CMake config files find_package() depends on), we'll need to set up dependencies manually in your CMakeLists.txt. Here's how to do it step by step:
Step 1: Add OpenCV Header Paths
First, you'll need the corresponding OpenCV header files (the include/opencv2 directory tree) — make sure you have these placed somewhere, like /opt/opencv/include. CMake needs to know where to find these headers so your code compiles correctly.
Add this line to your CMakeLists.txt (replace the path with your actual header directory):
include_directories(/opt/opencv/include)
If you prefer modern target-based CMake (recommended for newer projects), link the headers directly to your target instead:
target_include_directories(your_target_name PRIVATE /opt/opencv/include)
Step 2: Locate Your Shared Libraries
Use CMake's find_library() command to pinpoint each of your pre-downloaded .so files. This ensures CMake uses exactly the libraries you provided, not system-wide versions.
Add these lines, updating the PATHS value to your actual library directory:
# Find opencv_core find_library(OPENCV_CORE_LIB NAMES opencv_core PATHS /opt/opencv/lib NO_DEFAULT_PATH ) # Find opencv_imgproc find_library(OPENCV_IMGPROC_LIB NAMES opencv_imgproc PATHS /opt/opencv/lib NO_DEFAULT_PATH ) # Find opencv_imgcodecs find_library(OPENCV_IMGCODECS_LIB NAMES opencv_imgcodecs PATHS /opt/opencv/lib NO_DEFAULT_PATH ) # Make sure all libraries were found if(NOT OPENCV_CORE_LIB OR NOT OPENCV_IMGPROC_LIB OR NOT OPENCV_IMGCODECS_LIB) message(FATAL_ERROR "One or more required OpenCV libraries are missing from the specified path!") endif()
The NO_DEFAULT_PATH flag tells CMake not to search standard system directories, so it only uses your custom libraries.
Step 3: Link Libraries to Your Target
Finally, link the found libraries to your project's executable or library target. For example, if your target is named my_opencv_app, add:
target_link_libraries(my_opencv_app PRIVATE ${OPENCV_CORE_LIB} ${OPENCV_IMGPROC_LIB} ${OPENCV_IMGCODECS_LIB} )
Full Example CMakeLists.txt Snippet
Putting it all together, here's what your relevant code might look like:
cmake_minimum_required(VERSION 3.10) project(MyOpenCVProject) # Set C++ standard (match what your project uses) set(CMAKE_CXX_STANDARD 11) # Include OpenCV headers include_directories(/opt/opencv/include) # Locate individual OpenCV libraries find_library(OPENCV_CORE_LIB NAMES opencv_core PATHS /opt/opencv/lib NO_DEFAULT_PATH ) find_library(OPENCV_IMGPROC_LIB NAMES opencv_imgproc PATHS /opt/opencv/lib NO_DEFAULT_PATH ) find_library(OPENCV_IMGCODECS_LIB NAMES opencv_imgcodecs PATHS /opt/opencv/lib NO_DEFAULT_PATH ) # Check for missing libraries if(NOT OPENCV_CORE_LIB OR NOT OPENCV_IMGPROC_LIB OR NOT OPENCV_IMGCODECS_LIB) message(FATAL_ERROR "Required OpenCV libraries not found!") endif() # Add your executable add_executable(my_opencv_app main.cpp) # Link libraries to the executable target_link_libraries(my_opencv_app PRIVATE ${OPENCV_CORE_LIB} ${OPENCV_IMGPROC_LIB} ${OPENCV_IMGCODECS_LIB} )
Quick Notes
- Replace
/opt/opencv/includeand/opt/opencv/libwith your actual header and library paths. - If your
.sofiles have version suffixes (likelibopencv_core.so.3.4), either rename them to remove the version (so CMake finds them asopencv_core) or add the versioned names to theNAMESparameter (e.g.,NAMES opencv_core opencv_core.3.4). - Double-check that your header files match the version of the shared libraries — mismatched versions will cause compilation or runtime errors.
内容的提问来源于stack exchange,提问作者Yves

