这种错误提示通常是由于编译器无法找到链接的库文件或者缺少必要的头文件导致的。解决方法如下:
示例代码:
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("test.jpg");
if (img.empty())
{
cout << "Failed to open image!" << endl;
return -1;
}
imshow("Test", img);
waitKey(0);
return 0;
}
在编译时需要添加opencv的链接选项:
g++ test.cpp -o test -lopencv_core -lopencv_imgcodecs -lopencv_highgui
示例代码:
#include
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
如果没有添加iostream头文件会出现错误提示:
test.cpp:(.text+0x10): undefined reference to `std::cout'
test.cpp:(.text+0x15): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0x1d): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x22): undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
test.cpp:(.text+0x2a): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x35): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
需要添加iostream头文件:
#include
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
示例代码:
#include
using namespace std;
int main()
{
int a = 0;
int b = 1;
int c = b / a;
cout << c << endl;
return 0;
}
编译时添加调试选项:
g++ -g test.cpp -o test
运行编译后的可执行文件:
./test
运行结果如下:
Floating point exception (core dumped)
问题明显是除以0了,可以通过这个调试信息定位到问题并进行解决。
下一篇:编译指令ubuntu