606 字
3 分钟
Making a Portable VS-C++ (Based on VSCode)

Main text#

Lazy version you can download directly without doing it yourself, password QRSJ

VSCode

* LLVM * mingw64

Download VSCode

Be sure to download the compressed archive (.zip) format.


Download LLVM

No need to select the sig file

Be sure to choose “Pre-Built Binaries”, and the system bitness must match the one you chose above!


Download MinGW-W64

If your system is x64, download x86_64-posix-seh.

If your system is x32, download x86_64-win32-seh.


  1. Extract VSCode somewhere, then extract MinGW into the VSCode directory

The reason I do these next steps is just to get the LLVM directory. I uninstall it only because I don’t want too many items in the program list. If you’re not obsessive about that, you can skip steps 2-4 and just choose the first folder as the installation directory.

  1. Install LLVM, install it anywhere for now

  2. Copy the entire LLVM directory into the directory from step 1

  3. Uninstall the LLVM from your system

  4. Copy !start.vbs and start.bat from the appendix into the folder from step 1

  5. Comment out the last line of start.bat i.e. add :: before the last line

  6. !!Don’t close the window after this step!! In cmd (or PowerShell), run the script with .\start.bat, then type clang and gcc. If you get “xxx is not recognized as an internal or external command”, it means your start.bat configuration has a problem.

  7. In the folder from step 1, create folders with the following names:

Terminal window
Users
Users\AppData
Users\AppData\Local
Users\AppData\Roaming
Users\AppData\Desktop
extensions
  1. Install extensions
Terminal window
Chinese(简中语言包)
C/C++
vscode-clangd
Code Runner
Bracket Pair Colorizer 2
One Dark Pro(主题包,可选)
  1. In the cmd (or PowerShell) from step 7, type .\code.exe to launch VSCode

  2. Create a new directory in the file explorer (I just created a folder named !Code in the step 1 directory), open it in VSCode, then choose “Save Workspace As”

Note that you should put each language’s working directory in a separate folder, e.g. put C in the c folder and C++ in the c++ folder

  1. Create a new folder named C++ inside !Code (adjust based on your own situation)

  2. Create a .vscode folder in the C++ directory, then copy launch.json, tasks.json, settings.json, and compile_flags.txt from the appendix into it

  3. Uncomment the line from step 6, close this VSCode, and run !start.vbs

  4. Create a new file and type in some test code

#include <iostream>
using namespace std;
int main (){
string a="";
cout << "Input:" << endl;
cin >> a;
cout << a;
return 0;
}
  1. Click the arrow in the top right, and see if the console outputs Input:. Then type something and see if it’s echoed back unchanged

  2. That’s it

Appendix#


start.bat#

Terminal window
set ThisDir=%~dp0
set USERPROFILE=%ThisDir%Users
set APPDATA=%USERPROFILE%\AppData\Roaming
set path=%ThisDir%LLVM\bin;%ThisDir%mingw64\bin;%path%
REM Code.exe --extensions-dir "extensions"
Code.exe !Code\C++\code.code-workspace

!start.vbs#

Terminal window
set ws=createobject("wscript.shell")
ws.run "start.bat",0

launch.json#

{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile"
}
]
}

tasks.json#

{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"command": "clang++", //如果你是用c语言,改成clang
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g",
"-Wall",
"-static-libgcc",
"--target=x86_64-w64-mingw",
],
"type": "process",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
}
]
}

settings.json#

{
"files.defaultLanguage": "c",
"editor.formatOnType": true,
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.acceptSuggestionOnEnter": "off",
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"c": "cd $dir && clang '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && &'$dir$fileNameWithoutExt'",
"cpp": "cd $dir && clang++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && &'$dir$fileNameWithoutExt'"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.preserveFocus": true,
"code-runner.clearPreviousOutput": false,
"code-runner.ignoreSelection": true,
"C_Cpp.clang_formatSortIncludes": true,
"C_Cpp.errorSquiggles": "Disabled",
"C_Cpp.autocomplete": "Disabled",
"C_Cpp.suggestSnippets": false,
}

compile_flags.txt#

Terminal window
-Wall
--target=x86_64-w64-mingw
-std=c++17
#写的语言是C,注释掉上面那行
Making a Portable VS-C++ (Based on VSCode)
https://tski.uk/blog/en/vscode-portable/
作者
Tokisaki Galaxy
发布于
2019-08-27
许可协议
CC BY