从linux c++转入win c++开发配置环境的记录:powershell自动补全,msvc环境的导入

Terminal & autocomplete

先前从事Linux C++开发期间,个人PC端学习先后使用过 Fluent Terminal 和 Windows Terminal 搭配ssh + vscode,两者自带的毛玻璃特效视觉效果都很不错
image

Fluent Terminal

image

Windows Terminal
Windows 开发并不需要同 Linux 一样配合大量的命令行,对楼主来说不再需要用到 MobaXterm 或 Xshell 这样强的大集成工具,一个简单的 Terminal App 足矣应付

autocomplete

为了获得类似z-sh的 autocomplete 体验,我们需要 PSReadLine

Powershell 7 自动配置了该模块,默认配置开启了 autocomplete 功能(其实不叫这个名字,楼主对这个新事物还在摸索阶段),在官方链接中即可获取,由于众所周知的网络原因,推荐使用msi安装。
https://learn.microsoft.com/zh-cn/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3
安装完成后,可以将 Powershell 7 添加到 Terminal App 的 新建 Profile,以下是 autocomplete 的效果,类似 oh-my-zsh 的右键补全
image

或者如果读者无法安装 Powershell 7

PSReadLine 是可以单独安装的,参见
https://github.com/PowerShell/PSReadLine
安装完成后,输入 code $profile 或(如果没有VSCode) notepad.exe $profile,增加以下内容

Set-PSReadLineOption -PredictionSource History

MSVC 环境导入

配置 env 是 win C++ 开发最让我恶心的问题(不管是工作还是个人使用PC学习),在 Linux 上开发真没这么多吊事。Visual Studio 的开发环境并不被 Powershell 自动载入,为了能在 VSCode 中编译随手写的 C++ 文件,根据 VSCode 官方文档的说法,我们需要先进入 Visual Studio 的 Developer Command Prompt,通过它打开 VSCode、Powershell 或者其他需要 MSVC 编译器的App,才能正常使用 cl.exe 命令和 MSVC环境(我tm光打这行字都嫌累

捣鼓了一天,找到了解决方法
同样code $profile打开 Powershell 的 Profile 配置
输入

Import-Module "<VS安装路径>\<版本:如2022>\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"; 
Enter-VsDevShell -SkipAutomaticLocation -SetDefaultWindowTitle -InstallPath "<VS安装路径>\<版本:如2022>\Community\"

保存后打开的 Powershell 实例中即可正常使用包括 cl MSBuild 在内的一系列命令,通过该 Powershell 打开的 VSCode 也能默认加载 MSVC 环境了。

在Profile中设置了这俩命令,Powershell 初始化吊慢怎么办?

可怜楼主用的PC是苟延残喘了6年的拯救者,机械键盘已经带不动状如牛的 MSVC 环境了。
我在 Profile 中将上面两个命令封进了函数

function LoadMsbuild {
	Import-Module "<VS安装路径>\<版本:如2022>\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"; 
	Enter-VsDevShell -SkipAutomaticLocation -SetDefaultWindowTitle -InstallPath "<VS安装路径>\<版本:如2022>\Community\"
}
# P.S. LoadMsbuild只是我瞎想的名字,至于这俩命令具体干了啥,希望有大牛评论区解释下

这样楼主就可以仅在需要使用 msvc 时 call 这个LoadMsbuild命令
image

大功告成!写个hello world庆祝下吧
image

posted @ 2023-01-18 00:33  scuzqy  阅读(177)  评论(0)    收藏  举报