vscode 配置 c 开发环境(windows) - housekeeper-software/tech GitHub Wiki

安装一些插件

c/c++
c/c++ Extension Pack
我们默认使用Microsoft c++ compiler(cl)

新建工程

新建一个工程目录,打开vscode
新建一个c++文件

生成launch.json

点击调试图标,播放按钮下拉菜单选择'添加调试配置',生成launch.json

生成 c_cpp_properties.json

Ctrl+Shift+P,选择: C/C++:Edit configurations(JSON)

生成 tasks.json

菜单 Terminae->Configure Task,选择编译器,比如 cl,生成tasks.json

举例

加入我们要编译一个 libuv的测试程序,我们先将libuv编译成静态库,它的位置如下:
libuv: 
   include: E:\libuv-1.50.0\include
   lib : E:\libuv-1.50.0\Debug\libuv.lib

我们工程的目录是 d:\libuv_test

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": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\libuv_test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}\\build",
            "environment": [],
            "console": "integratedTerminal"
        }
    ],
    "logging": {
        "exceptions": true,
        "programOutput": true,
        "engineLogging": true,
        "trace": true
    }
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "E:\\libuv-1.50.0\\include"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}/**",
                    "E:\\libuv-1.50.0\\build\\Debug"
                ],
                "limitSymbolsToIncludedHeaders": false,
                "databaseFilename": ""
            },
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/C",
                    "\"D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat\"",
                    "&&"
                ]
            }
        },
    },
    "tasks": [
        {
            "label": "Build with MSVC",
            "type": "shell",
            "command": "cl",
            "args": [
                "/EHsc", // 启用 C++ 异常处理
                "/MDd", // 使用动态链接的多线程调试版本的 CRT
                "/Zi", // 生成调试信息,这个很重要,否则没法调试
                "/I", "E:\\libuv-1.50.0\\include", // 包含头文件目录
                "${workspaceFolder}/*.cc", // 编译当前工作区中的所有 cpp 文件
                "/link", // 链接阶段
                "/LIBPATH:E:\\libuv-1.50.0\\build\\Debug", // 指定 libuv.lib 的路径
                "libuv.lib", // 链接 libuv.lib
                "psapi.lib",
                "user32.lib",
                "advapi32.lib",
                "iphlpapi.lib",
                "userenv.lib",
                "ws2_32.lib",
                "dbghelp.lib",
                "ole32.lib",
                "shell32.lib",
                "/out:${workspaceFolder}\\build\\libuv_test.exe" // 输出可执行文件
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$msCompile"],
            "detail": "Build the project using MSVC x64 and link libuv.lib"
        }
    ]
}