3

VSCode reports an incomplete type is not allowed error when trying to use struct ip. I know this is a problem with intellisense as my program compiles just fine using gcc monitor.c -o monitor -lnet so there isn't an actual error, but intellisense seems to disagree.

Here is the minimal code to get the error:

#include <netinet/ip.h>
#include <stdlib.h>

int main()
{
    struct ip * my_ip = (struct ip *) malloc(sizeof(struct ip));
}

I tried adding /usr/include/** to the c_cpp_propertied.json file.

Taking a look into the ip.h file directly I can see that the struct definition is "hidden" inside an #ifdef __USE_MISC so I added that to the defines section of c_cpp_propertied.json with no luck.

I'm fresh out of ideas and I haven't been able to find anything related to the issue. Nothing helpful anyway.

1 Answers1

1

Taking a look into the ip.h file directly I can see that the struct definition is "hidden" inside an #ifdef __USE_MISC so I added that to the defines section of c_cpp_propertied.json with no luck.

According to this you can try to do following in your example:

#ifndef __USE_MISC
#define __USE_MISC
#endif // __USE_MISC


#include <netinet/ip.h>
#include <stdlib.h>


int main()
{
    struct ip * my_ip = (struct ip *) malloc(sizeof(struct ip));
}

Explanation: You mentioned that struct is hidden in #ifdef __USE_MISC preprocessor condition. Which means it's only visible if somewhere in your code before this file the definition #define __USE_MISC exists, or if this definiton is passed via compiler flags. Seems like VSCode C/C++ Intellisense isn't doing this, while gcc does


P.S. Also, please take a look at what does this macro means and what does it used for: link


P.P.S. After further investigation was found that OP has no _DEFAULT_SOURCE definition defined in his compiler flags/source files. So, the actual answer is to add following snippet of code before includes (or to the compiler flags or IDE settings):

#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif // _DEFAULT_SOURCE

#include <netinet/ip.h>

// ...

Ihor Baklykov
  • 543
  • 7
  • 24
  • The strange thing is that OP can compile without adding this macro. That means, it must already be defined somewhere where it is visible to the compiler but not to VS Code – Gerhardh Dec 16 '22 at 10:51
  • @Gerhardh well, seems like OP possibly has something with his Glibc paths of includes/libs/pkg-config, because: https://sourceware.org/git/?p=glibc.git;a=blob;f=include/features.h;h=123de9fd4709ed0aa4f3f29d5ec473bdd0152871;hb=HEAD#l99 – Ihor Baklykov Dec 16 '22 at 11:21
  • Admittedly this is my mistake, I should have mentioned I already tried adding `#define __USE_MISC` inside the source file and got a redefinition error when compiling meaning that what @Gerhardh said is true. This time I tried with `#ifndef` as @IGR94 suggested, unfortunately it didn't work. – Nick Pnevmatikatos Dec 16 '22 at 11:24
  • @NickPnevmatikatos check "_DEFAULT_SOURCE" definition. Do you have it? __USE_MISC depends on it. Also, what's your OS? – Ihor Baklykov Dec 16 '22 at 11:27
  • OS, is manjaro linux. Adding _DEFAULT_SOURCE to the includes of vscode seems to have solved it. Should I mark your answer as correct or paste the suggestion on a new answer and mark that one? – Nick Pnevmatikatos Dec 16 '22 at 11:47
  • @NickPnevmatikatos wierd why it's not there already. Let me edit my answer and then you could mark it as correct if you wish – Ihor Baklykov Dec 16 '22 at 11:59