用 VS code 的 C/C++ IntelliSense 插件开发大型项目,如何排除一些源码目录?

2021-12-25 15:56:59 +08:00
 kgdb00
大型项目建立索引时间太长且太消耗系统资源,这个插件有没有类似 ctags, gtags 根据文件列表来创建索引的功能?
3098 次点击
所在节点    程序员
16 条回复
hei1000
2021-12-25 16:49:30 +08:00
你没有先生成 compile_commands.json 文件?
kgdb00
2021-12-25 17:34:34 +08:00
@hei1000 感谢回复!
compile_commands.json 这个文件好像是 cmake 生成的,但我的项目不是 cmake 构建的,我是不是得找一个这个文件的模板然后手动生成这个文件,或先编写一个 CMakeLists.txt ,然后再用 cmake 生成这个文件?
skimplus
2021-12-25 17:44:50 +08:00
make 构建可以执行
bear -a make
kgdb00
2021-12-25 18:07:12 +08:00
@skimplus 感谢回复!

你说的是 https://github.com/rizsotto/Bear 这个工具吗?在 linux 平台上没有-a 参数。
skimplus
2021-12-25 18:16:18 +08:00
@kgdb00 我这边是支持的,这个参数是用来增量向 compile_commands.json 里输出的
--append, -a Extend existing compilation database with new entries. Duplicate entries are detected and not present in the final output. The output is not continuously updated, it's done when the build command finished. (default: False)
kgdb00
2021-12-25 18:17:13 +08:00
@skimplus
明白了,bear -- make 这样可以生成一个 compile_commands.json 文件
skimplus
2021-12-25 18:27:05 +08:00
@kgdb00
我记得我以前的工程是在 build 目录 bear -a make 就可以了
我把上面的命令绑定到 vscode build task 上了,一般修改一些就 build 一下
不加 -a 可能 compile_commands.json 里就被替换为 make 新编译的部分(修改的文件)
加上 -a (或者 --append )可以保证 compile_commands.json 里面的内容是完整的
kgdb00
2021-12-25 18:29:18 +08:00
@skimplus 不过使用 bear 工具生成的 compile_commands.json 文件也太大了吧?每一个源代码文件都有一大串 json 来列出所有的编译参数,而且这些编译参数还都是一样的,非常臃肿。

用就算用程序去去除不要包含的源文件也会比较麻烦,有没有生成简单一点的格式的方式?

ctags ,cscope ,gun global 这些工具都是可以根据一个最简单的文件路径列表(也就是 find 命令输出的格式)的方式来生成 tag 数据库。
kgdb00
2021-12-25 18:30:24 +08:00
{
"arguments": [
"/usr/lib64/ccache/gcc",
"-c",
"-Wp,-MD,./.builtin-stop.o.d",
"-Wp,-MT,builtin-stop.o",
"-DCONFIG_GUEST_INIT",
"-DCONFIG_GUEST_PRE_INIT",
"-DCONFIG_X86_64",
"-DCONFIG_X86",
"-D_FILE_OFFSET_BITS=64",
"-D_GNU_SOURCE",
"-DKVMTOOLS_VERSION=\"\"",
"-DBUILD_ARCH=\"x86\"",
"-Iinclude",
"-Ix86/include",
"-O2",
"-fno-strict-aliasing",
"-g",
"-Wall",
"-Wformat=2",
"-Winit-self",
"-Wmissing-declarations",
"-Wmissing-prototypes",
"-Wnested-externs",
"-Wno-system-headers",
"-Wold-style-definition",
"-Wredundant-decls",
"-Wsign-compare",
"-Wstrict-prototypes",
"-Wundef",
"-Wvolatile-register-var",
"-Wwrite-strings",
"-Wno-format-nonliteral",
"-Werror",
"-DCONFIG_HAS_ZLIB",
"-o",
"builtin-stop.o",
"builtin-stop.c"
],
"directory": "/a/source/kvmtool",
"file": "/a/source/kvmtool/builtin-stop.c",
"output": "/a/source/kvmtool/builtin-stop.o"
},
每个文件都是这么一大坨 json
skimplus
2021-12-25 18:37:10 +08:00
@kgdb00
现在这点文件大小根本没啥,compile_commands.json 实际使用起来比 ctag 好很多(快,准确)
我用的 bear 是直接用 apt install bear 安装的( bear 2.4.3@ubuntu 20.04 lts )
如果你用 cmake 的话直接把 CMAKE_EXPORT_COMPILE_COMMANDS 打开就行,不用 bear
kgdb00
2021-12-25 19:47:36 +08:00
@skimplus 再请教一下老哥,

我给 linux 内核源码树创建了 compile_commands.json 文件,只包含 1730 个源代码文件,c_cpp_properties.json 配置如下,但好像 compileCommands 没有生效,ms-vscode.cpptools-1.7.1/bin/cpptools 这个程序一直在生成 tag 文件,cpu 100%运行了一个小时,输出的文件都 2 个 GB 了,还没完成,是我哪里设置的不对吗?

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include/"
],
"defines": [],
"compilerPath": "/usr/lib64/ccache/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64",
"compileCommands": "${workspaceFolder}/compile_commands.json"
}
],
"version": 4
}
waruqi
2021-12-25 20:12:10 +08:00
用 xmake 就行了 配合 vscode 插件 自动生成 compile_commands.json 支持 intelligense ,还能支持项目构建 https://github.com/xmake-io/xmake-vscode/issues/40
hei1000
2021-12-25 20:28:58 +08:00
@kgdb00
```
# generate compile_commands.json file for C/C++ files used by ccls/lsp
if test -f CMakeLists.txt
cmake -H. -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
test -f build/compile_commands.json; and ln -nsfv build/compile_commands.json .
else if test -f meson.build
if test -d build
# compile_commands.json is only updated by meson compile(ninja) if no --reconfigure
meson build --reconfigure
else
meson build
end
test -f build/compile_commands.json; and ln -nsfv build/compile_commands.json .
else if test -f scripts/gen_compile_commands.py # available for Linux kernel v5+
make defconfig
if test $status = 0; and make
scripts/gen_compile_commands.py
end
else if test -f Makefile -o -f makefile # NOTE: some old linux kernel src contains the Makefile/makefile, this will fail
# NOTE: If inside old linux kernel, check https://github.com/gniuk/linux-compile-commands for more info
# make clean # without this line, the compile_commands.json generated may be empty

# NOTE: pip install scan-build to get intercept-build
if command -q intercept-build; and command intercept-build --append -v make
else if command -q compiledb; and command compiledb -f -v -n make
else if command -q bear; and command bear --append -- make
else
echo "None of scan-build/compiledb/bear is not installed or all failed to generate compile_commands.json!"
end
end
find . -name "compile_commands.json" -exec ls -lh {} +
end
```

这是我生成 compile_commands.json 文件的一个 fish shell 函数,make/cmake/meson/linuxkernel 都包括了
hei1000
2021-12-25 20:31:21 +08:00
垃圾排版,把我的缩进都删了
lingxi27
2021-12-25 21:13:32 +08:00
clion+cmake 你值得拥有
Giki
2021-12-26 01:14:27 +08:00
我用 compile_commands.json + clangd 插件,体感比 IntelliSense 顺滑

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824400

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX