vscode如何编译运行c/c++程序

class="content_article_viewer_show">

center;">vscode如何编译运行c/c++程序

color: rgb(79, 79, 79); line-height: 32px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; white-space: normal; background-color: rgb(255, 255, 255);">安装微软C/C++插件

在VS Code的扩展图标里搜索c++,安装C/C++插件并重载VS Code

77f28a9f8d12ced65d0d4a3333b2e02.png

重启VS Code之后,打开一个含c/c++源码的文件夹,VS Code将会创建一个名为.vscode的子文件夹用于存放配置文件。

安装MinGW

去官网下载安装器,安装好后选择需要安装的工具:

47c97e09dea24c5828ed9729e535b3a.png

选择好之后应用更改:

831e6307e565c48c0d8d78a3617e331.png

然后修改系统的环境变量,将可执行文件目录加入到Path中:

ea840f777326e5bfb13efa5b0a620f4.png

智能提示

为实现代码补全,需要创建一个c_cpp_properties.json文件。新建一个hello.c,输入:

#include <stdio.h>
int main(void)
{
    printf("hello");
    getchar();
    return 0;
}

VS Code会提示找不到头文件,点击错误处的小灯泡,编辑包含路径

bc57ae2abd1b4d16a64c443a2507e87.png

在c_cpp_properties.json中修改Win32下的路径设置

"browse": {
    "path": [
        "${workspaceRoot}",
        "C:\\DevProgram\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++"
    ],
    "limitSymbolsToIncludedHeaders": true,
    "databaseFilename": ""
}

返回编辑器,点击小灯泡,将头文件所在目录添加到包含路径中

8155464b34294f44de34b1f87d01360.png

构建程序

构建程序需要创建一个tasks.json文件,点击任务->配置任务:

3120f134449835ff9501600e61a23cc.png

在弹出的选项中选择Others,然后修改tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build hello",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "hello.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

color: rgb(79, 79, 79); line-height: 32px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; white-space: normal; background-color: rgb(255, 255, 255);">调试代码

调试代码需要创建一个launch.json文件,点击调试栏的齿轮图标

27fa935c56b6b4032be29fb6cd366e2.png

修改launch.json

{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/a.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\DevProgram\\MinGW\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "build hello"
            }
        ]
    }

color: rgb(79, 79, 79); line-height: 32px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; white-space: normal; background-color: rgb(255, 255, 255);">调试运行

返回编辑器,设置断点,按F5开始调试

d7bf7f461f6a7b5e52975836465df2b.png

相关文章教程推荐:vscode教程

以上就是vscode如何编译运行c/c++程序的详细内容,更多请关注其它相关文章!