Skip to content

IntelliSense misses gcc-style precompiled headers with compile_commands.json #14871

Open

Description

@nitz

Environment

  • OS and Version:
    Windows 10 19045.7807 22H2

  • VS Code Version:
    1.146.0

  • C/C++ Extension Version:
    1.35.6

  • If using SSH remote, specify OS of remote machine:
    Debian 13.7 arm64

Bug Summary and Steps to Reproduce

Bug Summary:

When providing a compile_commands.json to vscode-cpptools, IntelliSense can miss including precompiled headers depending on how they're provided.

Steps to reproduce:

Assuming a setup such as this:

├── build
│   ├── compile_commands.json
│   └── proj.p
│       ├── cpp_pch.h.gch
└── src
    ├── main.cpp
    └── pch
        └── cpp_pch.h

My build first creates a precompiled header from src/pch/cpp_pch.h, outputting it as build/proj.p/cpp_pch.h.gch. Then, when compiling main, I include it by specifying the proj.p directory as an include directory, and including it with -include cpp_pch.h. Since G++ will automatically look for <pch name>.gch, it finds and uses the already generated one. As src/pch/ is not on the include path, it never tries to include src/pch/cpp_pch.h

This produces a compile_commands.json like so:

[
  {
    "directory": "/home/user/proj/build",
    "command": "g++ -Iproj.p -I. -I.. -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -x c++-header -MD -MQ proj.p/cpp_pch.h.gch -MF proj.p/cpp_pch.h.gch.d -o proj.p/cpp_pch.h.gch -c ../src/pch/cpp_pch.h",
    "file": "../src/pch/cpp_pch.h",
    "output": "proj.p/cpp_pch.h.gch"
  },
  {
    "directory": "/home/user/proj/build",
    "command": "g++ -Iproj.p -I. -I.. -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -fpch-preprocess -include cpp_pch.h -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -MD -MQ proj.p/src_main.cpp.o -MF proj.p/src_main.cpp.o.d -o proj.p/src_main.cpp.o -c ../src/main.cpp",
    "file": "../src/main.cpp",
    "output": "proj.p/src_main.cpp.o"
  }
]

This works fine for compiling and linking, but IntelliSense does not properly notice that the file used as input to generate the cpp_pch.h.gch is what is actually being included via the -include cpp_pch.h. Things that are used in main.cpp get red problem squiggles, because IntelliSense doesn't know anything about what's in cpp_pch.h

Expected behavior:

Ideally vscode-cpp would notice the compile command that generates a *.gch and include the header referenced as an include for sources, especially when the IntelliSense mode is *-gcc-*

Configuration and Logs

## C/C++: Log Diagnostics


-------- Diagnostics - 13/11/2026, 15:26:57
Version: 1.35.6
Current Configuration:
{
    "name": "Linux",
    "includePath": [
        "/home/user/proj/**"
    ],
    "defines": [],
    "cStandard": "gnu17",
    "cppStandard": "gnu++17",
    "intelliSenseMode": "linux-gcc-arm64",
    "compileCommands": [
        "/home/user/proj/build/compile_commands.json"
    ],
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "mergeConfigurations": false,
    "recursiveIncludes": {},
    "recursiveIncludesPriorityIsExplicit": false,
    "recursiveIncludesOrderIsExplicit": false,
    "compilerPathIsExplicit": false,
    "configurationProvider": "mesonbuild.mesonbuild",
    "browse": {
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.default.compileCommands": "/home/user/proj/build/compile_commands.json",
    "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
}
Additional Tracked Settings:
{
    "editorTabSize": 4,
    "editorInsertSpaces": false,
    "editorAutoClosingBrackets": "languageDefined",
    "filesEncoding": "utf8",
    "filesAssociations": {},
    "filesExclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/.jj": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true
    },
    "filesAutoSaveAfterDelay": false,
    "editorInlayHintsEnabled": true,
    "editorParameterHintsEnabled": true,
    "searchExclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true
    },
    "workbenchSettingsEditor": "ui"
}
Custom browse configuration: 
{
    "browsePath": [
        ""
    ],
    "compilerPath": "g++",
    "compilerArgs": []
}
cpptools version (native): 1.35.5.0
Current database path: /home/user/.cache/vscode-cpptools/d6c28b94876ca376959235a45cf94837/.browse.VC.db
Translation Unit Mappings:
[ /home/user/proj/src/main.cpp - source TU]:
[ /home/user/proj/src/pch/cpp_pch.h - source TU]:
Translation Unit Configurations:
[ /home/user/proj/src/main.cpp ]
    Process ID: 3242704
    Memory Usage: 22 MB
    Compiler Path: /usr/bin/g++
    Language: C++
    Include Paths:
        include: /home/user/proj/build/proj.p
        include: /home/user/proj/build
        include: /home/user/proj
        system include: /usr/include/c++/14
        system include: /usr/include/aarch64-linux-gnu/c++/14
        system include: /usr/include/c++/14/backward
        system include: /usr/lib/gcc/aarch64-linux-gnu/14/include
        system include: /usr/local/include
        system include: /usr/include/aarch64-linux-gnu
        system include: /usr/include
    Defines:
        _GLIBCXX_ASSERTIONS=1
        _FILE_OFFSET_BITS=64
    Standard Version: c++17
    IntelliSense Mode: linux-gcc-arm64
    Other Flags:
        --g++
        --gnu_version=140200
    compile_commands.json entry:
        directory: /home/user/proj/build
        file: /home/user/proj/src/main.cpp
        command: g++ -Iproj.p -I. -I.. -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -fpch-preprocess -include cpp_pch.h -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -MD -MQ proj.p/src_main.cpp.o -MF proj.p/src_main.cpp.o.d -o proj.p/src_main.cpp.o -c ../src/main.cpp
        output: proj.p/src_main.cpp.o
[ /home/user/proj/src/pch/cpp_pch.h ]
    Process ID: 3242926
    Memory Usage: 21 MB
    Compiler Path: /usr/bin/g++
    Language: C++
    Include Paths:
        include: /home/user/proj/build/proj.p
        include: /home/user/proj/build
        include: /home/user/proj
        system include: /usr/include/c++/14
        system include: /usr/include/aarch64-linux-gnu/c++/14
        system include: /usr/include/c++/14/backward
        system include: /usr/lib/gcc/aarch64-linux-gnu/14/include
        system include: /usr/local/include
        system include: /usr/include/aarch64-linux-gnu
        system include: /usr/include
    Defines:
        _GLIBCXX_ASSERTIONS=1
        _FILE_OFFSET_BITS=64
    Standard Version: c++17
    IntelliSense Mode: linux-gcc-arm64
    Other Flags:
        --g++
        --gnu_version=140200
    compile_commands.json entry:
        directory: /home/user/proj/build
        file: /home/user/proj/src/pch/cpp_pch.h
        command: g++ -Iproj.p -I. -I.. -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -x c++-header -MD -MQ proj.p/cpp_pch.h.gch -MF proj.p/cpp_pch.h.gch.d -o proj.p/cpp_pch.h.gch -c ../src/pch/cpp_pch.h
        output: proj.p/cpp_pch.h.gch
Total Memory Usage: 43 MB
Browse Paths from compile_commands.json, from workspace folder: /home/user/proj
    /home/user/proj
    /usr/include
    /usr/lib/gcc/aarch64-linux-gnu/14/include
    /usr/local/include

Other Extensions

Additional context

I'm using Meson as a build system which does exactly this for how it handles PCHs, but the issue could happen with any build system, or even just providing the compile commands manually.

Related topics I found while figuring this out, as I had assumed it was a meson (or their extension) thing before realizing the PCH info was actually making it into compile_commands.json: mesonbuild/meson#10590, mesonbuild/vscode-meson#238

Activity

  1. nitz commented on Dec 1, 2026

    @nitz
    Author

    In my attempt to work around this issue, I've tried a few different things now.

    The most direct approach for me seemed to be to make use of the forcedInclude feature to have the real pch header get picked up by IntelliSense. I first tried modifying vscode-meson's configuration provider to add a forcedInclude for the PCH path. That didn't seem to work, so I just manually added it to c_cpp_settings.json for each entry in my project. Despite seeing the configuration value in the "Current Configuration" section of the "C/C++: Log Diagnostics", it never showed up in any of the translation unit outputs that followed.

    I started digging and discovered it is likely by design, according to the way the documentation specifies, in addition to some comments by contributors: Colen Garoutte-Carson (@Colengms)'s: #8274 (comment); and Sean McManus (@sean-mcmanus)'s: #2888 (comment), and #1842 (comment)

    The behavior when trying browse.path was the same as forcedInclude.


    Seeing as the header inclusion is supplied via the -include cpp_pch.h argument, it seems like all the prerequisite information is making it into compile_commands.json, but it's just not being interpreted correctly due to missing how gcc automatically searches include paths for <header-filepath>.gch if <header-filepath> doesn't exist.

    If I add -include ../src/pch/cpp_pch.h to the entry for ../src/main.cpp in compile_commands.json, IntelliSense picks it up and immediately works as expected. However, adding such an argument via my build system that ends up generating the compile_commands.json isn't possible, because doing so causes g++ to fail. The comments in this thread about scripting a patch to compile_commands.json post generation does technically work around the solution. As does the "if intellisense include pch in every file" approach. Both of those workarounds are ad hoc and fragile at best, though.

    After all this digging, I still believe the best change would be to have IntelliSense handle the gcc style PCH search, optionally using the PCH generation command if offered in compile_commands.json, (as in, when a command with a .h file and a .h.gch output, use that pairing to handle the lookup behavior rather than needing to do the full search like gcc does).

    As best I can tell, all the logic handling turning the command line arguments into actionable information for IntelliSense is handled within the LSP binaries, so I don't see anywhere to suggest an improvement to handle this on the extension side. If there's a better place for me to submit this issue in that case, please just let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions