打算开发个Android内核模块玩一玩,奈何我的Pixel3的内核源码和环境全配在服务器上了,只好捣鼓一下远程开发,最后选择了VSCode。

VSCode插件下载

  1. clangd
  2. Remote Development

compile_commands.json文件生成

compile_commands.json 是一个 JSON 格式的文件,用来描述一个 C/C++ 项目中所有源文件的编译信息。这个文件通常由构建系统生成(如 CMake、Bear),并被许多代码编辑器和IDE用来提供更准确的代码补全、语法检查、错误检测等功能。

compile_commands.json的生成方式根据构建系统而异,自行根据所用构建系统搜索相关方法。

有一个哥们写了一个生成的脚本,可以用它来一键生成。

vscode-linux-kernel

1
2
3
4
5
6
7
# 首先确保内核源码经过一次完整的构建
make defconfig
make
# 进行相关配置文件的生成
# 可以不在.vscode下,自行复制输出的配置文件到.vscode下即可
git clone git@github.com:amezin/vscode-linux-kernel.git .vscode
python .vscode/generate_compdb.py -O /你的编译输出文件路径

VSCode的配置

上文提到了通过脚本来生成配置文件,可以直接用它生成的配置。

针对settings.json可以进行如下配置(clangd等路径信息改为自己的,我这里直接用x86_64的clangd了,反正编译是通过构建脚本的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
"files.associations": {
"iostream": "cpp",
"intrinsics.h": "c",
"ostream": "cpp",
"vector": "cpp",
"*.h": "c"
},
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"C_Cpp.errorSquiggles": "disabled",
"C_Cpp.intelliSenseEngineFallback": "disabled",
"C_Cpp.intelliSenseEngine": "disabled",
"C_Cpp.autocomplete": "disabled",
"clangd.path": "/usr/bin/clangd",
"clangd.arguments": [
"--compile-commands-dir=${workspaceFolder}/",
"--log=verbose",
"--pretty",
"--all-scopes-completion",
"--completion-style=bundled",
"--cross-file-rename",
"--header-insertion=iwyu",
"--header-insertion-decorators",
"--background-index",
"--clang-tidy",
"--clang-tidy-checks=cppcoreguidelines-*,performance-*,bugprone-*,portability-*,modernize-*,google-*",
"--fallback-style=file",
"-j=2",
"--pch-storage=disk",
"--function-arg-placeholders=false"
],
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"files.exclude": {
"**/.*.*.cmd": true,
"**/.*.d": true,
"**/.*.S": true
},
"[c]": {
"editor.detectIndentation": false,
"editor.tabSize": 8,
"editor.insertSpaces": false,
"editor.rulers": [
80,
100
]
}
}

由于不知道哪里没配置对,我就在c_cpp_properties.json内又配置了一遍includePath:

需要注意的是,我是直接拉取了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"configurations": [
{
"name": "linux-gcc-x64",
"cStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compileCommands": "${workspaceFolder}/compile_commands.json",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/kernel/**",
"${workspaceFolder}/kernel/google/bluecross/**",
"${workspaceFolder}/kernel/google/bluecross/drivers",
"${workspaceFolder}/kernel/google/bluecross/kernel",
"${workspaceFolder}/kernel/google/bluecross/include/**",
"${workspaceFolder}/kernel/google/bluecross/arch/arm64/include/**",
],
"cppStandard": "${default}",
"compilerPath": "/usr/bin/gcc"
}
],
"version": 4
}

参考文章: