问题
I am a newbie to cmake and I am writing an application for using tesseract. the g++ command line work fine
g++ -O3 -std=c++11 `pkg-config --cflags --libs tesseract opencv` my_first.cpp -o my_first
But I wrote the following CMakeFile.txt and building in Clion and it throws a bunch of linking errors
cmake_minimum_required(VERSION 2.6)
add_compile_options(-std=c++11)
project (my_first)
find_package(PkgConfig REQUIRED)
pkg_check_modules (OPENCV REQUIRED opencv)
link_directories(${OPENCV_LIBRARY_DIRS})
pkg_check_modules (TESSERACT REQUIRED tesseract)
link_directories(${TESSERACT_LIBRARY_DIRS})
add_executable(myfirst my_first.cpp)
The following is the error thrown
/usr/local/Cellar/cmake/3.11.3/bin/cmake --build
/Users/ggovindan/tessaract_ocr/tesseract/experiments/cmake-build-debug --target all -- -j 4
[ 50%] Linking CXX executable myfirst
Undefined symbols for architecture x86_64:
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in my_first.cpp.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in my_first.cpp.o
cv::String::operator=(cv::String const&) in my_first.cpp.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&) in
my_first.cpp.o
"cv::imread(cv::String const&, int)", referenced from:
_main in my_first.cpp.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() in my_first.cpp.o
"tesseract::TessBaseAPI::GetUTF8Text()", referenced from:
_main in my_first.cpp.o
"tesseract::TessBaseAPI::SetPageSegMode(tesseract::PageSegMode)", referenced from:
_main in my_first.cpp.o
"tesseract::TessBaseAPI::Init(char const*, char const*,
tesseract::OcrEngineMode, char**, int, GenericVector<STRING> const*,
GenericVector<STRING> const*, bool)", referenced from:
tesseract::TessBaseAPI::Init(char const*, char const*,
tesseract::OcrEngineMode) in my_first.cpp.o
"tesseract::TessBaseAPI::SetImage(unsigned char const*, int, int, int, int)", referenced from:
_main in my_first.cpp.o
"tesseract::TessBaseAPI::TessBaseAPI()", referenced from:
_main in my_first.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[2]: *** [myfirst] Error 1
make[1]: *** [CMakeFiles/myfirst.dir/all] Error 2
make: *** [all] Error 2
回答1:
This is what you need to do.Let me know if you face any problems.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package( PkgConfig REQUIRED)
pkg_search_module( TESSERACT REQUIRED tesseract )
pkg_search_module( LEPTONICA REQUIRED lept )
include_directories( ${TESSERACT_INCLUDE_DIRS} )
include_directories( ${LEPTONICA_INCLUDE_DIRS} )
link_directories( ${TESSERACT_LIBRARY_DIRS} )
link_directories( ${LEPTONICA_LIBRARY_DIRS} )
find_package( OpenCV REQUIRED )
include_directories("myfirst.h")
file(GLOB SOURCES "myfirst.cpp")
add_executable(myfirst ${SOURCES})
target_link_libraries( myfirst ${OpenCV_LIBS} )
target_link_libraries( myfirst ${TESSERACT_LIBRARIES} )
target_link_libraries( myfirst ${LEPTONICA_LIBRARIES} )
#install
install(TARGETS myfirst DESTINATION /usr/local/bin)
来源:https://stackoverflow.com/questions/50994135/cmakefile-for-using-tesseract-and-opencv