C语言VS Code 开发环境搭建


由于之前使用VS Code较少,缺少在VS Code上开发C程序的经验。本篇博文主要记录使用VS Code开发C程序时,环境搭建步骤。

官方文档

主要看下列两个文档即可,建议仔细阅读,二者结合起来看

  1. Mac版VS Code配置C/C++
  2. 关于用户指南中Debug的部分

安装拓展

在左侧Extensions中搜索C/C++,安装即可

生成c_cpp_properties.json

  1. Command + Shift + P,C/C++ : Edit Configuration(UI),Mac下一般使用默认配置即可.在这里插入图片描述
    点击配置页面的c_cpp_properties.json,会在当前文件下生成一个名为.vscode的文件夹,下面会有一个c_cpp_properties.json文件
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4
}

生成tasks.json

tasks.json里的配置是编译器编译、构建相关的。想要更详细的了解task,可参考官方文档

  1. 在C程序源文件,右侧点击Run C/C++ File,选择C/C++: clang在这里插入图片描述
    之后会在.vscode下生成一个名为task.json的文件。官方文档说明
    示例文件
{
    "tasks": [
        {
            "type": "cppbuild",
            // 与launch.json中的preLaunchTask对应,这里要注意,默认生成出的label中带有中文,我这里修改为全英文
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

注意: 这里args里面-g后面的参数${file}只是编译单文件的,如果要编译多个c文件怎么办呢?

两种方式:

  • 用Makefile管理,关于Makefile的写法可参考陈皓写的跟我一起写Makefile
  • 把-g后面的内容换出多个c文件的路径,例如
	"-g",
	"${workspaceFolder}/helloWorld.c",
	"${workspaceFolder}/struct_learn/book.c",
	"${workspaceFolder}/input_output_learn/read_user_input.c",

官方文档中说是支持通配符的,也就是用*.c代替,Windows版本是没问题的,但是在Mac上似乎不行,GitHub上关于该问题最近的一个issue-Update 1.19.4 breaks wildcard operator ‘*’ in tasks.json code will not compile

生成launch.json

launch.json里的配置debug相关的。

  1. 按照官方文档,并没有自动创建launch.json,于是手动创建在这里插入图片描述
    如果不显示C++(GDB/LLDB),那就去C程序源文件,右侧点击Debug C/C++ File,然后再进行上述操作,即可看到C++(GDB/LLDB).点击之后,发现会在.vscode文件夹下创建一个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
    // 所有属性:https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
    "version": "0.2.0",
    "configurations": [
      {
        "name": "C/C++: clang++ build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        // 使用外部终端
        "externalConsole": true,
        "MIMode": "lldb",
        // 和tasks.json中的label对应
        "preLaunchTask": "C/C++: clang++ build active file"
      }
    ]
  }

这里只说明两点,就是上述文件中带注释的部分,

  • debug时如果需要用户输入,则开启外部终端.对应官方文档在这里插入图片描述
  • 要配置preLaunchTask,名字和tasks.json中lable对应,这点也好理解,在debug之前是需要编译的,编译的任务是由task.json完成的,所以这里要和task.json对于对应在这里插入图片描述launch.json的所有属性详解见官方文档User Guide > Debugging部分

测试Debug

在程序中打断点,并在main函数所在的c文件中使用Debug模式启动(Windows下快捷键是F5),发现打开了Mac自动自带的终端,在debug模式下可以在终端输入,可以正常debug程序。

如何让程序debug完不退出?

Stackoverflow上的问题:How to configure VS Code so it won’t close console after program run
在网上找了些答案,基本是以下几种

  • 使用pause
  • 在结束之前搞一个while(true)循环

都不是我想要的,最终发现了评论里的一句话,

Place a breakpoint at the end of main

哈哈哈,就是这个方法最简单,也就是这样在这里插入图片描述
断点打在结尾的}处

Windows版本的配置

  1. 对于Windows需要先安装MinGW, 详细步骤见VsCode 文档 > C++ > GCC on Windows > Installing the MinGW-w64 toolchain, 确保按照文档中的步骤进行并验证
gcc --version
g++ --version
gdb --version
  1. 对于C/C++拓展我的配置如图,在这里插入图片描述

这里我是修改过的,默认不是这样

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "D:/MinGW/ucrt64/bin/gcc.exe",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x86"
        }
    ],
    "version": 4
}
  1. tasks.json配置
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "D:\\MinGW\\ucrt64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
  1. launch.json配置
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            // 模式是GDB
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\MinGW\\ucrt64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file",
        }
    ]
}

GDB和LLDB的区别

So, I would use LLDB while using Clang, use GDB while using GCC compiler as the good combination or pair because LLDB is based on LLVM, whereas GDB is the GNU debugger

反编译Object文件

使用objdump命令,可以反汇编.o文件

objdump -d xxx.o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值