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.
CodePudding user response:
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>
// ...