dimanche 30 août 2015

failure to call cv::imread in opencv on mac while compiling with g++

When I call the cv::imread() in main.cpp file on Mac 10.10, I confront with this problem:

Undefined symbols for architecture x86_64:
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
  readOpenCv()     in main.o
"cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
  readOpenCv()     in main.o

However, if I call the function in C language, all the things run well.

IplImage* img = cvLoadImage( filename, CV_LOAD_IMAGE_COLOR);
cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
cvShowImage("MyJPG", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "MyJPG" );

someone people say I should add -stdlib=libstdc++ in my makefile, however, it's still useless. After half day passed by, I finally find a solution. Except add -stdlib=libstdc++ on makefile, I have to specify the CXX variable in makefile, because the default g++ will still call the c++ in Mac system instead of installed by homebrew. My whole makefile is like below:

objects = main.o

#you have to specify the c++ version, c++-4.9 was installed by homebrew
CXX = /usr/local/bin/c++-4.9
CC = /usr/local/bin/g++-4.9
PKG_CONFIG = /usr/local/bin/pkg-config

# use any one of the LDFLAGS below
LDFLAGS = `$(PKG_CONFIG) --libs protobuf` `$(PKG_CONFIG) --libs --cflags opencv`
# LDFLAGS = -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_highgui

main: $(objects) 
    $(CC) -o main $(objects) $(LDFLAGS)
    # if unspecified the varibale CC, you have to add options -stdlib=libstdc++
    # $(CC) -o main $(objects) $(LDFLAGS) -stdlib=libstdc++

run:
./main

.PHONY : clean
clean:
    -rm main $(objects)

I hope my experience is able to help someone out of troubles.

Aucun commentaire:

Enregistrer un commentaire