How to correctly define CMakeLists.txt for Tesseract?

163 views
Skip to first unread message

Raphael Stonehorse

unread,
Feb 17, 2024, 1:06:27 PM2/17/24
to tesseract-ocr
I've been trying to use CMake for Tesseract compilation and building

With this CMakeLists.txt :

    cmake_minimum_required(VERSION 3.5)
    project(BasicExample)
   
    set(CMAKE_CXX_STANDARD 17)
   
    find_package(PkgConfig REQUIRED)
   
    pkg_check_modules(tesseract REQUIRED IMPORTED_TARGET tesseract)
    pkg_check_modules(leptonica REQUIRED IMPORTED_TARGET lept)
    pkg_check_moduleS(libcurl REQUIRED IMPORTED_TARGET libcurl)
   
    add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE BasicExample.cpp)
   
    target_link_libraries(BasicExample PUBLIC
        PkgConfig::leptonica
        PkgConfig::tesseract
        -lcurl
    )

I get these errors:

    raphy@raohy:~/tesseract/Examples$ cmake -B builddir
    -- The C compiler identification is GNU 12.3.0
    -- The CXX compiler identification is GNU 13.2.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/bin/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/c++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found PkgConfig: /usr/bin/pkg-config (found version "1.8.1")
    -- Checking for module 'tesseract'
    --   Found tesseract, version 5.3.4
    -- Checking for module 'lept'
    --   Found lept, version 1.82.0
    -- Checking for module 'libcurl'
    --   Found libcurl, version 8.2.1
    -- Configuring done (0.3s)
    -- Generating done (0.0s)
    -- Build files have been written to: /home/raphy/tesseract/Examples/builddir
    raphy@raohy:~/tesseract/Examples$
    raphy@raohy:~/tesseract/Examples$ cmake --build builddir/
    [ 50%] Building CXX object CMakeFiles/BasicExample.dir/BasicExample.cpp.o
    [100%] Linking CXX executable BasicExample
    /usr/bin/ld: /usr/local/lib/libtesseract.a(baseapi.cpp.o): in function `tesseract::TessBaseAPI::ProcessPagesInternal(char const*, char const*, int, tesseract::TessResultRenderer*)::{lambda(char const*)#1}::operator()(char const*) const':
    baseapi.cpp:(.text+0x13): undefined reference to `curl_easy_strerror'
    /usr/bin/ld: baseapi.cpp:(.text+0x3b): undefined reference to `curl_easy_cleanup'
    /usr/bin/ld: /usr/local/lib/libtesseract.a(baseapi.cpp.o): in function `tesseract::TessBaseAPI::ProcessPagesInternal(char const*, char const*, int, tesseract::TessResultRenderer*)':
    baseapi.cpp:(.text+0xad07): undefined reference to `curl_easy_init'
    /usr/bin/ld: baseapi.cpp:(.text+0xad48): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: baseapi.cpp:(.text+0xad5d): undefined reference to `curl_easy_strerror'
    /usr/bin/ld: baseapi.cpp:(.text+0xad89): undefined reference to `curl_easy_cleanup'
    /usr/bin/ld: baseapi.cpp:(.text+0xb26f): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: baseapi.cpp:(.text+0xb298): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: baseapi.cpp:(.text+0xb2c1): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: baseapi.cpp:(.text+0xb2fa): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: baseapi.cpp:(.text+0xb324): undefined reference to `curl_easy_setopt'
    /usr/bin/ld: /usr/local/lib/libtesseract.a(baseapi.cpp.o):baseapi.cpp:(.text+0xb3c9): more undefined references to `curl_easy_setopt' follow
    /usr/bin/ld: /usr/local/lib/libtesseract.a(baseapi.cpp.o): in function `tesseract::TessBaseAPI::ProcessPagesInternal(char const*, char const*, int, tesseract::TessResultRenderer*)':
    baseapi.cpp:(.text+0xb455): undefined reference to `curl_easy_perform'
    /usr/bin/ld: baseapi.cpp:(.text+0xb6b0): undefined reference to `curl_easy_cleanup'
    /usr/bin/ld: /usr/local/lib/libtesseract.a(tessdatamanager.cpp.o): in function `tesseract::TessdataManager::LoadArchiveFile(char const*)':
    tessdatamanager.cpp:(.text+0x199): undefined reference to `archive_read_new'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x1ad): undefined reference to `archive_read_support_filter_all'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x1b5): undefined reference to `archive_read_support_format_all'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x1c5): undefined reference to `archive_read_open_filename'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x1e6): undefined reference to `archive_entry_pathname'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x1f6): undefined reference to `archive_read_next_header'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x208): undefined reference to `archive_read_free'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x294): undefined reference to `archive_entry_size'
    /usr/bin/ld: tessdatamanager.cpp:(.text+0x2e1): undefined reference to `archive_read_data'
    collect2: error: ld returned 1 exit status
    gmake[2]: *** [CMakeFiles/BasicExample.dir/build.make:99: BasicExample] Error 1
    gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/BasicExample.dir/all] Error 2
    gmake: *** [Makefile:91: all] Error 2
    raphy@raohy:~/tesseract/Examples$

What am I missing? How to make it work?

Zdenko Podobny

unread,
Feb 17, 2024, 1:21:24 PM2/17/24
to tesser...@googlegroups.com
First of all: you should use tools you are familiar with. Your CMake configuration (CMakeLists.txt) does not look that way (you would use CMake to check required libraries, not PkgConfig, you would not hardcode curl for linking etc...),

Next. you should provide all the details to replicate the problem. What is missing (at least):
  1. BasicExample.cpp
  2. What OS you use (you put "WIN32 MACOSX_BUNDLE" to add_executable, what is quite surprising combination)
  3. How did you install Tesseract? Some error outputs indicate manual static linked tesseract. Why a static build? It could be quite tricky to link static library with external dependencies 
Best regards,

Zdenko


so 17. 2. 2024 o 19:06 Raphael Stonehorse <raphael.s...@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/c42c4e75-ae31-4a78-9152-839df7c00421n%40googlegroups.com.

Raphael Stonehorse

unread,
Feb 19, 2024, 4:49:07 AM2/19/24
to tesser...@googlegroups.com
Hi Zdenko,

thanks for your kind help and suggestions

I solved the issue, thanks to the help and suggestions, and explanations, I kindly received in StackOverFlow


Best regards
Raphy

Tom Morris

unread,
Feb 20, 2024, 12:02:14 PM2/20/24
to tesseract-ocr
On Monday, February 19, 2024 at 4:49:07 AM UTC-5 raphael.s...@gmail.com wrote:
I solved the issue, thanks to the help and suggestions, and explanations, I kindly received in StackOverFlow

In case someone has a similar issue in the future, the suggestion on StackOverflow was to use CMake's INTERFACE_LINK_LIBRARIES property.

...
pkg_check_modules(tesseract REQUIRED IMPORTED_TARGET tesseract)
# pkg-config doesn't know about dependencies of static libraries, so add these dependencies manually.
set_property(TARGET PkgConfig::tesseract APPEND PROPERTY INTERFACE_LINK_LIBRARIES curl)

target_link_libraries(BasicExample PUBLIC
    PkgConfig::tesseract # With that linkage CMake will automatically add linkage with curl.
)


There's more explanation in the StackOverflow answer.

Tom

Zdenko Podobny

unread,
Feb 20, 2024, 12:09:42 PM2/20/24
to tesser...@googlegroups.com
Any reason why to use an external 3rd party app that is not available on all platforms instead of cmake native function which is available everywhere cmake is?

Zdenko


ut 20. 2. 2024 o 18:02 Tom Morris <tfmo...@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages