Home > Mobile >  Why my header files can't correctly index each other's declaration dependency with clangd
Why my header files can't correctly index each other's declaration dependency with clangd

Time:08-08

I'm now codeing C/C in VSCode with clangd.There are some annoying problems.For example,I defined a variable in "a.h",which also used in "b.h".But it will error in b.h with:

"Unknown type name 'xxxxx'clang(unknown_typename)".

Actually it doesn't affect the compliling results,But always lots of annoying red waves there.

//in "a.h"
typedef unsigned long uint64;
//in "b.h"
uint64 abc; //Error here: (Unknown type name 'uint64'clang(unknown_typename)
//in "xxx.c"
#include "a.h"
#include "b.h"

abc=1; // Correct here

I have only used "complile_commands.json" to configure clangd.It's run good that I can easily jump to definition or declaration.Is there anything more for clangd I need to configure?

(PS: here is my clangd's settins)

"clangd.onConfigChanged": "restart",
    "clangd.arguments": [
        "--clang-tidy",
        "--clang-tidy-checks=performance-*,bugprone-*",
        "--compile-commands-dir=${workspaceFolder}/.vscode/",
        "--background-index",
        "--completion-style=detailed",
        "--enable-config",
        "--function-arg-placeholders=false",
        "--all-scopes-completion",
        "--header-insertion-decorators",
        "--header-insertion=never",
        "--log=verbose",
        "--pch-storage=memory",
        "--pretty",
        "--ranking-model=decision_forest",
        "--cross-file-rename",
        "-j=16"
    ],
    "clangd.checkUpdates": false,

CodePudding user response:

This has nothing to do with VScode or clangd.

Instead, the problem is that in file b.h you have not included a.h and thus uint64 is unknown at the point where you're using it uint64 abc;.

To solve this, you need to include a.h before using uint64:

a.h

#pragma once 
typedef unsigned long uint64;

b.h

#pragma once 
#include "a.h" //added this

uint64 abc;  //works now
  •  Tags:  
  • c c
  • Related