Skip to content

tessbridge.cpp:5:10: fatal error: 'leptonica/allheaders.h' file not found #324

@m1101322632

Description

@m1101322632

When I run go build , I Got bellow:
当我运行 go build 时,我遇到了如下报错

Image

My env:
我的环境如下:

  • golang: 1.24.2 darwin/arm64
  • tesseract: tesseract 5.5.0 leptonica-1.85.0
  • operating system:Mac Os 13.5.1
  • PC: MacBook Pro with M1 chip

It took me a lot of time to find a solution,So I recorded it to contribute to everyone。
这个问题花费了我很多时间去寻找解决方案,故我将它记录下来以便给后续遇到同样问题的人节省时间。

Without further ado, let's get to the point. I will explain this problem in order of causes and solutions.
闲话少说,这就步入正题,我将依次按出现原因,解决方式来讲清楚这个问题。

First, let's print the compilation plan first to see where the C/C++ header file library directory is specified during actual compilation.

首先,我们先打印编译计划,看看实际编译时 c/c++ 的头文件库目录指定是哪里

$go build
$ go list -e -json -compiled -x ,
Find the following line
在其中找到下面这一行:
cd /Users/xuxiaozhou/go/pkg/mod/github.com/otiai10/gosseract/[email protected]
TERM='dumb' c++ -I . -fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=$WORK/b258=/tmp/go-build -gno-record-gcc-switches -fno-common -Wno-unused-result -I $WORK/b258/ -O2 -g -std=c++0x -frandom-seed=_smLv7B2OJukRbCGnxUd -o $WORK/b258/_x003.o -c tessbridge.cpp

From "dumb' c++ -I . ", we can see that the compiler searches for header files in the current directory (indicated by the dot .) in addition to the standard library and other default search paths.
从 “dumb' c++ -I . ” 可知,编译器除了标准库和其他默认的搜索路径之外,还要在当前目录(由点 . 表示)下查找头文件。

Next, let’s look for the reasons

其次,我们来寻找原因

But my tesseract header file directory is '/opt/homebrew/include', which is not included, so it prompts that 'leptonica/allheaders.h' cannot be found, so I need to add the #cgo compilation parameter to specify it.
但我的tesseract头文件目录是'/opt/homebrew/include',没有被包含在内,所以提示找不到'leptonica/allheaders.h',故需要添加#cgo 编译参数来进行指定。

Finally, let’s look at the solution

最后,我们来看解决方案

Open the client.go file in the gosseract project and replace the following code:
打开gosseract项目下的client.go文件,将如下代码

// #if __FreeBSD__ >= 10 // #cgo LDFLAGS: -L/usr/local/lib -llept -ltesseract // #else // #cgo CXXFLAGS: -std=c++0x // #cgo LDFLAGS: -llept -ltesseract // #cgo CPPFLAGS: -Wno-unused-result // #endif // #include <stdlib.h> // #include <stdbool.h> // #include "tessbridge.h"

Replace with:
替换成:
/// #if __FreeBSD__ >= 10 // #cgo LDFLAGS: -L/opt/homebrew/lib -lleptonica -ltesseract // #else // #cgo CXXFLAGS: -I/opt/homebrew/include -std=c++11 // #cgo LDFLAGS: -L/opt/homebrew/lib -lleptonica -ltesseract // #cgo CPPFLAGS: -Wno-unused-result // #endif // #include <stdlib.h> // #include <stdbool.h> // #include "tessbridge.h"

Note that ‘#cgo CXXFLAGS: -I/opt/homebrew/include -std=c++11’ tells the compiler to add the I/opt/homebrew/include directory to the header file search directory when compiling the C++ program.
注意,‘#cgo CXXFLAGS: -I/opt/homebrew/include -std=c++11’ 这一句是告知编译c++程序时头文件搜索目录要加上I/opt/homebrew/include目录。

If you follow the AI ​​model prompt to add CGO_CFLAGS='-I/opt/homebrew/include', it will not solve the problem because CGO_CFLAGS controls the search directory for header files that need to be added when compiling the C language.
而如果你按AI大模型提示添加 CGO_CFLAGS='-I/opt/homebrew/include',是解决不了问题的。因为CGO_CFLAGS控制的是编译c语言时需要加上的头文件搜索目录。

And '#cgo LDFLAGS: -L/opt/homebrew/lib -lleptonica -ltesseract' tells the compiler the location of the tesseract dynamic link library file when compiling.
而‘#cgo LDFLAGS: -L/opt/homebrew/lib -lleptonica -ltesseract’ 是告知编译器在编译时tesseract的动态链接库文件的位置。

Finally, we verify

最终,我们来进行验证

$ go clean -cache
$ go build

You will find that the error has disappeared, that is, the compilation is successful.
你将会发现报错消失了,即编译通过

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions