我一直在看柯南2的文档,问题和线程,但我不能设法让Tesseract与柯南合作。让我给你看看我的设置:
App
└── main.cpp
CMakeLists.txt
conanfile.txt
main.cpp
是我能想到的最基本的函数:
#include <tesseract/baseapi.h>
int main(void) {
tesseract::TessBaseAPI api;
return 0;
}
我的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.11)
# --------------------------------- Setup project config
set(PROJECT_NAME foo)
project(${PROJECT_NAME} CXX)
set(CMAKE_CXX_STANDARD 20)
# --------------------------------- Set project flags and optimization
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g3 -O0 -W -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -g0 -O3")
# --------------------------------- Fetch sources
set(SOURCES App/main.cpp)
# --------------------------------- Fetch dependencies
find_package(Tesseract REQUIRED)
message(STATUS "Tesseract_INCLUDE_DIRS: ${Tesseract_INCLUDE_DIRS}")
message(STATUS "Tesseract_LIBRARIES: ${Tesseract_LIBRARIES}")
message(STATUS "Tesseract_VERSION: ${Tesseract_VERSION}")
# --------------------------------- Build binary
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/App
${Tesseract_INCLUDE_DIRS}
)
最后是我的conanfile.txt
(Leptonica
是Tesseract
的直接依赖项):
[requires]
tesseract/5.3.0
leptonica/1.82.0
[generators]
CMakeDeps
CMakeToolchain
最后,我只运行以下命令(我通常使用cmake然后make,但这些是来自文档的确切命令):
conan install . --output-folder=build/ --build=missing
# [...]
# Install finished successfully
cmake -B build/ -S . -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug
# [...]
# -- Tesseract_INCLUDE_DIRS: /Users/foo/.conan2/p/b/tesse4be5a6b24f5df/p/include
# -- Tesseract_LIBRARIES: Tesseract::libtesseract
# -- Tesseract_VERSION: 5.3.0
# [...]
# -- Configuring done (0.9s)
# -- Generating done (0.0s)
# -- Build files have been written to: /Users/foo/Desktop/test_tesseract/build
cmake --build build/ -j 4 -v
# [...]
# Change Dir: '/Users/foo/Desktop/test_tesseract/build'
# Run Build Command(s): /opt/homebrew/Cellar/cmake/3.27.0/bin/cmake -E env VERBOSE=1 /usr/bin/make -f Makefile -j4
# /opt/homebrew/Cellar/cmake/3.27.0/bin/cmake -S/Users/foo/Desktop/test_tesseract -B/Users/foo/Desktop/test_tesseract/build --check-build-system CMakeFiles/Makefile.cmake 0
# /opt/homebrew/Cellar/cmake/3.27.0/bin/cmake -E cmake_progress_start /Users/foo/Desktop/test_tesseract/build/CMakeFiles /Users/foo/Desktop/test_tesseract/build//CMakeFiles/progress.marks
# /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
# /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/toto.dir/build.make CMakeFiles/toto.dir/depend
# cd /Users/foo/Desktop/test_tesseract/build && /opt/homebrew/Cellar/cmake/3.27.0/bin/cmake -E cmake_depends "Unix Makefiles" /Users/foo/Desktop/test_tesseract /Users/foo/Desktop/test_tesseract /Users/foo/Desktop/test_tesseract/build /Users/foo/Desktop/test_tesseract/build /Users/foo/Desktop/test_tesseract/build/CMakeFiles/toto.dir/DependInfo.cmake "--color="
# /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/toto.dir/build.make CMakeFiles/toto.dir/build
# [ 50%] Building CXX object CMakeFiles/toto.dir/App/main.cpp.o
# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/foo/Desktop/test_tesseract/App -I/Users/foo/.conan2/p/b/tesse4be5a6b24f5df/p/include -stdlib=libc++ -stdlib=libc++ -g3 -O0 -W -Wall -Wextra -std=gnu++20 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -MD -MT CMakeFiles/toto.dir/App/main.cpp.o -MF CMakeFiles/toto.dir/App/main.cpp.o.d -o CMakeFiles/toto.dir/App/main.cpp.o -c /Users/foo/Desktop/test_tesseract/App/main.cpp
# [100%] Linking CXX executable toto
# /opt/homebrew/Cellar/cmake/3.27.0/bin/cmake -E cmake_link_script CMakeFiles/toto.dir/link.txt --verbose=1
# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -stdlib=libc++ -stdlib=libc++ -g3 -O0 -W -Wall -Wextra -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/toto.dir/App/main.cpp.o -o toto
# ld: Undefined symbols:
# tesseract::TessBaseAPI::TessBaseAPI(), referenced from:
# _main in main.cpp.o
# tesseract::TessBaseAPI::~TessBaseAPI(), referenced from:
# _main in main.cpp.o
# clang: error: linker command failed with exit code 1 (use -v to see invocation)
# make[2]: *** [toto] Error 1
# make[1]: *** [CMakeFiles/toto.dir/all] Error 2
# make: *** [all] Error 2
起初我有一个错误,在main.cpp
中找不到#include <tesseract/baseapi.h>
,但这只是我的错,我忘了通过CMakeLists.txt
链接库。
当我修复这个问题时,我无法在构建命令中获得-L
,-I
甚至-l
(在-v
详细模式下查看),我只是得到了未定义的符号错误。
为了尝试强制至少一个-l
,我尝试在CMakeLists.txt
中设置target_link_libraries()
,如下所示:
target_link_libraries(${PROJECT_NAME}
PRIVATE ${Tesseract_LIBRARIES}
)
这会产生相同的结果,最后我手动强制它写入-ltesseract
,如下所示:
target_link_libraries(${PROJECT_NAME}
PRIVATE ${Tesseract_LIBRARIES}
tesseract
)
这实际上是可行的,并在最后附加了-l
,但我得到了一个更糟糕的错误:
ld: library 'tesseract' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
作为参考,我在arm64架构(macOS)上,但我不明白为什么这会是问题。
2条答案
按热度按时间polkgigr1#
你必须链接tesseract图书馆显然。暴露tesseract的include目录会让预处理器高兴,但不会让链接器高兴。
更喜欢使用现代的CMake,像一个好公民一样依赖CMake导入的目标,而不是这些丑陋的
Tesseract_*
CMake变量。很容易错过接口定义,接口特性等内容,而CMake目标包含所有这些信息:igetnqfo2#
Conan考虑了构建类型(Release/Release/...)
默认情况下,
conan profile detect
命令设置build_type=Release
的配置,我用-DCMAKE_BUILD_TYPE=Debug
标志构建cmake。Conan在专用于Release的路径中设置了所有内容,cmake在专用于Release的路径中查找。
切换到
-DCMAKE_BUILD_TYPE=Release
修复了它。