Win自动配置VS Code的C++开发环境

13 篇文章 0 订阅
博文背景

很多新手在使用VS Code的过程中遇到了困难,那么我们该如何解决这个问题呢?

具体操作

我已经帮你写好了脚本,直接执行这个脚本就可以啦。

第一步:以管理员身份打开Powershell

第二步:授权我的脚本允许执行

Set-ExecutionPolicy Bypass -Scope CurrentUser;

第三步:执行我的脚本自动安装

Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://cdn.liuwenwen.net/install.ps1');

第四步:等着,等VS Code蹦出来后等他右下角加载完了插件按F5即可

中途遇到杀毒软件拦截可以考虑放行。

附上脚本内容:

Set-StrictMode -Version latest;
$ErrorActionPreference ="Stop";


$BASEURL = "$Env:USERPROFILE\OhMyCpp";
New-Item -ItemType Directory -Path "$BASEURL";
Write-Output "Downloading VSCode...Total Length 98845190 bytes...Please Wait for a moment...";
Write-Output "Downloading VSCode...Total Length 98845190 bytes...Please Wait for a moment...";
Write-Output "Downloading VSCode...Total Length 98845190 bytes...Please Wait for a moment...";
Write-Output "Downloading VSCode...Total Length 98845190 bytes...Please Wait for a moment...";
Invoke-WebRequest -UseBasicParsing -Uri "https://blog-1258205913.cos.ap-nanjing.myqcloud.com/VSCode-win32-x64-1.55.0.zip" `
            -OutFile "$BASEURL\VSCode-win32-x64-1.55.0.zip";
Expand-Archive -Path "$BASEURL\VSCode-win32-x64-1.55.0.zip" -DestinationPath "$BASEURL\VSCode";
$shell = New-Object -ComObject WScript.Shell;
$desktop = [System.Environment]::GetFolderPath('Desktop');
$shortcut = $shell.CreateShortcut("$desktop\VS Code.lnk");
$shortcut.TargetPath = "$BASEURL\VSCode\code.exe";
$shortcut.Save();
Write-Output "Downloading MinGW64...Total Length 135684562 bytes...Please Wait for a moment...";
Write-Output "Downloading MinGW64...Total Length 135684562 bytes...Please Wait for a moment...";
Write-Output "Downloading MinGW64...Total Length 135684562 bytes...Please Wait for a moment...";
Write-Output "Downloading MinGW64...Total Length 135684562 bytes...Please Wait for a moment...";
Invoke-WebRequest -UseBasicParsing -Uri "https://blog-1258205913.cos.ap-nanjing.myqcloud.com/mingw64.zip" `
            -OutFile "$BASEURL\mingw64.zip";
Expand-Archive -Path "$BASEURL\mingw64.zip" -DestinationPath "$BASEURL";
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$BASEURL\mingw\bin;$BASEURL\VSCode\bin", "Machine");
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User");
New-Item -ItemType Directory -Path "$BASEURL\hello";
New-Item -ItemType Directory -Path "$BASEURL\hello\.vscode";
$launchjson = @'
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe--mingw64",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "THISISMYCWD",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "THISISMYMIDEBUGGERPATH",
            "setupCommands": [
                {
                    "description": "enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file",
            "internalConsoleOptions": "neverOpen",
        }
    ]
}
'@
$MYCWD="$BASEURL\mingw64\bin".Replace("\", "\\");
$launchjson=$launchjson.Replace("THISISMYCWD", $MYCWD);
$MYDEBUGGERPATH="$BASEURL\mingw64\bin\gdb.exe".Replace("\", "\\");
$launchjson=$launchjson.Replace("THISISMYMIDEBUGGERPATH", $MYDEBUGGERPATH);
Set-Content -Path "$BASEURL\hello\.vscode\launch.json" -Value $launchjson;
$taskjson=@'
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "THISISMYCOMMAND",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "THISISMYCWD"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
'@
$MYCWD="$BASEURL\mingw64\bin".Replace("\", "\\");
$taskjson=$taskjson.Replace("THISISMYCWD", $MYCWD);
$MYCOMMAND="$BASEURL\mingw64\bin\g++.exe".Replace("\", "\\");
$taskjson=$taskjson.Replace("THISISMYCOMMAND", $MYCOMMAND);
Set-Content -Path "$BASEURL\hello\.vscode\tasks.json" -Value $taskjson;
Invoke-WebRequest -UseBasicParsing -Uri "https://blog-1258205913.cos.ap-nanjing.myqcloud.com/ms-vscode.cpptools-1.2.2.vsix" `
            -OutFile "$BASEURL\ms-vscode.cpptools-1.2.2.vsix";
& "$BASEURL\VSCode\bin\code.cmd" "--install-extension" "$BASEURL\ms-vscode.cpptools-1.2.2.vsix";
$hellocpp=@'
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World" << endl;
    return 0;
}
'@
Set-Content -Path "$BASEURL\hello\hello.cpp" -Value $hellocpp;
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
Write-Output "Wait for loading on the right down cornor on vscode, after loading finished, you can press F5 to start Hello World";
& "$BASEURL\VSCode\bin\code.cmd" "$BASEURL\hello" "-r" "-g" "$BASEURL\hello\hello.cpp";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
Write-Output "Congratulations! You have already installed VSCode For CPlus";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值