使用 Visual Studio 2022 自带的 cl.exe 测试编译 opencv helloworld

1. 参考博客:https://blog.csdn.net/yangSHU21/article/details/130237669( 利用OpenCV把一幅彩色图像转换成灰度图 )( 代码用的此博客的,就改了下图片文件路径而已 )。

2. 编译探索步骤:

test.cpp:

#include <iostream>
#include "opencv2/opencv.hpp"
 
using namespace cv;
using namespace std;
 
void opencv_test();
void ConvertRGB2GRAY(const Mat& image, Mat& imageGray);
 
void opencv_test() {
	Mat src = imread("D:/sucai/picture/bizhi/0001.jpg");
	Mat src0 = imread("D:/sucai/picture/bizhi/0001.jpg", 0);
	Mat grayImage;
	Mat cvt_gray_image;
 
	//读入的彩色图
	namedWindow("origin_image", WINDOW_NORMAL);
	imshow("origin_image", src);
 
	//opencv使用imread函数读入彩色图,设置第二个参数为0得到的灰度图
	namedWindow("opencv_image", WINDOW_NORMAL);
	imshow("opencv_image", src0);
 
	//使用公式GRAY=0.299*R+0.587*G+0.114*B
	ConvertRGB2GRAY(src, grayImage);
	namedWindow("my_gray_image", WINDOW_NORMAL);
	imshow("my_gray_image", grayImage);
 
	//先读入彩色图,然后使用cvtColor函数进行灰度转化
	cvtColor(src, cvt_gray_image, COLOR_BGR2GRAY);
	namedWindow("cvt_gray_image", WINDOW_NORMAL);
	imshow("cvt_gray_image", cvt_gray_image);
	waitKey(0);
}
 
void ConvertRGB2GRAY(const Mat& image, Mat& imageGray)
{
	if (!image.data || image.channels() != 3)
	{
		return;
	}
	//创建一张单通道的灰度图像
	imageGray = Mat::zeros(image.size(), CV_8UC1);
	//取出存储图像像素的数组的指针
	uchar* pointImage = image.data;
	uchar* pointImageGray = imageGray.data;
	//取出图像每行所占的字节数
	size_t stepImage = image.step;
	size_t stepImageGray = imageGray.step;
 
 
	for (int i = 0; i < imageGray.rows; i++)
	{
		for (int j = 0; j < imageGray.cols; j++)
		{
			//opencv的通道顺序是BGR,而不是我们常说的RGB顺序
			pointImageGray[i * stepImageGray + j] =
				(uchar)(0.114 * pointImage[i * stepImage + 3 * j] +
					0.587 * pointImage[i * stepImage + 3 * j + 1] +
					0.299 * pointImage[i * stepImage + 3 * j + 2]);
		}
	}
}
 
 
 
int main()
{
	cout << "Hello CMake." << endl;
	opencv_test();
	return 0;
}

直接使用 cl test.cpp 编译,报错 "test.cpp(1): fatal error C1034: iostream: 不包括路径集",使用 Everything 搜索 iostream 文件所在位置为 "D:\install\VisualStudio2022_comm\VC\Tools\MSVC\14.40.33807\include",于是将 cl 语句中加入:

-I"D:/install/VisualStudio2022_comm/VC/Tools/MSVC/14.40.33807/include" ^

执行,报错 " fatal error C1083: 无法打开包括文件: “crtdbg.h”: No such file or directory",搜索 crtdbg.h 的位置为 C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt,向 cl 语句中加入 :

-I"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt" ^

执行,报错 "无法打开包括文件: “opencv2/opencv.hpp”: No such file or directory",这个我知道在哪,就不用搜了,当然你也可以搜索,向 cl 语句中加入:

-I"D:/install/opencv/opencv/build/include" ^

执行,报错 "LINK : fatal error LNK1104: 无法打开文件“libcpmt.lib”",搜索 libcpmt.lib 的位置为 

D:\install\VisualStudio2022_comm\VC\Tools\MSVC\14.40.33807\lib\x64,于是向 cl 语句中加入:

-link libcpmt.lib ^
-LIBPATH:"D:/install/VisualStudio2022_comm/VC/Tools/MSVC/14.40.33807/lib/x64" ^

执行,报错 "LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”",搜索 kernel32.lib 的位置为 

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64,于是向 cl 语句中加入:

-link kernel32.lib ^
-LIBPATH:"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.26100.0/um/x64"

执行,报错 "LINK : fatal error LNK1104: 无法打开文件“libucrt.lib”",搜索 libucrt.lib 的位置为C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64,于是向 cl 语句中加入:

-link libucrt.lib ^
-LIBPATH:"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.26100.0/ucrt/x64"

执行,报错:

test.obj
LINK : warning LNK4044: 无法识别的选项“/link”;已忽略
LINK : warning LNK4044: 无法识别的选项“/link”;已忽略
test.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl cv::Mat::Mat(void)" (??0Mat@cv@@QEAA@XZ),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "public: static class cv::MatExpr __cdecl cv::Mat::zeros(class cv::Size_<int>,int)" (?zeros@Mat@cv@@SA?AVMatExpr@2@V?$Size_@H@2@H@Z),函数 "void __cdecl ConvertRGB2GRAY(class cv::Mat const &,class cv::Mat &)" (?ConvertRGB2GRAY@@YAXAEBVMat@cv@@AEAV12@@Z) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?imread@cv@@YA?AVMat@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::namedWindow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?namedWindow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV_InputArray@1@@Z),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::cvtColor(class cv::_InputArray const &,class cv::_OutputArray const &,int,int)" (?cvtColor@cv@@YAXAEBV_InputArray@1@AEBV_OutputArray@1@HH@Z),函数 "void __cdecl opencv_test(void)" (?opencv_test@@YAXXZ) 中引用了该符号
test.exe : fatal error LNK1120: 8 个无法解析的外部命令

这个错就看不懂了,不是简单的 xxx 文件找不到活无法打开了,否则我使用 Everything 搜出来都给添加到头文件包含目录以及静态库和静态库目录,经过这几天的研究,我发现缺少的系统的文件( 例如 windows系统的头文件、库文件等,或者 vs2022自带的头文件、库文件等,例如 opencv 、tensorRT等的就不算是系统文件 )会报错 xxx 文件找不到,但是第三方的的文件不会提示这么明显的报错,但是我们知道我们此刻是在使用什么,当然是 opencv 了,缺少的要么是头文件,要么是库文件,头文件目录前面已经设置了,那肯定是库文件了,打开 opencv 的静态库文件目录 D:\install\opencv\opencv\build\x64\vc16\lib:

 将该目录下的 .lib 文件加进去,即向 cl 语句中添加如下信息:

-link opencv_world4100.lib ^
-link opencv_world4100d.lib ^
-LIBPATH:"D:/install/opencv/opencv/build/x64/vc16/lib" ^

执行,成功生成了 test.exe,最终的 cl 编译语句为:

cl ^
	-I"D:/install/VisualStudio2022_comm/VC/Tools/MSVC/14.40.33807/include" ^
	-I"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt" ^
	-I"D:/install/opencv/opencv/build/include" ^
    test.cpp ^
	-link libcpmt.lib ^
	-link kernel32.lib ^
	-link libucrt.lib ^
	-link opencv_world4100.lib ^
	-link opencv_world4100d.lib ^
	-LIBPATH:"D:/install/VisualStudio2022_comm/VC/Tools/MSVC/14.40.33807/lib/x64" ^
	-LIBPATH:"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.26100.0/um/x64" ^
	-LIBPATH:"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.26100.0/ucrt/x64" ^
	-LIBPATH:"D:/install/opencv/opencv/build/x64/vc16/lib"

执行 test.exe:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值