Quantcast
Viewing all articles
Browse latest Browse all 22144

g++-9.1 fails to link opencv [duplicate]

I am trying to compile C++ code that uses OpenCV and Spinnaker (Camera) libraries on a Raspberry Pi 3.

The Spinnaker library requires a new version of gcc (5+ if I remember it right), while Raspbian Jessie uses gcc version 4.9:

pi@dataisbeautiful:~/SpinnakerCamera/src $ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.9/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 4.9.2-10+deb8u2' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.9.2 (Raspbian 4.9.2-10+deb8u2)

Therefore I have installed gcc version 9.1 by following the guide in: https://solarianprogrammer.com/2017/12/08/raspberry-pi-raspbian-install-gcc-compile-cpp-17-programs/

pi@dataisbeautiful:~/SpinnakerCamera/src $ g++-9.1 -v
Using built-in specs.
COLLECT_GCC=g++-9.1
COLLECT_LTO_WRAPPER=/opt/gcc-9.1.0/libexec/gcc/arm-linux-gnueabihf/9.1.0/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../gcc-9.1.0/configure --prefix=/opt/gcc-9.1.0 --build=x86_64-pc-linux-gnu --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --enable-languages=c,c++,fortran --with-arch=armv6 --with-fpu=vfp --with-float=hard --disable-multilib --program-suffix=-9.1
Thread model: posix
gcc version 9.1.0 (GCC)

However, when I try to compile an OpenCV test file, I get undefined references:

-- OpenCV library status:
--     config: /usr/local/lib/cmake/opencv4
--     version: 4.1.2
--     libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;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_sfm;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
--     include path: /usr/local/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/SpinnakerCamera/src
[ 50%] Linking CXX executable opencv_spinnaker
CMakeFiles/opencv_spinnaker.dir/test.cpp.o: In function `main':
test.cpp:(.text+0x118): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test.cpp:(.text+0x204): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
CMakeFiles/opencv_spinnaker.dir/test.cpp.o: In function `drawText(cv::Mat&)':
test.cpp:(.text+0x3c0): undefined reference to `cv::putText(cv::_InputOutputArray const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
collect2: error: ld returned 1 exit status
CMakeFiles/opencv_spinnaker.dir/build.make:145: recipe for target 'opencv_spinnaker' failed
make[2]: *** [opencv_spinnaker] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/opencv_spinnaker.dir/all' failed
make[1]: *** [CMakeFiles/opencv_spinnaker.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

The funny thing is, whenever I switch back to the old gcc (4.9.2) by commenting out the set(CMAKE_CXX_COMPILER /opt/gcc-9.1.0/bin/g++-9.1) line in my CMakeLists.txt I am able to successfully compile it. What am I doing wrong and how can I make my compiler find my libraries?

My CMakeLists.txt:

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

#set(BUILD_TARGET example01)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_COMPILER /opt/gcc-9.1.0/bin/g++-9.1)
set(CMAKE_C_COMPILER /opt/gcc-9.1.0/bin/gcc-9.1)

project(opencv_spinnaker)

find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")


include_directories(${SPINNAKER_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
include_Directories(/usr/include/spinnaker/)
add_executable(opencv_spinnaker test.cpp SpinnakerCamera.h spinnaker_driver.h utils.h)

target_link_libraries(opencv_spinnaker LINK_PRIVATE ${SPINNAKER_LIBRARIES} /usr/lib/libSpinnaker.so ${OpenCV_LIBS})

and the test file:

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>

using namespace cv;
using namespace std;

void drawText(Mat & image);

int main()
{
    cout << "Built with OpenCV "<< CV_VERSION << endl;
    Mat image;
    VideoCapture capture;
    capture.open(0);
    if(capture.isOpened())
    {
        cout << "Capture is opened"<< endl;
        for(;;)
        {
            capture >> image;
            if(image.empty())
                break;
            drawText(image);
            imshow("Sample", image);
            if(waitKey(10) >= 0)
                break;
        }
    }
    else
    {
        cout << "No capture"<< endl;
        image = Mat::zeros(480, 640, CV_8UC1);
        drawText(image);
        imshow("Sample", image);
        waitKey(0);
    }
    return 0;
}

void drawText(Mat & image)
{
    putText(image, "Hello OpenCV",
            Point(20, 50),
            FONT_HERSHEY_COMPLEX, 1, // font face and scale
            Scalar(255, 255, 255), // white
            1, LINE_AA); // line thickness and type
}


Viewing all articles
Browse latest Browse all 22144

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>