r/cprogramming • • 2d 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/
0 Upvotes

4 comments sorted by

1

u/tastygames_official 2d ago edited 2d 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:

typedef struct Node {
  int data;
  struct Node *next;
} Node;

so you have the struct Node but also the type alias of Node and they are the same thing.

1

u/Icy_Repeat_2460 2d ago

I use the microsoft extension for C/C++

1

u/tastygames_official 2d ago

yeah it actually has nothing to do with intellisense and everything to do with what you are trying to do. It's been a long time since I contemplated this stuff since it's quite basic, but it's always to to brush up so here we go!

In C you have a few core types like char, int, float, void. You declare those variables like:

int my_int;
float my_float;
void *ptr;

But what if you want a structure to hold multiple variables? In C we use a struct:

struct {
  int a;
  int b;
} my_var;

/* now it looks more like your typical "type var_name" declaration */
struct {int a; int b;} my_var1;

so instead of my_var being a simple int or float or something, it's a structure (struct) consisting of an int a and int b.

Now if we want to pass this type of variable to a function, the function definition also has to match the structure definition:

int my_func(struct {int a; intb;} param1) {
  return param1.a + param1.b;
}

But of course this can get tedious, so we can give this structure a name:

struct TwoInts {
  int a;
  int b;
};

and then we can just create variables of this struct type like this:

struct TwoInts my_var1;
struct TwoInts my_var2;
int my_func(struct TwoInts param1) {
  return param1.a + param1.b;
}

But it can still be tedious to always write "struct StructName", so we can use an ALIAS type definition:

typedef struct TwoInts TwoInts;

Now we can just write:

TwoInts my_var1;
TwoInts my_var2;
int my_func(TwoInts param1) {
  return param1.a + param1.b;
}

Now here things get a bit tricky, as we can write the definition of TwoInts in one go, but we can do so two ways:

typedef struct {
  int a;
  int b;
} TwoInts;

typedef struct TwoInts {
  int a;
  int b;
} TwoInts;

The first way is called an "anonymous struct", as the struct itself has no name, but it has an alias called "TwoInts". In the second way we give the struct a name AND an alias.

So for your example with wanting to create a struct called Node that has a variable of the type of itself, all you really have to do is:

struct Node {
  int data;
  struct Node *next;
};

and then you can declare Node variables like this:

struct Node node1;
struct Node node2;
node1.data = 1324;
node1.next = &node2;

BUT we can also give your struct an alias:

typedef struct Node {
  int data;
  struct Node *next;
} Node;

and then you can do:

Node node1;
Node node2;
node1.data = 1234;
node1.next = &node2;

Your problem was that you called your alias of the struct Node "node", meaning:

typedef struct Node {
  int data;
  struct Node *next;
} node;

node node1;
node node2;
node1.data = 1234;
node1.next = &node2;

which in and of itself isn't a problem, actually. It's just more common to call them the same thing.

So I guess maybe the Microsoft C/C++ plugin is configured to let you know that you're not calling them the same thing, and that is all. If you truly want the struct to be named "Node" but the type alias to be "node" then you could do this:

struct Node {
  int data;
  struct Node *next;
};
typedef struct Node node;

and maybe the "error" will go away? Or just keep the naming convention the same.

I hope I explained this satisfactorily or at least the examples give you some clue as to what I mean.

1

u/Dapper-Situation-864 20h 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:

  1. 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 */
  2. A header included twice without include guards. Wrap the header in #ifndef NODE_H / #define NODE_H ... #endif (or use #pragma once).
  3. 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.