Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

c++ - Running OpenCV 3 in Mac Terminal

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...