Tesseract Init returns ld error

The test code snippet is textbook style:

  if (tess.**Init**(nullptr, "eng")) { // (const char *datapath, const char *language)
    cerr << "Tesseract initialization failed" << endl;
    return -3;
  }

The baseapi.h definition is:

  int **Init**(const char *datapath, const char *language, OcrEngineMode mode,
           char **configs, int configs_size,
           const std::vector<std::string> *vars_vec,
           const std::vector<std::string> *vars_values,
           bool set_only_non_debug_params);
  int Init(const char *datapath, const char *language, OcrEngineMode oem) {
    return Init(datapath, language, oem, nullptr, 0, nullptr, nullptr, false);
  }
  int **Init**(const char *datapath, const char *language) {
    return Init(datapath, language, OEM_DEFAULT, nullptr, 0, nullptr, nullptr,
                false);
  }
  // In-memory version reads the traineddata file directly from the given
  // data[data_size] array, and/or reads data via a FileReader.
  int **Init**(const char *data, int data_size, const char *language,
           OcrEngineMode mode, char **configs, int configs_size,
           const std::vector<std::string> *vars_vec,
           const std::vector<std::string> *vars_values,
           bool set_only_non_debug_params, FileReader reader);

The Init call in my code appears to have the right parameters, and its call to another member Init also has the right parameters. But KDevelop reports the following error:

/home/reza/projects/kde/TextExtract/build> make -j8
[ 50%] Building CXX object CMakeFiles/TextExtract.dir/main.cpp.o
[100%] Linking CXX executable TextExtract
/usr/bin/ld: CMakeFiles/TextExtract.dir/main.cpp.o: in function `main':
/home/reza/projects/kde/TextExtract/main.cpp:47:(.text+0x29d): undefined reference to `tesseract::TessBaseAPI::TessBaseAPI()'
/usr/bin/ld: /home/reza/projects/kde/TextExtract/main.cpp:84:(.text+0x40a): undefined reference to `tesseract::TessBaseAPI::~TessBaseAPI()'
/usr/bin/ld: /home/reza/projects/kde/TextExtract/main.cpp:84:(.text+0x4fa): undefined reference to `tesseract::TessBaseAPI::~TessBaseAPI()'
/usr/bin/ld: CMakeFiles/TextExtract.dir/main.cpp.o: in function `tesseract::TessBaseAPI::Init(char const*, char const*)':
/usr/local/include/tesseract/baseapi.h:206:(.text._ZN9tesseract11TessBaseAPI4InitEPKcS2_[_ZN9tesseract11TessBaseAPI4InitEPKcS2_]+0x43): undefined reference to `tesseract::TessBaseAPI::Init(char const*, char const*, tesseract::OcrEngineMode, char**, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TextExtract.dir/build.make:151: TextExtract] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/TextExtract.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
*** Failure: Exit code 2 ***

I’m a total newbie in KDevelop (and C++). I tried the following (from baseapi.h):

    if (tess.**Init**(nullptr, "eng", OEM_DEFAULT,
           nullptr,
           0,
           nullptr,
           nullptr,
           false)) {

and

  if (tess.**Init**(nullptr, "eng", OEM_DEFAULT)) {
    cerr << "Tesseract initialization failed" << endl;
    return -3;
  }

But these didn’t work either. What is the self-inflicted error here, please? Thanks.

Regards.

looks like general C++ programming to me. I see no hints of OpenCV in any of this.

Thanks, @crackwitz. I understand just enough to be dangerous. It is a Tesseract reference(s) error. My CMakeLists.txt is:

cmake_minimum_required( VERSION 3.5 )
project( TextExtract )
find_package( OpenCV REQUIRED)
find_package( Tesseract REQUIRED )
include_directories( ${Tesseract_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )
link_directories( ${Tesseract_LIBRARY_DIRS} ${OpenCV_LIBRARY_DIRS} )
add_executable( TextExtract main.cpp)
target_link_libraries( TextExtract ${OpenCV_LIBS} ${Tesseract_LIBRARY_DIRS} )
install(TARGETS TextExtract RUNTIME DESTINATION bin)

When I built OpenCV from source, the Tesseract variables were retrieved by CMake as follows:

Tesseract_DIRS            /usr/local/bin/tesseract
Tesseract_INCLUDE   NOT FOUND
Tesseract_LIBRARY    NOT FOUND

Tesseract and Leptonica were installed from source:

$ tesseract --version
tesseract 5.3.4-47-gd5e00
 leptonica-1.84.2

The KDevelop IDE is able to retrieve the include headers, but then after that my limited knowledge hits a roadblock because the Tesseract library is not being linked. There is a folder:
/usr/local/lib/cmake/tesseract with a bunch of files but none for linking with user code?

I need help in linking the Tesseract library through the CMakeLists.txt statement(s). Otherwise, it will be “Hello Python” :melting_face: for me. Just one more statement in the CMakeLists.txt will help me to overcome my roadblock. Thanks.

Regards.
P.S.
I’ve tried Satya’s sample too and the roadblock is identical - cannot link to Tesseract.

Hello @crackwitz,

Here’s my tesseract installation:

$ which tesseract
/usr/local/bin/tesseract
$ pkg-config --libs tesseract
-L/usr/local/lib -ltesseract

I updated my CMakeLists.txt file as follows:

cmake_minimum_required( VERSION 3.5 )
project( TextExtract )
find_package( OpenCV REQUIRED)
find_package( Tesseract REQUIRED )
include_directories( ${Tesseract_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )
set( Tesseract_LIBRARY_DIRS "/usr/local/lib/tesseract" )
link_directories( ${Tesseract_LIBRARY_DIRS} ${OpenCV_LIBRARY_DIRS} )
add_executable( TextExtract main.cpp)
target_link_libraries( TextExtract ${OpenCV_LIBS} ${Tesseract_LIBRARY_DIRS} )
install(TARGETS TextExtract RUNTIME DESTINATION bin)

The error message is now different:

/home/reza/projects/kde/TextExtract/build> make -j8
make[2]: *** No rule to make target '/usr/local/lib/tesseract', needed by 'TextExtract'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/TextExtract.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
*** Failure: Exit code 2 ***

I’m assuming that it does not like the set statement. What would be different approach? Thanks.

Regards.

If my memory is good you don’t need tesseract in your cmakelist.txt

1 Like

Thanks! I’m probably confusing myself because the initial error was that one of Tesseract destructor functions was not found. I’ll give it a try because I had disabled it and was focussing more noise reduction and other preparatory work before performing the OCR.

Regards.