otiai10 / gosseract

Go package for OCR (Optical Character Recognition), by using Tesseract C++ library
https://pkg.go.dev/github.com/otiai10/gosseract
MIT License
2.74k stars 290 forks source link

ld: library 'leptonica' not found #306

Open filiprejmus opened 8 months ago

filiprejmus commented 8 months ago

Summary

I tried installing gosseract for my project and getting

/usr/local/go/pkg/tool/darwin_arm64/link: running clang++ failed: exit status 1
ld: library 'leptonica' not found
clang++: error: linker command failed with exit code 1

Code sample

client := gosseract.NewClient()
defer client.Close()
bodyBytes, err := io.ReadAll("IMAGE_BYTES")
if err != nil {
    return "", fmt.Errorf("unable to read file content: %v", err)
}
client.SetImageFromBytes(bodyBytes)
body, err = client.Text()
if err != nil {
    return "", fmt.Errorf("unable to convert file: %v", err)
}

Environment

uname -a
Darwin MacBook-Pro-von-Filip.local 23.4.0 Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:06 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T8103 arm64
go env
GO111MODULE='on'
GOARCH='arm64'
GOBIN='/Users/filiprejmus/go/bin'
GOCACHE='/Users/filiprejmus/Library/Caches/go-build'
GOENV='/Users/filiprejmus/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/filiprejmus/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/filiprejmus/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/filiprejmus/repos/the-platform/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/tx/nflzyw31689gryfy6_m7_z4r0000gn/T/go-build916142092=/tmp/go-build -gno-record-gcc-switches -fno-common'
go version
go version go1.22.1 darwin/arm64
tesseract --version
tesseract 5.3.4
 leptonica-1.84.1
  libgif 5.2.1 : libjpeg 8d (libjpeg-turbo 3.0.0) : libpng 1.6.43 : libtiff 4.6.0 : zlib 1.2.12 : libwebp 1.3.2 : libopenjp2 2.5.2
 Found NEON
 Found libarchive 3.7.2 zlib/1.2.11 liblzma/5.4.4 bz2lib/1.0.8 liblz4/1.9.4 libzstd/1.5.5
 Found libcurl/8.4.0 SecureTransport (LibreSSL/3.3.6) zlib/1.2.12 nghttp2/1.58.0

Bonus

I tried

export LIBRARY_PATH="/opt/homebrew/lib" 
export CPATH="/opt/homebrew/include" 

this makes my project compile but the process is killed right after running it

filiprejmus commented 8 months ago

BTW using github.com/otiai10/gosseract/v2 v2.4.1

filiprejmus commented 8 months ago

Found a fix and dropping it for visibility. My go env used clang for the CC and CXX vars. I changed it to gcc and g++ respectively. The extra env vars

export LIBRARY_PATH="/opt/homebrew/lib" 
export CPATH="/opt/homebrew/include" 

solve the problem. I don't know if there's a change to do to catch this without including a manual steps for users like me with homebrew installed tesseract and leptonica.

otiai10 commented 2 months ago

Thank you, @filiprejmus !!

As far as I understand, the very minimum steps to get it work on macos is described here https://github.com/otiai10/gosseract/blob/main/.github/workflows/go-ci.yml#L13-L32