opencv动态链接库错误

问题描述

1
2
3
4
5
6
7
8
9
10
11
/tmp/cc961U4H.o: In function `main':
ie.cpp:(.text+0xac): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/tmp/cc961U4H.o: In function `cv::Mat::~Mat()':
ie.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc961U4H.o: In function `cv::Mat::operator=(cv::Mat const&)':
ie.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/cc961U4H.o: In function `cv::Mat::create(int, int, int)':
ie.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0x9d): undefined reference to `cv::Mat::create(int, int const*, int)'
/tmp/cc961U4H.o: In function `cv::Mat::release()':
ie.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status

解决

在编译命令后加:

1
`pkg-config  --cflags --libs opencv`

例如:

1
g++ -o ie ie.cpp -std=c++11 `pkg-config  --cflags --libs opencv`