系统环境
Mac 上可能已经安装了 Clang。要验证是否已安装,请打开 macOS 终端窗口并输入以下命令:
clang --version如没有安装,请使用以下命令安装 Clang,也可以直接安装 XCode。
xcode-select --installVSCode
安装完 VSCode 后,还需要安装以下两个插件:
- C++ extension for VS Code
- CMake Tools
其中 C++ extension for VS Code 会自动安装另外两个依赖的 C++ 插件。
librime
按下面命令下载 librime 的代码:
brew install cmake ninjagit clone git@github.com:rime/librime.gitcd librimegit submodule update --init# 使用 VSCode 打开当前项目code .提示
- 如果运行
code .出错,请在 VSCode 中CMD+Shift+P,输入code回车安装。- 如您本地没有安装 brew,请参考 https://brew.sh/ 安装。
librime 依赖库
# 下载 boostsbash install-boost.shexport BOOST_ROOT="$(pwd)/deps/boost-1.84.0"
# 编译其他依赖make deps
# 生成 debug bin 文件make debug
( cd debug/bin; echo "congmingdeRime{space}shurufa" | ./rime_api_console)VSCode 调试环境配置
打开命令面板:CMD+Shift+P,键入tasks:configure task(不用输入完,看到匹配选项后即可), 回车,选择 Create task.json from template,在选择 Other。
当然你也可以直接新建 .vscode 文件夹,然后在此文件夹中,新建一个 tasks.json 文件。
继续打开命令面板:CMD+Shift+P,键入debug: add configuration(不用输入完,看到匹配选项即可),回车,选择 C++...。
当然你也可以在 .vscode 文件夹,新建一个 launch.json 文件。
下面我们调整下 tasks.json 与 launch.json 文件,使我们可以在 VSCode 断点调试 librime 的代码。
-
task.json: 用来配置一个项目中的各种任务。我们知道,每个项目在开发过程中需要编译,测试,调试等任务,你就可以通过此文件配置这些任务, 配置好后,就可以在命令面板中直接执行任务,而不需要在终端或者鼠标点击执行了。目前我们需要配置下 librime 项目中生成 debug bin 文件的任务。内容如下:
{"version": "2.0.0","options": {"env": {"BOOST_ROOT": "/Users/morse/Documents/github.com/imfuxiao/librime/deps/boost-1.84.0",}},"tasks": [{"label": "make debug","type": "shell","command": "make debug",}],}- options 下的 env: 为在 shell 中执行 make debug 命令注入 BOOST_ROOT 环境变量,不添加会报错,因为找不到依赖库 boost 的头文件。
- label: 为每个 task 起一个唯一的名字,这里的名称随意起,注意:后面
launch.json中会调用此名称。 - type: shell,因为我们是在命令行执行
- command: make debug,是 librime 已经写好的编译 debug 文件的命令,在项目的
Makefile中。
-
launch.json: 用来设置 debug 配置信息的,无论是什么语言在 VSCode 中调试,都需要通过这个文件配置 debug 信息。 这里我们需要调试 task 中已经编译好的rime_console_api,配置如下:{"version": "0.2.0","configurations": [{"name": "rime_api_console","type": "cppdbg","request": "launch","program": "${workspaceRoot}/debug/bin/rime_api_console","args": [],"stopAtEntry": false,"cwd": "${workspaceRoot}","environment": [],"externalConsole": true,"MIMode": "lldb","preLaunchTask": "make debug"}]}我们在
configuration中可以配置多个 debug 目标,这里只配置的rime_api_console。- “preLaunchTask”: “make debug”
此选项重要,因为我们的 debug 的 bin 文件 rime_api_console 是通过
make debug生成的,所以我们需要每次修改代码后,调试前,重新编译 bin 文件。 这里配置的是tasks.json中对应 task 中label的内容。 - “program”: ”${workspaceFolder}/debug/bin/rime_api_console”
此选项重要,对应需要 debug 的目标文件,我们这里配置的是已经编译好的
rime_api_console文件。
- “preLaunchTask”: “make debug”
此选项重要,因为我们的 debug 的 bin 文件 rime_api_console 是通过
-
settings.json: 当前项目的 VSCode 配置信息。因为 librime 使用 CMake 管理项目依赖,这里可以配置一些 CMake 编译参数,如boost依赖路径。{"cmake.configureArgs": ["-DBOOST_ROOT=/Users/morse/Documents/github.com/imfuxiao/librime/deps/boost-1.84.0",],}