0

I've installed OpenCV 4.0.0 with this command

brew install hybridgroup/tools/opencv

And I'm trying to compile simple program which loads image from filesystem into cv::Mat.

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

int main() {
    auto img = cv::imread("/Users/peter/Dektop/source.png");
    return 0;
}

And I'm keep getting this error

Undefined symbols for architecture x86_64:
"cv::imread(std::__cxx11::basic_string, std::allocator > const&, int)", referenced from: _main in ccw6DQpj.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

I'm compiling with this command. GCC 8.2.0.

g++-8 main.cpp -std=c++17 `pkg-config --cflags --libs opencv4`
Peter
  • 435
  • 1
  • 3
  • 11
  • 2
    Gcc changed its binary representation of `std::string` since copy-on-write was no longer allowed, are you sure the library you have uses the same representation? – JVApen Dec 18 '18 at 12:07
  • Check also my last answer there is the library exists. – Matthieu Brucher Dec 18 '18 at 12:07
  • Note: Looks like this changed from gcc-5 on, do you know how the opencv4 library got build? (Gcc4, Gcc5 or Clang with libcxx?) – JVApen Dec 18 '18 at 12:20
  • @JVApen I believe it was clang with libcxx. I've found this information in INSTALL_RECEIPT.json. – Peter Dec 18 '18 at 12:27
  • Are you sure the file name is `opencv4` and not `opencv4.pc` for `pkg-config --cflags --libs opencv4.pc`? – roschach Dec 18 '18 at 17:01
  • @FrancescoBoi 100% sure. – Peter Dec 18 '18 at 17:06
  • I tried :) If you decide to compile it by yourself let me know. I recently have done it – roschach Dec 18 '18 at 17:08
  • @FrancescoBoi I've tried to compile OpenCV. I've deleted `c++` symlink which was pointing on built-in Apple clang and created new one pointing to GCC 8.2.0. Compilation failed with a lot of errors in `.mm` files. – Peter Dec 18 '18 at 17:34
  • Maybe I am wrong or did no understand but if you have installed it with brew you did not compiled it...anyway instead of symlinking which is a mess on osx you can also keep it in a local folder and referencing it during conpilation for example with a Makefile. I will try to post an answer with what I used for symlinking and also for having a local compiled folder if that's useful for u. – roschach Dec 18 '18 at 21:00

1 Answers1

1

Given the answers in the comments, you have a binary incompatible version of the STL.

OpenCV4 is build with libc++, which defines the symbol of string as std::v1::basic_string, while libstdc++ (default of GCC), uses std::__cxx11::basic_string

At compile time, you don't know the difference between these 2 usages, as you are using the same interface. However, the implementation is different. As you are calling, a function that requires such string, you'll need to compile with the same version of the STL.

I'm sure that for Clang, you can pass -stdlib=libc++ to indicate this version of the STL, I haven't tried it with GCC.

JVApen
  • 11,008
  • 5
  • 31
  • 67