0

I'm writing a new question because all the solutions I found don't seem to work...

I compiled OpenCV 4.0.1 with the following options:

cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.1/modules \
-DOPENCV_TEST_DATA_PATH=../../opencv_extra-4.0.1/testdata \
-DWITH_OPENGL=ON \
-DWITH_OPENCL=ON \
-DBUILD_TIFF=ON \
-DWITH_IPP=ON \
-DWITH_TBB=ON \
-DWITH_EIGEN=ON \
-DWITH_V4L=ON \
-DBUILD_opencv_java=OFF \
-DWITH_CUDA=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
..

Now I'm trying to compile a simple script to test if everything worked:

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

int main()
{
    std::string path = "picture.jpg";
    cv::Mat img = cv::imread(path.c_str(), -1);
    return 0;
}

with the command

g++ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lm -L /usr/local/lib/ -I /usr/local/include/opencv4/ test.cpp -o test

And I get this error:

/tmp/ccrKkLP8.o: In function `main':
test.cpp:(.text+0xa7): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/tmp/ccrKkLP8.o: In function `cv::Mat::~Mat()':
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccrKkLP8.o: In function `cv::Mat::release()':
test.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status

Any ideas?

--- SOLUTION ---

I have to write it here, since the question is closed... The problem was in the order of arguments to g++. It worked after having put the source file after g++. The correct command is:

g++ test.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lm -L /usr/local/lib/ -I /usr/local/include/opencv4/ -o test

Marco
  • 1,454
  • 1
  • 16
  • 30
  • You code example `cv::imread(path.c_str(), -1);` doesn't match the signature failing shown at the linker error `cv::imread(std::__cxx11::basic_string, std::allocator > const&, int)'`. Could it be a recompile problem after you made a change from e.g. `cv::imread(path, -1);` to the code you show? – πάντα ῥεῖ Mar 21 '19 at 17:29
  • Well... `path` is a string and also the signature is cv::imread(string, int), there are just a lot of angular parentheses but all in all the first element is a string: `std::__cxx11::basic_string, std::allocator > const&` – Marco Mar 22 '19 at 08:42
  • Why are you passing`path.c_str()` then?? – πάντα ῥεῖ Mar 22 '19 at 11:48
  • I've actually tried passing in both `path` and `path.c_str()`... – Marco Mar 23 '19 at 11:13
  • I SOLVED IT!!! The problem is in the order of arguments in the command! After having put `test.cpp` just after `g++` the code compiled fine. I also had to run `sudo ldconfig` because the exe was not again finding the libraries. The final (working) command is: `g++ test.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lm -L /usr/local/lib/ -I /usr/local/include/opencv4/ -o test` – Marco Mar 23 '19 at 22:16

0 Answers0