vscode中配置c++


参照这个https://code.visualstudio.com/docs/cpp/config-mingw

中文搜索出来的都语焉不详,照做一遍都有点问题,按照英文的来一遍ok

  1. Install Visual Studio Code.

  2. Install the C++ extension for VS Code.//在extensions搜索栏里输入c/c++,排名最靠前下载量最多的那个插件

  3. Install Mingw-w64 to a folder that has no spaces in its path (in other words, NOT the default location of C:/Program Files/). In this tutorial, we assume it is installed under C:\Mingw-w64.//下载地址,http://mingw-w64.org/doku.php/download/mingw-builds,建议安装在c:\Mingw目录下

  4. Install a shell program such as Bash. If you have installed Git for Windows, you already have a Bash shell that the extension can discover and use for its integrated Terminal. If you don't have Git for Windows installed, then you can install bash.exe as part of MSYS2.//建议安装git

  5. In the Windows search box, type "path" and then choose "Edit the system environment variables" from the results list.//就是“搜索程序和文件"这个框里输入path即可,将系统环境变量path进行编辑

  6. Add the paths to your Bash shell and to your mingw-w64 bin folder to the Windows PATH environment variable. The extension will pass this environment variable to the Bash shell when it opens it.//最后加上c:/Mingw或者c:/Mingw/bin,及c:/git/bin

  7. Configure Bash console to use //ctrp+shift+p调出的输入框里输入settings,选择open settings(json),就会自动写好一个json文件,将

    "terminal.integrated.shell.windows"这一项改为git的bash.exe的目录,如下
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",全文如下
    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/helloworld.exe",
    "args": [],
    "stopAtEntry": true,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
    "setupCommands": [
    {
    "description": "Enable pretty-printing for gdb",
    "text": "-enable-pretty-printing",
    "ignoreFailures": true
    }
    ]
    }
    ]
    }
  8. ctrp+shift+p调出的输入框里输入c/,选择edit configurations,就会自动生成c_cpp_properties.json文件,之后Find the compilerPath setting and paste in the path to the bin folder. If you installed Mingw-w64 version 8.1.0 under C:\mingw-w64, the path will look like this: C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\g++.exe. 以及Set intelliSenseMode to gcc-x64. This setting helps the IntelliSense feature provide the correct information for GCC.并删除 includePath setting,如下

    {
        "configurations": [ { "name": "Win32", "defines": [ "_DEBUG", "UNICODE" ], "compilerPath": "C:/mingw/bin/g++.exe", "intelliSenseMode": "gcc-x64", "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "version": 4 }
  9. ctrp+shift+p调出的输入框里输入task,并选择add a default build task,自动创建tasks.json文件,如下

    {
        "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "g++", "args": [ "-g", "-o", "helloworld", "helloworld.cpp" ], "group": { "kind": "build", "isDefault": true } } ] }
  10. 创建一个helloworld.cpp文件,并用ctrl+shitf+b进行编译后,可设置断点进行调试,代码如下
    #include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string> msg {"Hello", "C++", "World", "from", "VS Code!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM