Skip to content

Commit 9c9990f

Browse files
committed
fix
1 parent fb60f86 commit 9c9990f

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ UpgradeLog*.XML
124124
/opencv_artifacts/
125125
/tesseract_artifacts/
126126
/vcpkg_installed/
127+
/src/build/
127128
/opencv_files
128129
/test/OpenCvSharp.Tests/dll/x64/*.dll
129130
/test/OpenCvSharp.Tests/dll/x86/*.dll

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ https://github.com/shimat?tab=packages
218218
git submodule update --init --recursive
219219
```
220220

221-
2. Build OpenCV from source (Tesseract is automatically installed via vcpkg):
221+
2. Build OpenCV from source (Tesseract and other native dependencies are automatically installed via vcpkg):
222+
```powershell
223+
# Install vcpkg dependencies (only needed once, or after vcpkg.json changes)
224+
C:\vcpkg\vcpkg.exe install --triplet x64-windows-static --overlay-triplets cmake\triplets --x-install-root vcpkg_installed
225+
```
222226
```powershell
223227
.\build_opencv_windows.ps1
224228
# Use -Jobs N to control parallel build (default: 4)
@@ -229,10 +233,11 @@ https://github.com/shimat?tab=packages
229233
3. Build the native wrapper `OpenCvSharpExtern`:
230234
```powershell
231235
cmake -S src -B src\build -G "Visual Studio 17 2022" -A x64 `
232-
-D CMAKE_PREFIX_PATH="opencv_artifacts" `
236+
-D "CMAKE_PREFIX_PATH=$PWD\opencv_artifacts" `
233237
-D CMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" `
234238
-D VCPKG_TARGET_TRIPLET=x64-windows-static `
235-
-D VCPKG_INSTALLED_DIR="vcpkg_installed"
239+
-D "VCPKG_INSTALLED_DIR=$PWD\vcpkg_installed" `
240+
-D "VCPKG_OVERLAY_TRIPLETS=$PWD\cmake\triplets"
236241
cmake --build src\build --config Release
237242
```
238243

@@ -241,6 +246,8 @@ https://github.com/shimat?tab=packages
241246
```powershell
242247
dotnet build src/OpenCvSharp/OpenCvSharp.csproj -c Release
243248
```
249+
> **Note on the native DLL:** Step 3 automatically copies `OpenCvSharpExtern.dll` into `test/OpenCvSharp.Tests/` as a post-build step, so `dotnet test` works out of the box.
250+
> For your own application, either add `src\build\OpenCvSharpExtern\Release` to `PATH`, or copy `OpenCvSharpExtern.dll` alongside your app's output directory.
244251
245252
### Ubuntu
246253
- Build OpenCV with opencv_contrib: https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html

build_opencv_windows.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ Write-Host "Done. OpenCV installed to: $installDir"
133133
Write-Host ""
134134
Write-Host "Next step — configure and build OpenCvSharpExtern:"
135135
Write-Host " cmake -S src -B src\build -G `"$vsGenerator`" -A x64 ``"
136-
Write-Host " -D CMAKE_PREFIX_PATH=`"$installDir`" ``"
136+
Write-Host " -D `"CMAKE_PREFIX_PATH=`$PWD\opencv_artifacts`" ``"
137137
Write-Host " -D CMAKE_TOOLCHAIN_FILE=`"$vcpkgToolchain`" ``"
138138
Write-Host " -D VCPKG_TARGET_TRIPLET=x64-windows-static ``"
139-
Write-Host " -D VCPKG_INSTALLED_DIR=`"$vcpkgInstalledDir`" ``"
140-
Write-Host " -D VCPKG_OVERLAY_TRIPLETS=`"$RepoRoot/cmake/triplets`""
139+
Write-Host " -D `"VCPKG_INSTALLED_DIR=`$PWD\vcpkg_installed`" ``"
140+
Write-Host " -D `"VCPKG_OVERLAY_TRIPLETS=`$PWD\cmake\triplets`""
141141
Write-Host " cmake --build src\build --config Release"

cmake/opencv_build_options.cmake

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,14 @@ set(BUILD_opencv_wechat_qrcode ON CACHE BOOL "" FORCE)
4545
set(WITH_GSTREAMER OFF CACHE BOOL "" FORCE)
4646
set(WITH_ADE OFF CACHE BOOL "" FORCE)
4747

48-
# Use a flat install layout (avoids x64/vc17/ subdirectories; libs go to staticlib/)
49-
set(OpenCV_INSTALL_BINARIES_PREFIX "" CACHE STRING "" FORCE)
48+
# On Windows, disable bundled 3rd-party lib builds so OpenCV uses the vcpkg-provided
49+
# versions instead. This ensures OpenCVModules.cmake references paths in
50+
# vcpkg_installed/ (which exist) rather than uninstalled paths in opencv_artifacts/.
51+
# These packages are guaranteed to be present via vcpkg.json dependencies.
52+
if(WIN32)
53+
set(BUILD_ZLIB OFF CACHE BOOL "" FORCE)
54+
set(BUILD_TIFF OFF CACHE BOOL "" FORCE)
55+
set(BUILD_JPEG OFF CACHE BOOL "" FORCE)
56+
set(BUILD_PNG OFF CACHE BOOL "" FORCE)
57+
set(BUILD_WEBP OFF CACHE BOOL "" FORCE)
58+
endif()

src/OpenCvSharpExtern/CMakeLists.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,26 @@ if(APPLE)
1717
endif()
1818

1919
# On Windows, OpenCV is built as a static library; tell the Windows Pack config to look for static libs.
20+
# Also force /MT (static CRT) to match OpenCV's build configuration, and enable /MP for parallel compilation.
2021
if(WIN32)
2122
set(OpenCV_STATIC ON)
23+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
24+
add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)
2225
endif()
2326

2427
find_package(OpenCV REQUIRED)
2528

2629
# iconv support isn't automatic on some systems
2730
find_package(Iconv QUIET)
28-
if(Iconv_FOUND)
29-
ocv_target_link_libraries(${the_module} Iconv::Iconv)
30-
else()
31-
ocv_target_compile_definitions(${the_module} PRIVATE "NO_ICONV=1")
31+
if(NOT Iconv_FOUND)
32+
add_compile_definitions(NO_ICONV=1)
3233
endif()
3334

3435
if(OpenCV_FOUND)
3536
include_directories(${OpenCV_INCLUDE_DIRS})
3637
#set(LIBS ${LIBS} ${OpenCV_LIBRARIES})
3738

3839
if(CMAKE_GENERATOR MATCHES "Visual Studio")
39-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
40-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
41-
4240
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib")
4341
endif()
4442

@@ -50,6 +48,10 @@ if(OpenCV_FOUND)
5048
target_link_libraries(OpenCvSharpExtern ${OpenCV_LIBRARIES})
5149
endif()
5250

51+
if(Iconv_FOUND)
52+
target_link_libraries(OpenCvSharpExtern Iconv::Iconv)
53+
endif()
54+
5355
# Link Tesseract on Windows (using vcpkg-style prefix via CMAKE_PREFIX_PATH)
5456
if(WIN32 AND NOT EMSCRIPTEN)
5557
find_package(Tesseract QUIET)

vcpkg.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"dependencies": [
3-
{
4-
"name": "tesseract",
5-
"platform": "windows"
6-
}
3+
{"name": "tesseract", "platform": "windows"},
4+
{"name": "zlib", "platform": "windows"},
5+
{"name": "libpng", "platform": "windows"},
6+
{"name": "libjpeg-turbo", "platform": "windows"},
7+
{"name": "tiff", "platform": "windows"},
8+
{"name": "libwebp", "platform": "windows"}
79
],
810
"builtin-baseline": "1e199d32ad53aab1defda61ce41c380302e3f95c"
911
}

0 commit comments

Comments
 (0)