From 1f8257adadd01a6bbaf39e72be72cdb36cf32303 Mon Sep 17 00:00:00 2001 From: Chevi koren Date: Wed, 29 Apr 2026 11:40:23 +0300 Subject: [PATCH] Merge pull request #28423 from Chevi-Koren:fix-clp-vcpkg-final MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix Clp library detection for vcpkg on Windows #28423 ## Problem Building OpenCV with `WITH_CLP=ON` on Windows (vcpkg) fails: LINK : fatal error LNK1181: cannot open input file 'libClp.lib' vcpkg generates `Clp.lib` and `CoinUtils.lib`, but OpenCV expected `libClp.lib`/`libCoinUtils.lib`. ## Solution Modernize CLP detection with a robust, platform-aware approach: 1. **CMake `find_package(Clp CONFIG)`**: detects vcpkg and uses imported targets (`Coin::Clp`, `Coin::CoinUtils`) 2. **Fallback**: pkg-config on Unix/Linux 3. **Skip** Android/iOS ## Benefits ✅ Fixes Windows build with vcpkg ✅ Uses modern CMake targets ✅ Backward compatible with Unix/Linux ✅ Safe for CI and mobile builds ✅ Graceful fallback if CLP unavailable ## Testing - **Windows 11 + vcpkg**: build succeeds - **No CLP installed**: configure/build succeeds - **Linux/Unix**: existing workflows unaffected - **Android/iOS**: properly skipped ## Changes - Update `cmake/OpenCVFindLibsPerf.cmake` - Replace hardcoded library names with CMake targets - Add mobile platform guards - Preserve backward compatibility Fixes #26592 --- ### Pull Request Readiness Checklist - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch (`4.x`) - [x] There is a reference to the original bug report and related work (#26592) - [x] The feature is well documented and sample code can be built with the project CMake - [x] No additional tests required - this is a build system fix that maintains existing functionality --- CMakeLists.txt | 4 ++ cmake/OpenCVFindLibsPerf.cmake | 80 +++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4138c81098..1edd5f53cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1871,6 +1871,10 @@ if(WITH_EIGEN OR HAVE_EIGEN) status(" Eigen:" HAVE_EIGEN THEN "YES (ver ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})" ELSE NO) endif() +if(WITH_CLP OR HAVE_CLP) + status(" CLP:" HAVE_CLP THEN "YES" ELSE NO) +endif() + if(WITH_OPENVX OR HAVE_OPENVX) status(" OpenVX:" HAVE_OPENVX THEN "YES (${OPENVX_LIBRARIES})" ELSE "NO") endif() diff --git a/cmake/OpenCVFindLibsPerf.cmake b/cmake/OpenCVFindLibsPerf.cmake index 861a39c47f..6976aeac1b 100644 --- a/cmake/OpenCVFindLibsPerf.cmake +++ b/cmake/OpenCVFindLibsPerf.cmake @@ -130,43 +130,71 @@ if(HAVE_EIGEN) endif() endif() + # --- Clp --- # Ubuntu: sudo apt-get install coinor-libclp-dev coinor-libcoinutils-dev ocv_clear_vars(HAVE_CLP) -if(WITH_CLP) - if(UNIX) - ocv_check_modules(CLP clp) - if(CLP_FOUND) - set(HAVE_CLP TRUE) - if(NOT ${CLP_INCLUDE_DIRS} STREQUAL "") - ocv_include_directories(${CLP_INCLUDE_DIRS}) + +if(WITH_CLP AND NOT ANDROID AND NOT IOS) + + # 1. Modern CMake config (vcpkg / system packages) + find_package(Clp CONFIG QUIET) + if(Clp_FOUND AND TARGET Coin::Clp AND TARGET Coin::CoinUtils) + set(HAVE_CLP 1) + list(APPEND OPENCV_LINKER_LIBS Coin::Clp Coin::CoinUtils) + endif() + + # 2. pkg-config fallback (Unix) + if(NOT HAVE_CLP AND UNIX) + find_package(PkgConfig QUIET) + if(PKG_CONFIG_FOUND) + pkg_check_modules(CLP QUIET clp) + if(CLP_FOUND) + set(HAVE_CLP 1) + ocv_include_directories(SYSTEM ${CLP_INCLUDE_DIRS}) + list(APPEND OPENCV_LINKER_LIBS ${CLP_LIBRARIES}) endif() - list(APPEND OPENCV_LINKER_LIBS ${CLP_LIBRARIES}) endif() endif() - if(NOT CLP_FOUND) - find_path(CLP_INCLUDE_PATH "coin" - PATHS "/usr/local/include" "/usr/include" "/opt/include" - DOC "The path to Clp headers") + # 3. Manual fallback (last resort) + if(NOT HAVE_CLP) + find_path(CLP_INCLUDE_PATH + NAMES coin/ClpSimplex.hpp + PATHS /usr/include /usr/local/include /opt/include + DOC "The path to Clp headers" + ) + if(CLP_INCLUDE_PATH) - ocv_include_directories(${CLP_INCLUDE_PATH} "${CLP_INCLUDE_PATH}/coin") - get_filename_component(_CLP_LIBRARY_DIR "${CLP_INCLUDE_PATH}/../lib" ABSOLUTE) - set(CLP_LIBRARY_DIR "${_CLP_LIBRARY_DIR}" CACHE PATH "Full path of Clp library directory") - link_directories(${CLP_LIBRARY_DIR}) - if(UNIX) - set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} Clp CoinUtils m) - else() - if(MINGW) - set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} Clp CoinUtils) - else() - set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} libClp libCoinUtils) + # Support both: + # /usr/include/coin/ClpSimplex.hpp + # /usr/local/include/ClpSimplex.hpp + /coin + ocv_include_directories( + ${CLP_INCLUDE_PATH} + "${CLP_INCLUDE_PATH}/coin" + ) + + find_library(CLP_LIBRARY + NAMES Clp libClp + ) + find_library(COINUTILS_LIBRARY + NAMES CoinUtils libCoinUtils + ) + + if(CLP_LIBRARY AND COINUTILS_LIBRARY) + set(HAVE_CLP 1) + list(APPEND OPENCV_LINKER_LIBS + ${CLP_LIBRARY} + ${COINUTILS_LIBRARY} + ) + if(UNIX) + list(APPEND OPENCV_LINKER_LIBS m) endif() endif() - set(HAVE_CLP TRUE) endif() endif() -endif(WITH_CLP) + +endif() # --- ARM KleidiCV if(WITH_KLEIDICV) @@ -219,4 +247,4 @@ if(WITH_FASTCV) set(HAVE_FASTCV FALSE CACHE BOOL "FastCV status") endif() endif() -endif(WITH_FASTCV) +endif(WITH_FASTCV) \ No newline at end of file