由于未定义的引用,无法获取OpenCV进行编译? [英] Cannot get OpenCV to compile because of undefined references?
问题描述
代码很简单,直接来自此教程。我运行Arch Linux并且OpenCV库存储在 / usr / include /
。我还检查以确保 / usr / include
在我的PATH。
The code is simple and is essentially straight from this tutorial. I am running Arch Linux and have the OpenCV library stored at /usr/include/
. I have also checked to ensure that /usr/include
is in my PATH.
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace cv;
int main(int argc, char** argv){
Mat image;
Mat grayImage;
if(!argv[1]){
std::cerr << "No image data!" << std::endl;
return -1;
}
image = imread(argv[1], 1);
cvtColor(image, grayImage, CV_BGR2GRAY);
imwrite("Gray_Image.jpg", grayImage);
namedWindow(argv[1], CV_WINDOW_AUTOSIZE);
namedWindow("Gray Image", CV_WINDOW_AUTOSIZE);
imshow(argv[1], image);
imshow("Gray Image", grayImage);
waitKey(0);
return 0;
}
编译器进程成功找到并包含这些头文件,但我仍然得到undefined编译时的参考错误。如果查看我包括的头文件,他们还包括 / usr / include / opencv2
中的其他文件。我检查和这样的头文件确实存在。
The compiler process successfully finds and include these header files, but I still get undefined reference errors at compile-time. If you look into the header files I included they further include other files in /usr/include/opencv2
. I have checked and such header files do exist.
有什么想法吗?
/tmp/ccudBcqD.o: In function `main':
test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)'
test.cpp:(.text+0x11f): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
test.cpp:(.text+0x138): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x158): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
test.cpp:(.text+0x180): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x1ca): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
test.cpp:(.text+0x241): undefined reference to `cv::namedWindow(std::string const&, int)'
test.cpp:(.text+0x291): undefined reference to `cv::namedWindow(std::string const&, int)'
test.cpp:(.text+0x2bf): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x2ff): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
test.cpp:(.text+0x32d): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x361): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
test.cpp:(.text+0x383): undefined reference to `cv::waitKey(int)'
/tmp/ccudBcqD.o: In function `cv::Mat::~Mat()':
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccudBcqD.o: In function `cv::Mat::operator=(cv::Mat const&)':
test.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccudBcqD.o: In function `cv::Mat::release()':
test.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
[Finished in 1.1s with exit code 1]
[shell_cmd: g++ "/home/branden/Desktop/OpenCV/test.cpp" -o "/home/branden/Desktop/OpenCV/test"]
[dir: /home/branden/Desktop/OpenCV]
[path: /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/vendor_perl:/usr/bin/core_perl]
推荐答案
这是一个链接器问题。尝试:
This is a linker issue. Try:
g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs`
这应该可以编译源代码。但是,如果您最近从源编译OpenCV,您将在运行时遇到链接问题,将不会找到库。
在大多数情况下,在从源代码编译库之后,您需要最后执行:
This should work to compile the source. However, if you recently compiled OpenCV from source, you will meet linking issue in run-time, the library will not be found. In most cases, after compiling libraries from source, you need to do finally:
sudo ldconfig
这篇关于由于未定义的引用,无法获取OpenCV进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!