I've been trying to link tesseract library to my c++ project in Visual Studio 2019 for a couple of days and I finally managed to do it.
Any thread that I found or even official tesseract documentation do not have full list of instructions on what to do.
I'll list what I have done, hopefully it will help someone. I don't pretend its the optimal way to do so.
There are basic tips in official tesseract documentation.
Go to "Windows" section.
I did install sw
and cppan
but I guess it wasn't necessary.
The main thing here is installing vcpkg.
It requiers Git so I installed it.
then:
> cd c:tools
(I installed it in c:\tools
, you may choose any dir)
> git clone https://github.com/microsoft/vcpkg
> .\vcpkg\bootstrap-vcpkg.bat
> .\vcpkg\vcpkg install tesseract:x64-windows-static
(I used x64 version)
> .\vcpkg\vcpkg integrate install
At this point everything should work, they said. Headers should be included, libs should be linked. But none was working for me.
Change project configuration to Release x64 (or Release x86 if you installed x86 tesseract).
To include headers: Go to project properties -> C/C++ -> General. Set Additional Include Directories to C:\tools\vcpkg\installed\x64-windows-static\include
(or whereever you installed vcpkg)
To link libraries : project properties -> Linker -> General. Set Additional Library Directories to C:\tools\vcpkg\installed\x64-windows-static\lib
Project properties -> C/C++ -> Code Generation. Set Runtime Library to Multi-threaded(/MT)
. Otherwise I got errors like "runtime mismatch static vs DLL"
Tesseract lib couldn't link to its dependcies, so I added all libs that I had installed to C:\tools\vcpkg\installed\x64-windows-static\lib
.
Project properties -> Linker -> Input. I set Additional Dependencies to archive.lib;bz2.lib;charset.lib;gif.lib;iconv.lib;jpeg.lib;leptonica-1.80.0.lib;libcrypto.lib;libpng16.lib;libssl.lib;libwebpmux.lib;libxml2.lib;lz4.lib;lzma.lib;lzo2.lib;openjp2.lib;tesseract41.lib;tiff.lib;tiffxx.lib;turbojpeg.lib;webp.lib;webpdecoder.lib;webpdemux.lib;xxhash.lib;zlib.lib;zstd_static.lib;%(AdditionalDependencies)
And after that it finally compiled and launched.
But... api->Init
returned -1
. To work with tesseract you should have tessdata directory with .traineddata files for the languages you need.
Download tessdata. I got it from official docs.
BTW, tessdata_fast worked better than tessdata_best for my purposes :)
So I downloaded single "eng" file and saved it like C:\tools\TesseractData\tessdata\eng.traineddata
.
Then I added environment variable TESSDATA_PREFIX
with value C:\tools\TesseractData\tessdata
. I also added C:\tools\TesseractData
to Path variables (just in case)
And after all this it is finally working for me.