Issue
I need to switch from an ubuntu 20.04 to a Windows 7 workstation.
I'm writing a program for Raspberry PI that needs pthread, so I need a linux test environment.
I installed an ubuntu 20.04 VM and reinstalled all the libraries used in my program :
- OpenCV 4.2
- Libtorch
- NumCpp
Here is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.15)
project(POC_V4)
set(CMAKE_CXX_STANDARD 14)
# Specifying we are using pthread for UNIX systems.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status :")
message(STATUS " libraries: ${TORCH_LIBRARIES}")
message(STATUS "OpenCV library status :")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(POC_V4 <all_my_sources_and_headers>)
target_link_libraries(POC_V4 ${TORCH_LIBRARIES} ${OpenCV_LIBS})
target_link_libraries(POC_V4 pthread dl util)
On the real PC and on the VM, the cmake output is the same:
-- Pytorch status :
-- libraries: torch;torch_library;/usr/lib/libc10.so
-- OpenCV library status :
-- version: 4.2.0
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_hfs;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_reg;opencv_rgbd;opencv_saliency;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- include path: /usr/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: <path_where_i_build>```
On my real Ubuntu PC, the build works properly, but it fails during linkage on the VM :
/usr/bin/ld: CMakeFiles/POC_V4.dir/src/communication.cpp.o: in function `Communication::showImage(cv::Mat, Box, std::string)':
communication.cpp:(.text+0x208): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
/usr/bin/ld: CMakeFiles/POC_V4.dir/src/communication.cpp.o: in function `Communication::showImage(cv::Mat, std::string)':
communication.cpp:(.text+0x393): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
/usr/bin/ld: CMakeFiles/POC_V4.dir/src/communication.cpp.o: in function `Communication::fps(cv::Mat, std::string)':
communication.cpp:(.text+0x655): undefined reference to `cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
/usr/bin/ld: CMakeFiles/POC_V4.dir/src/communication.cpp.o: in function `Communication::Communication()':
communication.cpp:(.text+0x816): undefined reference to `cv::namedWindow(std::string const&, int)'
/usr/bin/ld: CMakeFiles/POC_V4.dir/src/imageGetter.cpp.o: in function `ImageGetter::ImageGetter()':
imageGetter.cpp:(.text+0x342): undefined reference to `cv::VideoCapture::VideoCapture(std::string const&, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/POC_V4.dir/build.make:285: POC_V4] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/POC_V4.dir/all] Error 2
TLDR :
- Same OS
- Same library versions (OpenCV 4.2)
- Same CMakeLists.txt file
Linkage problem with OpenCV...
Any ideas ?
Solution
I finally managed to make it work, thanks to @squareskittles : I needed to uninstall OpenCV, and rebuild it from source to the good version, with C++14 standard.
If you have linking problems related to OpenCV when building, uninstall OpenCV and build it yourself :
sudo apt remove libopencv-dev && sudo apt autoremove
cd ~/Downloads
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.2.0
cd ../opencv
git checkout 4.2.0
mkdir build
Open the CMakeLists.txt
and add the following line at the beginning of the file :
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
Then build OpenCV :
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib/modules/ ..
make
sudo make install
sudo ldconfig
Install potentially missing libraries for graphical output handling.
sudo apt install libgtk2.0-dev pkg-config libcanberra-gtk-module libcanberra-gtk3-module
Try re-building the project, that should work !
Thx again @squareskittles !
Answered By - totok