Problems compiling code with cmake and Boost / OpenCV

4 days ago 9
ARTICLE AD BOX

I'm having some problems compiling a simple piece of code. I'm just starting to use cmake (before I only used handcrafted Makefile) and compiling this piece of code in my PC works normally.

Code:

#include <iostream> #include <string> #include <boost/program_options.hpp> //#include <opencv4/opencv2/core.hpp> //#include <opencv4/opencv2/imgproc.hpp> //#include "opencv4/opencv2/highgui.hpp" namespace po = boost::program_options; int main(int ac, char** av){ std::string mode; std::string addr; po::options_description desc("Allowed options"); desc.add_options() ("help, H", "Produce help message") ("exec-mode, E", po::value<std::string>(&mode)->default_value("exec"), "Choose the option to execute") ("rootaddr, A", po::value<std::string>(&addr)->default_value("/home/postdoc/ecl_workspace/"), "Root address to save the logs and results") ; po::variables_map vm; po::store(po::parse_command_line(ac, av, desc), vm); po::notify(vm); if(vm.count("help")) { std::cout << desc << std::endl; return 1; } if(vm.count("rootaddr")) std::cout << "Address given: " << addr << std::endl; //std::cout << "Test program for cmake imp" << std::endl; //std::cout << "OpenCV version : " << CV_VERSION << std::endl; //std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl; //std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl; //std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl; //cv::Mat matrix; //matrix = cv::imread("./pc.jpeg", cv::IMREAD_COLOR_BGR); std::string msg = "Message: testin"; //cv::putText(matrix, msg, cv::Point(100, 25), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0)); std::string winName = "Test window"; //cv::namedWindow(winName, cv::WINDOW_AUTOSIZE); //cv::imshow(winName, matrix); //int k = cv::waitKey(0); //cv::destroyWindow(winName); return 0; }

Using this CMake file:

cmake_minimum_required(VERSION 3.10.0) project(test_opencv VERSION 0.1.0 LANGUAGES C CXX) #find_package(OpenCV REQUIRED) #message(STATUS " OpenCV Version: ${OpenCV_VERSION}") find_package(Boost 1.83.0 COMPONENTS program_options REQUIRED) message(STATUS " Boost Version: ${Boost_VERSION}") add_executable(test_opencv main.cpp) #target_link_libraries(test_opencv PUBLIC ${OpenCV_LIBS}) target_link_libraries(test_opencv PUBLIC ${Boost_LIBRARIES})

However, when I uncomment the OpenCV inclusion in he CMakeLists.txt and try to use both the following error appears during the make operation:

/usr/bin/ld: CMakeFiles/test_opencv.dir/main.cpp.o: in function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)': main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0xad): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)' /usr/bin/ld: CMakeFiles/test_opencv.dir/main.cpp.o:(.data.rel.ro._ZTVN5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEE[_ZTVN5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEE]+0x38): undefined reference to `boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool) const' /usr/bin/ld: CMakeFiles/test_opencv.dir/main.cpp.o: in function `boost::program_options::typed_value<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char>::xparse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) const': main.cpp:(.text._ZNK5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcE6xparseERNS_3anyERKNSt7__debug6vectorIS7_SaIS7_EEE[_ZNK5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcE6xparseERNS_3anyERKNSt7__debug6vectorIS7_SaIS7_EEE]+0x82): undefined reference to `boost::program_options::validate(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/test_opencv.dir/build.make:167: test_opencv] Error 1 make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test_opencv.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

How the OpenCV is interfering into the Boost libs? For me this makes no sense, and since I am a relatively new user of CMake, I'm betting that I made a mistake in the configuration. My OpenCV is 4.13 manually compiled and the Boost version is the 1.83 (installed via apt)

Read Entire Article