|
2 | 2 | title: 打造高效 Neovim C/C++ 开发环境:从零到一的完整指南 |
3 | 3 | date: 2024-10-17 10:00:00 +0800 |
4 | 4 | tags: [Neovim, C++, 开发环境, LSP] |
5 | | -excerpt: "详细讲解如何从零开始配置 Neovim,打造媲美 IDE 的 C/C++ 开发体验,包括 LSP、自动补全、调试、格式化等核心功能。" |
| 5 | +excerpt: "用 clangd + Treesitter 打造 IDE 级体验,覆盖补全/诊断/调试/格式化/构建;修正 GDB/Lldb 适配,提供 Windows 与 CMake 指南。" |
| 6 | +layout: post |
| 7 | +comments: true |
6 | 8 | --- |
7 | 9 |
|
8 | 10 | 作为一个追求效率的开发者,我一直在寻找一个轻量、快速、可高度定制的编辑器。在尝试了各种 IDE 和编辑器后,我最终选择了 Neovim,并成功打造了一套完整的 C/C++ 开发环境。这篇文章将分享我的配置经验,帮你快速上手。 |
@@ -445,6 +447,7 @@ return { |
445 | 447 | "rcarriga/nvim-dap-ui", |
446 | 448 | "nvim-neotest/nvim-nio", |
447 | 449 | "theHamsta/nvim-dap-virtual-text", |
| 450 | + -- 如使用 LLDB:需要系统安装 lldb;如偏好 cpptools(Windows 友好),见下方"cppdbg" |
448 | 451 | }, |
449 | 452 | keys = { |
450 | 453 | { "<F5>", function() require("dap").continue() end, desc = "Debug: Continue" }, |
@@ -473,27 +476,49 @@ return { |
473 | 476 | dapui.close() |
474 | 477 | end |
475 | 478 |
|
476 | | - -- C/C++ 调试配置 (GDB) |
477 | | - dap.adapters.cppdbg = { |
478 | | - id = "cppdbg", |
| 479 | + -- 方案 A:LLDB(Linux/macOS 原生良好) |
| 480 | + dap.adapters.lldb = { |
479 | 481 | type = "executable", |
480 | | - command = "gdb", |
481 | | - args = { "-i", "dap" }, |
| 482 | + command = "lldb-vscode", -- 一般随 lldb 安装 |
| 483 | + name = "lldb", |
482 | 484 | } |
483 | | - |
484 | 485 | dap.configurations.cpp = { |
485 | 486 | { |
486 | | - name = "Launch file", |
487 | | - type = "cppdbg", |
| 487 | + name = "Debug (LLDB)", |
| 488 | + type = "lldb", |
488 | 489 | request = "launch", |
489 | 490 | program = function() |
490 | 491 | return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file") |
491 | 492 | end, |
492 | 493 | cwd = "${workspaceFolder}", |
493 | | - stopAtEntry = false, |
| 494 | + stopOnEntry = false, |
| 495 | + args = {}, |
494 | 496 | }, |
495 | 497 | } |
496 | 498 | dap.configurations.c = dap.configurations.cpp |
| 499 | + |
| 500 | + -- 方案 B:cppdbg(VS Code cpptools 适配,Windows/跨平台) |
| 501 | + -- 安装 Microsoft 'cpptools',获取 OpenDebugAD7 可执行文件路径并替换下面的 command |
| 502 | + -- local cpptools = "/path/to/OpenDebugAD7" |
| 503 | + -- dap.adapters.cppdbg = { |
| 504 | + -- id = "cppdbg", |
| 505 | + -- type = "executable", |
| 506 | + -- command = cpptools, |
| 507 | + -- options = { detached = false }, |
| 508 | + -- } |
| 509 | + -- table.insert(dap.configurations.cpp, { |
| 510 | + -- name = "Debug (cppdbg)", |
| 511 | + -- type = "cppdbg", |
| 512 | + -- request = "launch", |
| 513 | + -- program = function() |
| 514 | + -- return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file") |
| 515 | + -- end, |
| 516 | + -- cwd = "${workspaceFolder}", |
| 517 | + -- MIMode = "gdb", -- 或 "lldb" |
| 518 | + -- setupCommands = { |
| 519 | + -- { text = "-enable-pretty-printing", description = "pretty print", ignoreFailures = true }, |
| 520 | + -- }, |
| 521 | + -- }) |
497 | 522 | end, |
498 | 523 | } |
499 | 524 | ``` |
@@ -838,3 +863,66 @@ git clone https://github.com/magic-alt/nvim-cpp-ide.git ~/.config/nvim |
838 | 863 | 相比传统 IDE,Neovim 配置更灵活、启动更快、资源占用更低。虽然初期需要投入时间学习,但一旦掌握,你将拥有一个完全按照自己习惯定制的开发环境。 |
839 | 864 |
|
840 | 865 | 如果你在配置过程中遇到问题,欢迎在评论区交流,或者参考 [Neovim 官方文档](https://neovim.io/doc/) 和社区资源。Happy Coding! 🚀 |
| 866 | + |
| 867 | +--- |
| 868 | + |
| 869 | +## Windows 专项提示 |
| 870 | + |
| 871 | +1. **工具链**:首选 LLVM/Clang + lldb(choco/scoop 可得),或 MSVC + cpptools(cppdbg)。 |
| 872 | +2. **编译数据库**:CMake 生成的 `build/compile_commands.json` 在 Windows 可用 `mklink` 建软链接: |
| 873 | + ```bat |
| 874 | + mklink compile_commands.json .\build\compile_commands.json |
| 875 | + ``` |
| 876 | +3. **路径与编码**:确保 `shell` 使用 UTF-8,PowerShell 里:`[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()`。 |
| 877 | + |
| 878 | +## CMake Presets 与构建一键化 |
| 879 | + |
| 880 | +在项目根加入 `CMakePresets.json`(示例): |
| 881 | + |
| 882 | +```json |
| 883 | +{ |
| 884 | + "version": 3, |
| 885 | + "cmakeMinimumRequired": { "major": 3, "minor": 20 }, |
| 886 | + "configurePresets": [ |
| 887 | + { |
| 888 | + "name": "dev", |
| 889 | + "generator": "Ninja", |
| 890 | + "binaryDir": "${sourceDir}/build", |
| 891 | + "cacheVariables": { |
| 892 | + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", |
| 893 | + "CMAKE_BUILD_TYPE": "Debug" |
| 894 | + } |
| 895 | + } |
| 896 | + ], |
| 897 | + "buildPresets": [{ "name": "dev", "configurePreset": "dev" }] |
| 898 | +} |
| 899 | +``` |
| 900 | + |
| 901 | +Neovim 内可配合 `Civitasv/cmake-tools.nvim`: |
| 902 | +`<leader>cg` 配置、`<leader>cb` 构建、`<leader>cr` 运行。 |
| 903 | + |
| 904 | +## clangd 体验升级参数 |
| 905 | + |
| 906 | +在 `lspconfig.clangd.setup` 中建议启用: |
| 907 | + |
| 908 | +```lua |
| 909 | +cmd = { |
| 910 | + "clangd", |
| 911 | + "--background-index", |
| 912 | + "--clang-tidy", |
| 913 | + "--header-insertion=never", -- 避免自动插入不需要的头 |
| 914 | + "--completion-style=detailed", |
| 915 | + "--function-arg-placeholders", |
| 916 | + "--fallback-style=llvm", |
| 917 | +} |
| 918 | +``` |
| 919 | + |
| 920 | +## 常见故障排查(速查表) |
| 921 | + |
| 922 | +| 现象 | 排查点 | 处理 | |
| 923 | +|---|---|---| |
| 924 | +| 补全失效 | `:LspInfo` 看 clangd 是否 attached | 检查 `compile_commands.json`、根目录是否一致 | |
| 925 | +| 跳转慢 | 后台索引未完成 | 等待 `clangd` 后台索引;排除巨型目录 | |
| 926 | +| 调试断点不生效 | 可执行无符号表 | 使用 `-g` 或 `-DCMAKE_BUILD_TYPE=Debug` 重新构建 | |
| 927 | +| 终端中文乱码 | 终端编码/字体 | 统一 UTF-8,选择等宽字体(JetBrains Mono) | |
| 928 | +| Mason 安装失败 | 网络问题/权限不足 | 使用代理或手动下载 LSP 二进制文件 | |
0 commit comments