文章目录
1.VS2019下载
安装opencv
下载windows,安装即可
新建控制台项目,解决方案右键,属性
添加头文件目录,外部包含目录点击添加(例如:opencv的include目录)
2.编译,运行
添加动态库,静态库
什么是动态库与静态库
参考:
https://www.runoob.com/w3cnote/cpp-static-library-and-dynamic-library.html
静态库:程序编译时需要使用
如果缺少静态库,程序会编译失败,提示无法解析的外部符号
添加静态库(编译需要):
右键解决方案,属性选择当前配置(或者所有配置),所有平台(或者电脑所使用的64位系统,x64)
添加静态库 .lib 文件目录(以opencv为例:添加opencv\build\x64\vc15\lib)
opencv的动态库与静态库
Debug模式添加
opencv_world453d.lib
不要同时添加
opencv_world453.lib
opencv_world453d.lib
否则出错
出现此错误,则说明链接器的输入,同时添加了两条
lib并存的方法
每次修改lib显然很麻烦,有时候需要在debug配置与其余配置切换,所以可以同时添加,然后再程序开头指定链接哪个lib
1.删除所有在链接器输入的lib,
2.在mian.cpp上方添加需要使用的lib
#pragma comment(lib,"opencv_world453.lib")
添加动态库(运行需要):
动态库:程序运行时需要使用
如果程序运行时,提示缺少dll,则需要将动态库的文件夹路径,加入到环境变量中
注意:
1.动态库可以直接复制到exe程序下面,不添加环境变量
2.添加dll所在文件夹到环境变量中(也可复制到C:\Windows\System32,本质是添加到环境变量,因为System32在环境变量中)
添加环境变量
win + Q 搜索环境变量
opencv的动态库与静态库
如果添加了环境变量后,电脑需要重启,环境变量才会生效
如果重启后发现还是找不到dll,出现这种情况建议直接 拷贝到C:\Windows\System32
3.测试代码
#include <opencv2/opencv.hpp>
#include <iostream>
#pragma comment(lib,"opencv_world453d.lib")//链接器的附加库目录需要存在此lib
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("D:/Test.png");
imshow("picture", src);
waitKey(0);
return 0;
}
效果:
4.安装Eigen
方法与Opencv一样,
网上搜索Eigen,下载最新版
解压缩
添加 文件夹到附加库目录即可使用(不需要添加静态库与动态库)
5.总结:
opencv 的安装是最麻烦的,因为头文件,动态库,静态库都涉及到了,出现错误请务必按流程检查,静态库添加是否正确,动态库是否缺少
由此总结出C++第三方库的使用,有文档按照文档进行安装与使用,没有文档则按照静态库,动态库,头文件,分别添加三者的引用即可。
6.Vs2019 使用Cmake(推荐)
新建Cmake项目
添加头文件,静态库
Cmakelists 参考
# CMakeList.txt: VS2019Cmake 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
project ("VS2019Cmake")
# 头文件
include_directories(D:/Games101Learn/package/opencv/build/include)
include_directories(D:/Games101Learn/package/Eigen/eigen-3.4.0)
#静态链接库
link_directories(D:/Games101Learn/package/opencv/build/x64/vc15/lib)
# 将源代码添加到此项目的可执行文件。
add_executable (VS2019Cmake "VS2019Cmake.cpp" "VS2019Cmake.h" "my.cpp" "my.h")
# TODO: 如有需要,请添加测试并安装目标。
参考代码:
运行即可
vs2019 会自动智能提示添加到camke的头文件
7.建议使用Vcpkg(推荐)
建议使用Vcpkg 安装C++库,简单方便全自动