我是cmake的新手,正在编写使用tesseract的应用程序。 g++命令行工作正常

g++ -O3 -std=c++11 `pkg-config --cflags --libs tesseract opencv` my_first.cpp -o my_first

但是我写了以下CMakeFile.txt并在Clion中构建,它引发了许多链接错误
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)

以下是抛出的错误
/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

最佳答案

这就是您需要做的。如果您遇到任何问题,请告诉我。

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)

10-08 05:21