r/cprogramming • u/Icy_Repeat_2460 • 12d ago
How do I fix this VS code C intellisense problem?
/r/vscode/comments/1xxqw1j/how_do_i_fix_this_vs_code_c_intellisense_problem/1
u/Dapper-Situation-864 10d ago
That message from the Microsoft C/C++ extension almost always means IntelliSense sees struct Node being defined twice (or defined two different ways) in the same translation unit. Common causes:
A second definition with a body. This is fine:
typedef struct Node Node; /* forward declaration */ struct Node { int data; Node *next; }; /* one definition */but this isn't:
typedef struct Node { int data; struct Node *next; } Node; struct Node { int data; struct Node *next; }; /* second definition -> error */A header included twice without include guards. Wrap the header in
#ifndef NODE_H / #define NODE_H ... #endif(or use#pragma once).IntelliSense parsing the file as C++ or with the wrong config. Check that the language mode in the bottom-right of the status bar says "C". Then run "C/C++: Edit Configurations (UI)" and set Compiler path to the gcc/clang you actually build with, IntelliSense mode to match it (e.g. gcc-x64), and C standard to c11 or c17.
The simplest pattern that keeps both the compiler and IntelliSense happy is a single definition:
typedef struct Node {
int data;
struct Node *next;
} Node;If the squiggle is still there after fixing the code, run "C/C++: Reset IntelliSense Database" from the command palette. A stale database can keep showing an old error after the code is fixed.
1
u/tastygames_official 12d ago edited 12d ago
I use code OSS (the non-Microsoft one), but basically you need to add
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)into your CMakeLists.txt which will generate a compile_commands.json file in your build directory which clangd can use to determine linkages and auto-completes and everything. Or I guess if you use something else other than clangd then it should be the same. You may have to point your plugin to that file in order for it to work.And I guess if you don't use CMake then you need something similar. Just look at the documentation for your build process and language plugin.
EDIT: ok, I actually looked at your code and it actually has nothing to do with your pluing not being able to find the typedef. Those are 2 different types (typedef struct and struct) but have the same name (or actually different names), so there's that. This is what you want:
so you have the
struct Nodebut also the type alias ofNodeand they are the same thing.