I am trying to compile a cmake project that requires Tesseract and OpenCV. Everything is being done in Ubuntu 20.04.
But I have been struggling to get it compiled.
I compiled Tesseract from source and have been going through several SO posts and whatever article I find online, but still no luck. I noticed that Tesseract doesn't export a .cmake
file under /usr/local/lib/cmake/
, so you cannot do find_package(Tesseract x.x.x REQUIRED)
.
Looking around, I see that people recommend doing it this way:
find_package(PkgConfig REQUIRED)
pkg_search_module(TESSERACT REQUIRED tesseract)
pkg_search_module(LEPTONICA REQUIRED lept)
include_directories(${TESSERACT_INCLUDE_DIRS})
include_directories(${LEPTONICA_INCLUDE_DIRS})
link_directories( ${TESSERACT_LIBRARY_DIRS} )
link_directories( ${LEPTONICA_LIBRARY_DIRS} )
Which seems to find something. After doing pkg_search_module(TESSERACT REQUIRED tesseract)
, I can see that TESSERACT_INCLUDE_DIRS
is populated with this list: /usr/local/include;/usr/include/leptonica
. This is despite the fact that Tesseract header files are located under /usr/local/include/tesseract/
. Anyways, running cmake
goes through without an error.
However, running make
afterwards simply adds strange errors of
error: 'l_int32' does not name a type; did you mean 'u_int32_t'?
54 | l_int32 num; /*!< number of unknowns */
| ^~~~~~~
to my project. Even though I have not even included any header files of Tesseract or Leptonica.
This post is the only one that seems to be close to a solution.
I have also tried the following without success:
include_directories(/usr/local/include/tesseract/)
include_directories(/usr/local/include/leptonica/)
.cmake
file so that I can use find_package(Tesseract 4.1.1 REQUIRED)
directly.So Ubuntu's libtesseract-dev
package unfortunately does not bundle its CMake config files. But that doesn't mean you shouldn't look for them first.
find_package(Tesseract 4)
if (NOT Tesseract_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(Tesseract REQUIRED IMPORTED_TARGET tesseract lept)
add_library(Tesseract::libtesseract ALIAS PkgConfig::Tesseract)
endif ()
# ... later ...
target_link_libraries(my_app PRIVATE Tesseract::libtesseract)
Always use imported targets. Never ever use include_directories
or link_directories
. Forget those commands exist at all.
This code will try to find the Tesseract::libtesseract
target in the Tesseract CMake package. If it can't, it will look for a pkg-config module for Tesseract. If it can't find that, the build fails (REQUIRED
) but if it can, the pkg_check_modules
command will create the PkgConfig::tesseract
imported target. The add_library(.. ALIAS ..)
command adds an alias to make the target names match no matter which method succeeded.
You then use target_link_libraries
to link your application to Tesseract. CMake will handle the usage requirements (ie. link and include directories, compiler flags, etc.) for you.