Vs2019编写C++概述(Games101-windows环境搭建-使用opencv与Eigen)

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++库,简单方便全自动

使用链接

Program 3D Games in C++: The #1 Language at Top Game Studios Worldwide C++ remains the key language at many leading game development studios. Since it’s used throughout their enormous code bases, studios use it to maintain and improve their games, and look for it constantly when hiring new developers. Game Programming in C++ is a practical, hands-on approach to programming 3D video games in C++. Modeled on Sanjay Madhav’s game programming courses at USC, it’s fun, easy, practical, hands-on, and complete. Step by step, you’ll learn to use C++ in all facets of real-world game programming, including 2D and 3D graphics, physics, AI, audio, user interfaces, and much more. You’ll hone real-world skills through practical exercises, and deepen your expertise through start-to-finish projects that grow in complexity as you build your skills. Throughout, Madhav pays special attention to demystifying the math that all professional game developers need to know. Set up your C++ development tools quickly, and get started Implement basic 2D graphics, game updates, vectors, and game physics Build more intelligent games with widely used AI algorithms Implement 3D graphics with OpenGL, shaders, matrices, and transformations Integrate and mix audio, including 3D positional audio Detect collisions of objects in a 3D environment Efficiently respond to player input Build user interfaces, including Head-Up Displays (HUDs) Improve graphics quality with anisotropic filtering and deferred shading Load and save levels and binary game data Whether you’re a working developer or a student with prior knowledge of C++ and data structures, Game Programming in C++ will prepare you to solve real problems with C++ in roles throughout the game development lifecycle. You’ll master the language that top studios are hiring for—and that’s a proven route to success. Table of Contents Chapter 1: Game Programming Overview Chapter 2: Game Objects and 2D Graphics Chapter 3: Vectors and Basic Physics Chapter 4: A
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值