1

I'm on a MacBook Pro, OSX Yosemite 10.10.4. I used homebrew to install gcc (to use as a c++ compiler) and opencv. Now when I run basic opencv example scripts, I'm getting an error I don't understand.

Undefined symbols for architecture x86_64:

"cv::Mat::deallocate()", referenced from:

cv::Mat::release() in c++_opencv_test-e26d2b.o

"cv::Mat::create(int, int const*, int)", referenced from:

cv::Mat::create(int, int, int) in c++_opencv_test-e26d2b.o

"cv::Mat::operator=(cv::Scalar_ const&)", referenced from:

cv::Mat::Mat(int, int, int, cv::Scalar_ const&) in c++_opencv_test-e26d2b.o

"cv::fastFree(void*)", referenced from:

cv::Mat::~Mat() in c++_opencv_test-e26d2b.o

"cv::Formatter::get(char const*)", referenced from:

cv::operator<<(std::__1::basic_ostream >&, cv::Mat const&) in c++_opencv_test-e26d2b.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

(sorry for the poor formatting, advice on that would be welcomed as well)

Here's the code I ran ...

#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat img(2,2, CV_8UC3, Scalar(126,0,255));
    cout << "img = \n " << img << "\n\n";
    return 0;
}

I downloaded the normal OpenCV, not the 32-bit version, so I have no idea why I'm getting this error. I'm not very experienced in these command line issues. What should I try?

EDIT: Ok this command worked. My problem was definitely linking to the library.

g++ -ggdb 'pkg-config --cflags --libs /usr/local/opt/opencv3/lib/pkgconfig/opencv.pc' -stdlib=libstdc++ c++_opencv_test.cpp -o test

Apparently on OSX 10.9 and above, the compiler links by default to libc++ instead of libstdc++.

Anything I can do so I don't have to type out that long command each time I run something?

Jeff
  • 11
  • 1
  • 2
  • [**Why Using Namespace Std Is Terrible**](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – amanuel2 Oct 15 '16 at 22:21
  • what was the command line you used to build these examples? it seems you forgot to specify the libraries your linker should link to. Also, not using GCC, but clang (which isn't a problem per se, but not what you claim you do). – Marcus Müller Oct 15 '16 at 22:31
  • My command was "g++ c++_opencv_test.cpp -o test" – Jeff Oct 16 '16 at 06:21
  • @MarcusMüller you were right, my issue was specifying the library. Thanks! I did use GCC (or rather g++). Maybe GCC uses clang? In any case, this command worked -- `g++ -ggdb 'pkg-config --cflags --libs /usr/local/opt/opencv3/lib/pkgconfig/opencv.pc' -stdlib=libstdc++ c++_opencv_test.cpp -o test` Do you know of anything I can do so I don't have to type that huge mess out each time I use opencv? – Jeff Oct 16 '16 at 07:56
  • As for not typing the command again and again: you might want to learn how to use Makefile (or CMake) to build your system. It's not exactly friendly (a huge issue of C++), but once configured it's easy to invoke and automatically replays all those long commands (and also detects which need be replayed and which do not). – Matthieu M. Oct 18 '16 at 11:07

0 Answers0