You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在RedHat上编译Tesseract 5.3.3与Leptonica 1.84.1时出现未定义引用错误

编译Tesseract时出现Leptonica函数未定义引用错误

问题描述

安装Leptonica后编译Tesseract,执行make命令时出现大量Leptonica相关函数的未定义引用错误,核心错误信息如下:

CXXLD    tesseract
/usr/bin/ld: src/tesseract-tesseract.o: in function `main':
tesseract.cpp:(.text.startup+0xdd): undefined reference to `setMsgSeverity'
/usr/bin/ld: tesseract.cpp:(.text.startup+0x296): undefined reference to `getLeptonicaVersion'
...
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:4481: tesseract] Error 1
make[1]: Leaving directory '/opt/tesseract/tesseract-5.3.3'
make: *** [Makefile:8199: all-recursive] Error 1

复现环境与步骤

  • 环境:Windows通过WSL使用Docker容器,测试过RedHat ubi9和Ubuntu镜像,均出现相同错误
  • 操作步骤:
    1. 启动Docker容器:
      docker container run  -v /mnt/d/:/mnt -it --rm redhat/ubi9:latest
      
    2. 容器内安装依赖并编译Leptonica:
      yum install gcc-c++
      yum install -y libtool autoconf automake libpng-devel libtiff-devel libwebp-devel openjpeg2 libjpeg-devel libjpeg-turbo giflib zlib-devel libarchive libcurl-devel
      yum install sudo
      sudo dnf install wget
      sudo yum install zip unzip
      yum install autoconf-archive
      wget https://github.com/DanBloomberg/leptonica/releases/download/1.84.1/leptonica-1.84.1.tar.gz
      mv leptonica-1.84.1.tar.gz ./opt/tesseract
      gunzip leptonica-1.84.1.tar.gz
      tar -xvf leptonica-1.84.1.tar
      cd leptonica-1.84.1
      mkdir build
      cd build
      ../configure
      make
      sudo make install
      make -j4 check
      
    3. 编译Tesseract(出错阶段):
      cd /opt/tesseract/
      wget https://github.com/tesseract-ocr/tesseract/archive/refs/tags/5.3.3.tar.gz
      gunzip 5.3.3.tar.gz 
      tar -xvf 5.3.3.tar 
      rm 5.3.3.tar
      cd tesseract-5.3.3/
      export LEPTONICA_CFLAGS="-I/usr/local/include/leptonica"
      export LEPTONICA_LIBS="-L/usr/local/lib/"
      export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
      export CXXFLAGS="-I/usr/include -I/usr/include/libpng16 -I/usr/include/leptonica -lpng -ljpeg -lz"
      export LDFLAGS="-L/usr/lib"
      export LIBLEPT_HEADERSDIR="/usr/include/leptonica"
      ./autogen.sh
      LIBLEPT_HEADERSDIR=/usr/local/include/leptonica ./configure --prefix=/usr/local/ --with-extra-libraries=/usr/local/lib
      make
      

解决方案

核心问题分析

  • LEPTONICA_LIBS仅指定了库路径,未明确要链接的Leptonica库(-llept),导致链接器无法找到对应函数实现
  • 安装Leptonica后未更新系统动态库缓存,系统可能无法识别新安装的库
  • CXXFLAGS中错误添加了链接阶段的参数(-lpng等),编译与链接参数混用导致配置异常

修正步骤

  1. 安装Leptonica后更新动态库缓存
    在Leptonica执行sudo make install后,添加:

    sudo ldconfig
    
  2. 修正Tesseract编译的环境变量
    清理错误的环境变量,重新设置正确的参数:

    cd /opt/tesseract/tesseract-5.3.3/
    make clean  # 清理之前的编译残留
    export LEPTONICA_CFLAGS="-I/usr/local/include/leptonica"
    export LEPTONICA_LIBS="-L/usr/local/lib -llept"  # 添加-llept指定链接Leptonica库
    export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"
    unset CXXFLAGS  # 移除错误的编译参数
    unset LDFLAGS
    export LIBLEPT_HEADERSDIR="/usr/local/include/leptonica"
    
  3. 重新执行配置与编译

    ./autogen.sh
    ./configure --prefix=/usr/local/
    make
    sudo make install
    

内容的提问来源于stack exchange,提问作者ankur kapoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:35:11