VS code win10 環境,下載安裝調試

背景: 公司要用vs code 在win10下跑通代碼單步調試。下面是具體的操作。

安裝環境:win10,MinGW

下載地址

https://code.visualstudio.com/

安裝之後需要install C/C++插件

打開vscode,按ctrl+p打開快速命令框,輸入以下命令後等待

ext install cpptools

點擊需要的插件 install

完成後,需要點擊reload,使安裝生效。Reload之後的效果:

2 安裝編譯/調試環境

這裏使用的是codeblocks下的MinGW.

下載地址(不建議用win10自帶的IE瀏覽器,這裏用的qq瀏覽器):

https://sourceforge.net/projects/codeblocks/files/Binaries/17.12/Windows/

雙擊安裝,一路next即可。

安裝完成後會提示是否打開codeblocks,點擊是,跳出編譯器選擇界面,可以看到已經安裝好了gnu gcc compiler.

配置環境變量

在codeblocks的安裝目錄下,有g++編譯器,C:\Program Files (x86)\CodeBlocks\MinGW\bin。把這個目錄添加到環境變量裏。

Hello world~

在D盤新建一個文件夾,命名爲code.然後建一個文件,hello_world.c    打開VS Code,

File --> open folder --> 選中D:/code文件夾。出現如下界面。

選中code下方的區域,右鍵,新建文件hello.cpp。

 

Hello.cpp與hello.c都可以試着跑一下,代碼如下:

Hello.cpp

#include <iostream>

#include <stdlib.h>

 

int main()

{

    std::cout<< "hello world !\n";

    system("pause");

 

    return 0;

}

 

Hello.c

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    printf("hello world !\n");

    system("pause");

    return 0;

}

配置.json文件

點擊左側第4個按鈕(調試按鈕),再點擊工具欄帶紅點的齒輪按鈕,選擇c++(GDB/LLDB)

會在工作目錄下生成一個launch.json的啓動配置文件,將內容替換爲以下內容:

{

    // 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": "(gdb) Launch",

            "type": "cppdbg",

            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",

            "args": [],

            "stopAtEntry": false,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "gdb",

            "miDebuggerPath": "D:\\Program Files (x86)\\CodeBlocks\\MinGW/bin\\gdb32.exe",// miDebugger的路徑,注意這裏要與MinGw的路徑對應

            "preLaunchTask": "g++"// 調試會話開始前執行的任務,一般爲編譯程序,c++爲g++, c爲gcc

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ]

        }

    ]

}

 

注意:"miDebuggerPath"中的斜槓最好用\\,否則會出現鏈接問題。

D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe:d:\code\.vscode\launch.json: file format not recognized; treating as linker script

D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe:d:\code\.vscode\launch.json:1: syntax error

collect2.exe: error: ld returned 1 exit status

The terminal process terminated with exit code: 1

 

在.vscode文件夾中再創建一個文件:tasks.json,將以下內容copy到文件裏。

{

    "version": "2.0.0",

    "command": "g++",

    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 編譯命令參數

    "problemMatcher": {

        "owner": "cpp",

        "fileLocation": ["relative", "${workspaceRoot}"],

        "pattern": {

            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",

            "file": 1,

            "line": 2,

            "column": 3,

            "severity": 4,

            "message": 5

        }

    }

}

 

確保2個.json文件在.vscode文件夾下面。否則,會出現could not find the task 'g++' 的編譯問題。

左側第一個按鈕是查看打開的文件,第4個按鈕是調試代碼用的。

調試時,先點第4個按鈕,再點擊綠色的箭頭按鈕,就可以看到有打印數據出現。

 

參考文檔:

https://blog.csdn.net/bat67/article/details/81268581

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章