-1

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 the CMakeLists.txt and try to use both the following error appears during the make operation:

New CMakeLists.txt:

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})

Error in make:

/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)

Edit:

Adding the instruction

target_link_options(test_opencv PRIVATE -Wl,-t)

suggested in the comments gives the following:

With OpenCV:

/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/13/crtbeginS.o
CMakeFiles/test_opencv.dir/main.cpp.o
/usr/local/lib/libopencv_gapi.so.4.13.0
/usr/local/lib/libopencv_stitching.so.4.13.0
/usr/local/lib/libopencv_alphamat.so.4.13.0
/usr/local/lib/libopencv_aruco.so.4.13.0
/usr/local/lib/libopencv_bgsegm.so.4.13.0
/usr/local/lib/libopencv_bioinspired.so.4.13.0
/usr/local/lib/libopencv_ccalib.so.4.13.0
/usr/local/lib/libopencv_cudabgsegm.so.4.13.0
/usr/local/lib/libopencv_cudafeatures2d.so.4.13.0
/usr/local/lib/libopencv_cudaobjdetect.so.4.13.0
/usr/local/lib/libopencv_cudastereo.so.4.13.0
/usr/local/lib/libopencv_cvv.so.4.13.0
/usr/local/lib/libopencv_dnn_objdetect.so.4.13.0
/usr/local/lib/libopencv_dnn_superres.so.4.13.0
/usr/local/lib/libopencv_dpm.so.4.13.0
/usr/local/lib/libopencv_face.so.4.13.0
/usr/local/lib/libopencv_freetype.so.4.13.0
/usr/local/lib/libopencv_fuzzy.so.4.13.0
/usr/local/lib/libopencv_hdf.so.4.13.0
/usr/local/lib/libopencv_hfs.so.4.13.0
/usr/local/lib/libopencv_img_hash.so.4.13.0
/usr/local/lib/libopencv_intensity_transform.so.4.13.0
/usr/local/lib/libopencv_line_descriptor.so.4.13.0
/usr/local/lib/libopencv_mcc.so.4.13.0
/usr/local/lib/libopencv_quality.so.4.13.0
/usr/local/lib/libopencv_rapid.so.4.13.0
/usr/local/lib/libopencv_reg.so.4.13.0
/usr/local/lib/libopencv_rgbd.so.4.13.0
/usr/local/lib/libopencv_saliency.so.4.13.0
/usr/local/lib/libopencv_signal.so.4.13.0
/usr/local/lib/libopencv_stereo.so.4.13.0
/usr/local/lib/libopencv_structured_light.so.4.13.0
/usr/local/lib/libopencv_superres.so.4.13.0
/usr/local/lib/libopencv_surface_matching.so.4.13.0
/usr/local/lib/libopencv_tracking.so.4.13.0
/usr/local/lib/libopencv_videostab.so.4.13.0
/usr/local/lib/libopencv_wechat_qrcode.so.4.13.0
/usr/local/lib/libopencv_xfeatures2d.so.4.13.0
/usr/local/lib/libopencv_xobjdetect.so.4.13.0
/usr/local/lib/libopencv_xphoto.so.4.13.0
/usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.83.0
/usr/local/lib/libopencv_shape.so.4.13.0
/usr/local/cuda-12.9/targets/x86_64-linux/lib/libcudart.so
/usr/local/lib/libopencv_highgui.so.4.13.0
/usr/local/lib/libopencv_datasets.so.4.13.0
/usr/local/lib/libopencv_plot.so.4.13.0
/usr/local/lib/libopencv_text.so.4.13.0
/usr/local/lib/libopencv_ml.so.4.13.0
/usr/local/lib/libopencv_phase_unwrapping.so.4.13.0
/usr/local/lib/libopencv_cudacodec.so.4.13.0
/usr/local/lib/libopencv_videoio.so.4.13.0
/usr/local/lib/libopencv_cudaoptflow.so.4.13.0
/usr/local/lib/libopencv_cudalegacy.so.4.13.0
/usr/local/lib/libopencv_cudawarping.so.4.13.0
/usr/local/lib/libopencv_optflow.so.4.13.0
/usr/local/lib/libopencv_ximgproc.so.4.13.0
/usr/local/lib/libopencv_video.so.4.13.0
/usr/local/lib/libopencv_imgcodecs.so.4.13.0
/usr/local/lib/libopencv_objdetect.so.4.13.0
/usr/local/lib/libopencv_calib3d.so.4.13.0
/usr/local/lib/libopencv_dnn.so.4.13.0
/usr/local/lib/libopencv_features2d.so.4.13.0
/usr/local/lib/libopencv_flann.so.4.13.0
/usr/local/lib/libopencv_photo.so.4.13.0
/usr/local/lib/libopencv_cudaimgproc.so.4.13.0
/usr/local/lib/libopencv_cudafilters.so.4.13.0
/usr/local/lib/libopencv_imgproc.so.4.13.0
/usr/local/lib/libopencv_cudaarithm.so.4.13.0
/usr/local/lib/libopencv_core.so.4.13.0
/usr/local/lib/libopencv_cudev.so.4.13.0
/usr/lib/gcc/x86_64-linux-gnu/13/libstdc++.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libm.so
/lib/x86_64-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libmvec.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libc.so
/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/crtn.o

Without OpenCV:

/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/13/crtbeginS.o
CMakeFiles/test_opencv.dir/main.cpp.o
/usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.83.0
/usr/lib/gcc/x86_64-linux-gnu/13/libstdc++.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libm.so
/lib/x86_64-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libmvec.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libc.so
/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
/usr/lib/x86_64-linux-gnu/libc_nonshared.a
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/13/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/crtn.o

The only difference between both of them is the presence of cudart.so in the CMake file with OpenCV. There is no difference between the libs pulled.

12
  • 2
    However, when I uncomment the OpenCV inclusion Please post a minimal reproducible example, so users don't have to modify the code in the question to reproduce the issue. They only need copy & paste the code, build and get the mentioned errors. Commented yesterday
  • 1
    Add target_link_options(test_opencv PRIVATE -Wl,-t) to your cmakelist and try to build with and without opencv. It will print list of files used for the link. See if it links the same boost libraries in both cases or not. Commented yesterday
  • Boost is an optional dependency of OpenCV, with this defaulting to not using it. according to some research on github, it'd only be used in the flann module. -- I find it likely that OpenCV is not to blame, and that you did something that does not hinge on OpenCV. Commented yesterday
  • 1
    probably the same issue you have here stackoverflow.com/questions/79867214/linker-cant-find-cvimshow in that you're doing something with cmake and the linker that you shouldn't. Commented yesterday
  • 1
    What was the problem with answer I've provided under your question which has been deleted? I've verified this code on my machine and it works as expected. Commented 18 hours ago

1 Answer 1

0

In your old deleted question I've provided an answer.

Basically this was proposed and verified version of CMakeLists.txt:

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.4.0 COMPONENTS program_options REQUIRED)
message(STATUS " Boost Version: ${Boost_VERSION}")

add_executable(test_opencv main.cpp)

target_link_libraries(test_opencv
    PUBLIC
        opencv_core opencv_highgui
        Boost::program_options
)

I've test this on my machine (MacOS, Apple clang 17.0.0, dependencies installed by brew: Boost 1.74, OpenCV 4.13.0).

Sign up to request clarification or add additional context in comments.

1 Comment

For me it is still giving the same error, that's why I did not accepted as answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.