项目结构

.
├── .vscode
│ ├── launch.json
│ └── tasks.json
├── Main
│ ├── BUILD.bazel
│ └── hello.cc
├── .bazelrc
└── MODULE.bazel

首先完成包含c++代码的Main目录

Main/BUILD.bazel
cc_binary(
    name = "c++02",
    srcs = ["hello.cc"]
)

这里不像上一篇文章的MinGW,MSVC生成的目标文件时会加上后缀.exe,所以这里起名就不用有.exe了

Main/hello.cc
#include <iostream>

int main(int argc, char const *argv[])
{
    system("chcp 936 > nul");  // 设置控制台编码为GBK
    int a = 0;
    std::cout << "a = " << a << std::endl;
    std::cout << "你好世界" << std::endl;
    return 0;   
}


需要注意MSVC默认采用的字符集是GBK,因此我们文件必须被保存为GBK编码

另外为了最终显示在控制台上不乱码,使用了chcp修改控制台为GBK编码

再来进行Bazel模块设置

.bazelrc
# 通用配置:启用新版工具链解析
build --incompatible_enable_cc_toolchain_resolution

# MSVC 配置
build:msvc --copt=/std:c++17
build:msvc --compilation_mode=opt

这里由于我们环境变量设置c++优先为MSVC,所以这里配置比较简单

MODULE.bazel
module(
    name = "cc2604_02",
    version = "1.0.0",
)

# 声明对 C++ 规则集的依赖
bazel_dep(name = "rules_cc", version = "0.0.10")
bazel_dep(name = "platforms", version = "0.0.10")

到这里其实已经可以运行了

PS F:\Bazel Project\02_C to C++02(MSVC)> bazel run //main:c++02
WARNING: Build option --compilation_mode has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: Analyzed target //Main:c++02 (1 packages loaded, 458 targets configured).
INFO: From Compiling Main/hello.cc:
hello.cc
INFO: Found 1 target...
Target //Main:c++02 up-to-date:
  bazel-bin/Main/c++02.exe
INFO: Elapsed time: 9.443s, Critical Path: 5.20s
INFO: 6 processes: 4 internal, 2 local.
INFO: Build completed successfully, 6 total actions
INFO: Running command line: bazel-bin/Main/c++02.exe
a = 0
你好世界
PS F:\Bazel Project\02_C to C++02(MSVC)> 

第一次运行时,Bazel会下载依赖。电脑需要开启vpn才能成功

再来进行vscode任务和调试的设置

以下这2个文件可以在vscode中通过手动创建文件夹.vscode再创建文件生成。也可以使用ctrl+shift+p快捷生成

.vscode/launch.json
 
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Bazel Debug (MSVC)",
            "type": "cppvsdbg",  // 改为Visual Studio Windows Debugger
            "request": "launch",
            "program": "${workspaceFolder}/bazel-bin/Main/c++02.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "${env:PATH};C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64"
                }
            ],
            "externalConsole": true,  // MSVC调试建议使用外部控制台
            "console": "externalTerminal",
            "visualizerFile": "${workspaceFolder}/debug.natvis",  // 可选的调试可视化文件
            "preLaunchTask": "bazel-build-debug-msvc",  // 改为对应的任务名
            "logging": {
                "moduleLoad": false,
                "trace": false
            }
        },
        {
            "name": "Bazel Run (MSVC)",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bazel-bin/Main/c++02.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "console": "externalTerminal"
        }
    ]
}
.vscode/tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bazel-build-debug-msvc",
            "type": "shell",
            "command": "bazel",
            "args": [
                "build",
                "--compilation_mode=dbg",
                "--copt=/MTd",  // MSVC调试运行时库
                "--copt=/Zi",   // 调试信息
                "--copt=/DEBUG", // 生成PDB文件
                "//main:c++02"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "bazel-run-msvc",
            "type": "shell",
            "command": "bazel",
            "args": [
                "run",
                "--compilation_mode=dbg",
                "//main:c++02"
            ],
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": []
        },
        {
            "label": "bazel-build-release-msvc",
            "type": "shell",
            "command": "bazel",
            "args": [
                "build",
                "--compilation_mode=opt",
                "--copt=/MT",  // MSVC发布运行时库
                "--copt=/O2",   // 优化
                "//main:c++02"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": ["$msCompile"]
        },
        {
            "label": "bazel-run-release-msvc",
            "type": "shell",
            "command": "bazel",
            "args": [
                "run",
                "--compilation_mode=opt",
                "//main:c++02"
            ],
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": []
        },
        {
            "label": "bazel-clean",
            "type": "shell",
            "command": "bazel",
            "args": [
                "clean"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": []
        },
        {
            "label": "bazel-query-all",
            "type": "shell",
            "command": "bazel",
            "args": [
                "query",
                "//..."
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "problemMatcher": []
        }
    ]
}

配置了这2个文件后我们可以调试代码和使用任务运行程序了

调试

打下断点例如这里是在int a = 0打的断点,然后Fn+F5进入调试

运行任务

这里我给Tasks:Run Task设置了快捷键 Ctrl+Shift+T

更多推荐