2

I am using OpenCV to record the video from webcam. I used Xcode 6.4 to write a (C++) code to record the video on OS X Yosemite 10.10.4. It is working perfectly fine when I compile and run it from Xcode but when I want to compile (using g++ -o main main.cpp) it from terminal it generates following errors:

    Undefined symbols for architecture x86_64:
      "cv::VideoWriter::write(cv::Mat const&)", referenced from:
          _main in main-027833.o

      "cv::VideoWriter::VideoWriter(cv::String const&, int, double, cv::Size_<int>, bool)", referenced from:
          _main in main-027833.o

      "cv::VideoWriter::~VideoWriter()", referenced from:
          _main in main-027833.o

      "cv::VideoCapture::VideoCapture(int)", referenced from:
          _main in main-027833.o

      "cv::VideoCapture::~VideoCapture()", referenced from:
          _main in main-027833.o

      "cv::VideoCapture::operator>>(cv::Mat&)", referenced from:
          _main in main-027833.o

      "cv::Mat::deallocate()", referenced from:
          cv::Mat::release() in main-027833.o

      "cv::String::deallocate()", referenced from:
          cv::String::~String() in main-027833.o

      "cv::String::allocate(unsigned long)", referenced from:
          cv::String::String(char const*) in main-027833.o

      "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
          _main in main-027833.o

      "cv::waitKey(int)", referenced from:
          _main in main-027833.o

      "cv::fastFree(void*)", referenced from:
          cv::Mat::~Mat() in main-027833.o

      "cv::VideoCapture::get(int) const", referenced from:
          _main in main-027833.o

      "cv::VideoCapture::isOpened() const", referenced from:
          _main in main-027833.o

    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command fail

ed with exit code 1 (use -v to see invocation)

I know its because it can't link OpenCV lib. I tried using "vi

$HOME/.bash_profile" and then setting path like

"export PATH=$PATH:/usr/local/include" 

and

"export PATH=$PATH:/user/local/lib"

but it doesn't help. Also I tried

g++ -o main  main.cpp -l `pkg-config opencv --cflags --libs`

but of no use. I don't know how to specify path. Any help would be appreciated. Thanks!

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
student
  • 41
  • 6
  • Thanks for reply! No its not, because all my problem is related to OpenCV link errors. I compiled a C++ "hello world" program from terminal and it works fine but while I am compiling a code including OpenCV functions it generates the above specified errors. – student Jul 29 '15 at 05:21
  • did you read [Failure to link against appropriate libraries/object files or compile implementation files](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400) in the linked question? – m.s. Jul 29 '15 at 05:24
  • Thanks again for your reply. Yes I do. As I mentioned in my question when I compile using Xcode it is working perfectly fine that means I added all the library path correctly as specified in the question/answer you referred. The only problem is I don't know how to add that library path while compiling it from terminal. I tried this one now: "g++ -o main main.cpp -l /usr/local/lib" but end up with following error "ld: library not found for -l/usr/local/lib clang: error: linker command failed with exit code 1 (use -v to see invocation)" – student Jul 29 '15 at 06:41
  • it needs to be an uppercase `-L` which specifies the folder where the libraries are located as well as a lowercase `-lname_of_library` which tells the linker to link against this specific library – m.s. Jul 29 '15 at 06:57

1 Answers1

1

Finally, after 3 days of frustration I found the solution. I am sharing for someone who is also struggling with the same kind of problem.

What you have to do is just add all those flags that you added in the Xcode "other linker flags" while compiling your code from terminal. So here is the command that works for me.

g++ main.cpp -o main -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_calib3d -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_highgui

I didn't include all the flags because I was only working with videos but you may have to add more or less depending on what you are working on.

student
  • 41
  • 6