出现这种情况通常是因为C++程序中使用了某个动态链接库文件(.dll),但该文件未正确安装或未在程序运行时被引用。解决方法是在编译时将该动态链接库文件一同打包到可执行文件中,这样在运行程序时就不会出现缺失该文件的情况。 以下是将OpenCV库中的动态链接库文件打包到C++程序中的示例代码:
#include
#include
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("image.jpg");
if (img.empty())
{
cout << "Could not read the image" << endl;
return 1;
}
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", img);
waitKey(0);
return 0;
}
编译命令:
g++ -o program_name main.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs
其中"-lopenvc_core"、"-lopencv_highgui"和"-lopencv_imgcodecs"是OpenCV库中的动态链接库文件,通过将它们包含在编译命令中,可将它们打包到最终的可执行文件中,从而避免了启动时缺失.dll文件的问题。