cMakefile for using tesseract and opencv - c++

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

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)

Related

Linking GLFW on M1 Mac with Cmake

After a list of depreciation warnings for OpenGL, I get the following:
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Project_1.dir/main.cpp.o -o Project_1 /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/OpenGL.framework/OpenGL.tbd && :
Undefined symbols for architecture arm64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
My current CMake file:
cmake_minimum_required(VERSION 3.24)
project(Project_1)
set(CMAKE_CXX_STANDARD 17)
add_executable(Project_1 main.cpp)
find_package(OpenGL REQUIRED)
link_directories("/opt/homebrew/lib")
include_directories("/opt/homebrew/include")
target_link_libraries(Project_1 OpenGL::GL)
I've tried to explicitly tell the compiler to build for Apple Silicon with set(CMAKE_OSX_ARCHITECTURES "x86_64") to no avail.

Add curlpp to cmake project

I am trying to do a project in C++ with the help of curlcpp and cmake, but I am unable to compile the project.
I am new to CMakeLists and it is quite difficult to understand how to add the stuff you require to make your project work. I basically usually copy what I can find in other posts without really understanding how they work; tutorial and documentation is usually either too basic or to complex. To make things worse, I have the impression that each library is added differently so when there is a new one to add the only way I have to add it is to search for previous post asking how to do it. In this case I was unable to find anything that helped me.
So here is what I have done:
After installing curlpp and curl with homebrew
brew install curl
brew install curlpp
I write my project:
Cmakelists.txt:
cmake_minimum_required(VERSION 3.0)
project(stock_analysis)
set(CMAKE_CXX_FLAGS "-g -Wall -std=c++11")
######## BOOST STUFF ########
#DELETE NEXT LINE
set(Boost_NO_BOOST_CMAKE ON)
# set(Boost_USE_STATIC_LIBS ON) # only find static libs
# set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS
filesystem
)
######## CURL STUFF ########
include(FindCURL)
find_package(CURL REQUIRED)
if(CURL_FOUND)
message(STATUS "Found CURL version: ${CURL_VERSION_STRING}")
message(STATUS "Using CURL include dir(s): ${CURL_INCLUDE_DIRS}")
message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}")
else()
message(FATAL_ERROR "Could not find CURL")
endif()
SET(HEADERS
include
)
SET(SOURCE_FILES
src/main.cpp
src/analyse.cpp
src/helpers.cpp
src/simulation.cpp
src/data.cpp
)
include_directories(${HEADERS}
${Boost_INCLUDE_DIRS}
${CURL_INCLUDE_DIRS}
)
add_executable(app ${SOURCE_FILES})
target_link_libraries(app
${Boost_LIBRARIES}
${CURL_LIBRARIES}
)
Function in data.cpp (I basically copy the example in curlcpp webpage):
#include "data.h"
#include <iostream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
using namespace curlpp::options;
void fetchData(){
try
{
// That's all that is needed to do cleanup of used resources (RAII style).
curlpp::Cleanup myCleanup;
// Our request to be sent.
curlpp::Easy myRequest;
// Set the URL.
myRequest.setOpt<Url>("http://example.com");
// Send request and get a result.
// By default the result goes to standard output.
myRequest.perform();
}
catch( curlpp::RuntimeError &e )
{
std::cout << e.what() << std::endl;
}
catch( curlpp::LogicError &e )
{
std::cout << e.what() << std::endl;
}
}
The error I get:
Undefined symbols for architecture x86_64:
"curlpp::OptionBase::OptionBase(CURLoption)", referenced from:
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in data.cpp.o
"curlpp::OptionBase::~OptionBase()", referenced from:
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in data.cpp.o
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~Option() in data.cpp.o
"curlpp::UnsetOption::UnsetOption(char const*)", referenced from:
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in data.cpp.o
curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002>::updateHandleToMe(curlpp::internal::CurlHandle*) const in data.cpp.o
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::getValue() const in data.cpp.o
"curlpp::RuntimeError::~RuntimeError()", referenced from:
curlpp::UnsetOption::~UnsetOption() in data.cpp.o
"curlpp::libcurlRuntimeAssert(char const*, CURLcode)", referenced from:
void curlpp::internal::CurlHandle::option<void*>(CURLoption, void*) in data.cpp.o
"curlpp::Easy::perform()", referenced from:
StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
"curlpp::Easy::Easy()", referenced from:
StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
"curlpp::Easy::~Easy()", referenced from:
StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
"curlpp::Cleanup::Cleanup()", referenced from:
StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
"curlpp::Cleanup::~Cleanup()", referenced from:
StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
"curlpp::OptionBase::operator<(curlpp::OptionBase const&) const", referenced from:
vtable for curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002> in data.cpp.o
vtable for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in data.cpp.o
"typeinfo for curlpp::LogicError", referenced from:
GCC_except_table0 in data.cpp.o
"typeinfo for curlpp::OptionBase", referenced from:
curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in data.cpp.o
typeinfo for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in data.cpp.o
"typeinfo for curlpp::RuntimeError", referenced from:
GCC_except_table0 in data.cpp.o
typeinfo for curlpp::UnsetOption in data.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]: *** [app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2
I had previously followed the next thread:
Linking curl in a project using CMake
Adding curlcpp to target_link_libraries() in the cmakelists just makes another error appear:
ld: library not found for -lcurlpp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2
By the way, I do not understand why curlcpp appears from no where and without the traditional cmakelists notation ${Some_variable}
use curlpp like below(minimum code):
cmake_minimum_required(VERSION 3.14)
project(testCurlPP LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FindPkgConfig)
pkg_check_modules(CURLPP REQUIRED curlpp)
add_executable(testCurlPP
main.cpp
)
target_link_libraries(testCurlPP
${CURLPP_LDFLAGS}
)
Note: dont forget to building and installing curlpp before using that in projects.
building(take last release) curlpp:
mkdir build
cd build
cmake ..
make -j8
sudo make install
add its location to environment variables:
LD_LIBRARY_PATH=/usr/local/lib

How to link OpenGL related libraries on mac using cmake?

I am trying to complete a simple GLFW tutorial on mac using cmake, where I encounter a series of undefined symbol linking errors. I did my research regarding this issue and found no help. Below is my CMakeLists.txt.
cmake_minimum_required(VERSION 3.3)
project(GL_Template)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
find_package(GLM REQUIRED)
find_package(GLEW REQUIRED STATIC)
include_directories(${GLM_INCLUDE_DIR})
include_directories(/usr/local/include)
include_directories(./include)
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(IOKIT_LIBRARY IOKit REQUIRED)
find_library(COREVID_LIBRARY CoreVideo REQUIRED)
message(${COCOA_LIBRARY})
message(${IOKIT_LIBRARY})
message(${COREVID_LIBRARY})
file(GLOB A_SOURCE ./src/*.cpp)
add_executable(Hello example/main.cpp ${A_SOURCE})
target_link_libraries(Hello ${GLEW_LIBRARY} ${GLFW3_LIBRARIES})
target_link_libraries(Hello ${COCOA_LIBRARY} ${COREVID_LIBRARY} ${IOKIT_LIBRARY})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")
Any help would be helpful.
Below is the error message
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwGetTime", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSetCursorPosCallback", referenced from:
_main in main.cpp.o
"_glfwSetInputMode", referenced from:
_main in main.cpp.o
"_glfwSetKeyCallback", referenced from:
_main in main.cpp.o
"_glfwSetScrollCallback", referenced from:
_main in main.cpp.o
"_glfwSetWindowShouldClose", referenced from:
key_callback(GLFWwindow*, int, int, int, int) in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowHint", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", referenced from:
_main in main.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]: *** [Hello] Error 1
make[1]: *** [CMakeFiles/Hello.dir/all] Error 2
make: *** [all] Error 2
I found the solution. Obviously the GLFW3_LIBRARY is not defined and the defined variable should be GLFW_LIBRARY.

CMake finds boost libraries but Make fails to link them

After coming back to a project after a month I am able to run CMake successfully with the following output
-- Boost version: 1.61.0
-- Found the following Boost libraries:
-- system
-- thread
-- filesystem
-- chrono
-- date_time
-- atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/LittleNewt/gitness/MangaMeCLI/build
but for some reason when running Make on the generated MakeFile I get the following output
[ 50%] Building CXX object CMakeFiles/mangaMeCLI.dir/src/mangaMeCLI.cpp.o
[100%] Linking CXX executable mangaMeCLI
Undefined symbols for architecture x86_64:
"boost::filesystem::path::operator/=(char const*)", referenced from:
_main in mangaMeCLI.cpp.o
"boost::filesystem::path::operator/=(boost::filesystem::path const&)", referenced from:
boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) in mangaMeCLI.cpp.o
boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in mangaMeCLI.cpp.o
"boost::filesystem::detail::current_path(boost::system::error_code*)", referenced from:
boost::filesystem::current_path() in mangaMeCLI.cpp.o
"boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::create_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in mangaMeCLI.cpp.o
boost::filesystem::is_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init.75 in mangaMeCLI.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init.73 in mangaMeCLI.cpp.o
___cxx_global_var_init.74 in mangaMeCLI.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]: *** [mangaMeCLI] Error 1
make[1]: *** [CMakeFiles/mangaMeCLI.dir/all] Error 2
make: *** [all] Error 2
I did some research and have found others having the same issues due to not linking the correct version of the libraries for there architecture (64bit vs 32bit) but am not sure how to identify if this is the issue for me.
Here is my unchanged CMakeLists.txt file
CMAKE_MINIMUM_REQUIRED(VERSION 3.4.1)
PROJECT(MangaMeCLI)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
INCLUDE_DIRECTORIES(includes)
ADD_EXECUTABLE(mangaMeCLI src/mangaMeCLI.cpp)
MESSAGE("${CMAKE_CXX_FLAGS}")
SET(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system thread filesystem)
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${BOOST_LIBRARIES})
SET(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/*)
FIND_PACKAGE(OpenSSL REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(mangaMeCLI ${OPENSSL_LIBRARIES})
FIND_PACKAGE(cppnetlib REQUIRED)
INCLUDE_DIRECTORIES(${CPPNETLIB_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${CPPNETLIB_LIBRARIES})
After reading a useful comment I found that the BOOST_LIBRARIES variable was empty even though cmake prints that it found the boost libraries I was looking for. I am assuming this is the reason for my error.
CMake variables are case-sensitive. As per the find module's documentation, you want Boost_LIBRARIES, not BOOST_LIBRARIES:
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${Boost_LIBRARIES})

cMake txt file for linking glfw library

I'm learning about c++ by following a tutorial, I'm using cMake right now and I have my glfw library in my external folder but I can't get it to link correctly. Here is my cMake txt file
cmake_minimum_required(VERSION 2.8.4)
project(dunjun)
set(dunjun_src src/main.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES src/main.cpp)
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/external/glfw-3.1/include
)
link_directories(
${PROJECT_SOURCE_DIR}/external/glfw-3.1/src
)
add_executable(dunjun ${dunjun_src})
target_link_libraries(dunjun ${PROJECT_SOURCE_DIR}/external/glfw-3.1/src)
The errors it gives me are the following.
[100%] Building CXX object CMakeFiles/dunjun.dir/src/main.cpp.o
Linking CXX executable dunjun
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", referenced from:
_main in main.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[3]: *** [dunjun] Error 1
make[2]: *** [CMakeFiles/dunjun.dir/all] Error 2
make[1]: *** [CMakeFiles/dunjun.dir/rule] Error 2
make: *** [dunjun] Error 2