V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kgdb00
V2EX  ›  程序员

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

  •  
  •   kgdb00 · 2021-12-25 15:56:59 +08:00 · 3045 次点击
    这是一个创建于 824 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大型项目建立索引时间太长且太消耗系统资源,这个插件有没有类似 ctags, gtags 根据文件列表来创建索引的功能?
    第 1 条附言  ·  2021-12-26 19:18:53 +08:00
    我才发现 vs code 还有个插件叫做“C++ Intellisense”,我标题说的不准确,我问的是微软官方开发的那个“C/C++
    ”插件。
    16 条回复    2021-12-26 01:14:27 +08:00
    hei1000
        1
    hei1000  
       2021-12-25 16:49:30 +08:00
    你没有先生成 compile_commands.json 文件?
    kgdb00
        2
    kgdb00  
    OP
       2021-12-25 17:34:34 +08:00
    @hei1000 感谢回复!
    compile_commands.json 这个文件好像是 cmake 生成的,但我的项目不是 cmake 构建的,我是不是得找一个这个文件的模板然后手动生成这个文件,或先编写一个 CMakeLists.txt ,然后再用 cmake 生成这个文件?
    skimplus
        3
    skimplus  
       2021-12-25 17:44:50 +08:00
    make 构建可以执行
    bear -a make
    kgdb00
        4
    kgdb00  
    OP
       2021-12-25 18:07:12 +08:00
    @skimplus 感谢回复!

    你说的是 https://github.com/rizsotto/Bear 这个工具吗?在 linux 平台上没有-a 参数。
    skimplus
        5
    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
        6
    kgdb00  
    OP
       2021-12-25 18:17:13 +08:00
    @skimplus
    明白了,bear -- make 这样可以生成一个 compile_commands.json 文件
    skimplus
        7
    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
        8
    kgdb00  
    OP
       2021-12-25 18:29:18 +08:00
    @skimplus 不过使用 bear 工具生成的 compile_commands.json 文件也太大了吧?每一个源代码文件都有一大串 json 来列出所有的编译参数,而且这些编译参数还都是一样的,非常臃肿。

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

    ctags ,cscope ,gun global 这些工具都是可以根据一个最简单的文件路径列表(也就是 find 命令输出的格式)的方式来生成 tag 数据库。
    kgdb00
        9
    kgdb00  
    OP
       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
        10
    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
        11
    kgdb00  
    OP
       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
        12
    waruqi  
       2021-12-25 20:12:10 +08:00 via Android
    用 xmake 就行了 配合 vscode 插件 自动生成 compile_commands.json 支持 intelligense ,还能支持项目构建 https://github.com/xmake-io/xmake-vscode/issues/40
    hei1000
        13
    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
        14
    hei1000  
       2021-12-25 20:31:21 +08:00
    垃圾排版,把我的缩进都删了
    lingxi27
        15
    lingxi27  
       2021-12-25 21:13:32 +08:00
    clion+cmake 你值得拥有
    Giki
        16
    Giki  
       2021-12-26 01:14:27 +08:00
    我用 compile_commands.json + clangd 插件,体感比 IntelliSense 顺滑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5698 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:48 · PVG 09:48 · LAX 18:48 · JFK 21:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.