1

For the past week, I have been trying to run some simple OpenCV programs using the terminal. I have tried many tutorials and recommendations from various forums with little success. The problem arises when trying to link the OpenCV header files to my OpenCV main program. For a simple c++ program I would simply execute g++ main.cpp header.hpp to generate the program executeable. How do I go about linking the necessary OpenCV header files such as <opencv2/highgui/highgui.hpp> & <opencv2/core/core.hpp>?

For example, when attempted to execute the sample program from http://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html the following occurs:

Desktop Robert$ g++ loadIMG.cpp Undefined symbols for architecture x86_64: "cv::namedWindow(cv::String const&, int)", referenced from: _main in loadIMG-54c517.o "cv::Mat::deallocate()", referenced from: cv::Mat::release() in loadIMG-54c517.o "cv::Mat::copySize(cv::Mat const&)", referenced from: cv::Mat::operator=(cv::Mat const&) in loadIMG-54c517.o "cv::String::deallocate()", referenced from: cv::String::~String() in loadIMG-54c517.o "cv::String::allocate(unsigned long)", referenced from: cv::String::String(char const*) in loadIMG-54c517.o "cv::imread(cv::String const&, int)", referenced from: _main in loadIMG-54c517.o "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from: _main in loadIMG-54c517.o "cv::waitKey(int)", referenced from: _main in loadIMG-54c517.o "cv::fastFree(void*)", referenced from: cv::Mat::~Mat() in loadIMG-54c517.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Please note: OpenCV has already been built using the following tutorial: http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/

Any help or direction would be appreciated. Thank you.

Bobby L.
  • 73
  • 2
  • 6
  • If you want to install non-Apple packages, such as `OpenCV` or `ImageMagick` or `ffmpeg` or `gnuplot` or `parallel` or `pandoc` or `p7zip`, on a Mac, you really will do yourself a favour by installing `homebrew` from http://brew.sh as your Package Manager to download, install, update and remove packages. Then `OpenCV` is as easy as `brew install opencv` and you are finished. – Mark Setchell Nov 26 '15 at 09:59

2 Answers2

3

You haven't specified:

  1. the include path (header search path) using -I"/path/to/your/include"
  2. the path to the libraries using -L"/path/to/libraries"
  3. which libraries to link against, in this case core and highgui: -lopencv_core -lopencv_highgui

I have opencv headers in /opt/local/include and libraries in /opt/local/lib, so to compile a basic program like this:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
        Mat src = Mat(Size(320,240),CV_64F);;
        namedWindow("test");

        cout << "press any key to close" << endl;

        while(true){
                randn(src,0,1.0);
                imshow("test",src);
                if(waitKey() > 0) break;
        }
}

I compiled like so:

g++ main.cpp -I"/opt/local/include/" -L"/opt/local/lib/" -lopencv_core -lopencv_highgui -o main

Then ran ./main:

opencv test

Bare in mind you might have opencv installed in the /usr/local folder not /opt/local depending how you compiled/installed OpenCV.

Also, you might have pkg-config installed which can come in handy when you need to link against more libraries.

For example, you can run:

pkg-config --libs --cflags opencv

which in my case outputs:

-I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab 

but in your case, it should output your particular OpenCV paths.

This would simplify compiling to this:

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

The guide you linked to uses cmake which generates Makefiles for you. That's another nice options. Also, based on the same guide, you should have XCode installed which you can use to create a Command Line Tool and point the Header Search Paths and Library Search Paths.

Xcode Command Line Tool project creation

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • I am getting an error: `'opencv2/opencv.hpp' file not found`. How must my installed opencv-3.0.0 directory be arranged on my computer? Where do I find the headers and libraries? I also noticed that my `/usr/local/` directory is empty. – Bobby L. Nov 26 '15 at 04:09
  • When you followed the tutorial and created the SharedLibs folder, then used ```make``` then ```make install```, what folder did you use ? If you're not sure, go back to CMake and scroll down until you find the ```CMAKE_INSTALL_PREFIX``` section. This should be path the compiled shared libraries and headers will have been copied after compilation. – George Profenza Nov 26 '15 at 09:51
  • Have you tried using finder to locate it? It might not have installed correctly. You'll probably find several versions of it left over from the build process. The one you want is probably in /opt or /usr. Personally I find it easier to install opencv using mac port rather than build it myself. Also opencv3 has different header file layouts to 2.4 see http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html – docPhil Nov 26 '15 at 09:56
  • @BobbyL. If you run ```pkg-config --libs --cflags opencv``` in Terminal, what output do you get ? – George Profenza Nov 26 '15 at 10:13
  • works! Should `g++ main.cpp `pkg-config --libs --cflags opencv` -o main` contain all OpenCV 3 header files? For example: `` – Bobby L. Nov 26 '15 at 15:42
  • Awesome! the --cflags should figure out which **folder** contain the headers files needed (without worrying which files are actually needed) and your .cpp file only needs to worry about what header **files** it needs to access (without worrying about the folder) – George Profenza Nov 26 '15 at 16:09
0

I create a similar file that maybe can help you.

First I use:

sudo brew install opencv

Then I install the opencv.3.0 according to the hint given by the terminal. Then in the .cpp file which needs the API from opencv, I use:

#include "opencv2/opencv.hpp"

As my only include file about opencv. Actually, at that project I use highgui, core, and imgprog. But no worries here, I will show you how to solve them in linking part.

After you finish your project, you are going to compile your project on the terminal.

Because I also used the JNI interface, so I still need to link the jni.h.

Here we go:

g++   xxxx.cpp xxx.cpp -lstdc++ -fPIC -shared (to create a shared object)  
-I/absolute path/ (we can use -I  to be followed with  the absolute path of the library you need to use )
-I/Users/yuanzhan/Downloads/OpenCV-2.0.0/src/
-I /Users/yuanzhan/Downloads/OpenCV-2.0.0/include/opencv/ -I/usr/local/Cellar/opencv3/3.1.0_3/lib   -lopencv_core (open the library for use if you use the API fro here)-lopencv_highgui -lopencv_imgproc -L.(i put the cv2. on local otherwise you can add the absolute path here) -lcv2(use the package)  -v -o libopenCvSDK.so(generate my .so package).
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Hui Xu
  • 1