Today my colleague asked me a question. He reported an error when he made “Line 201 : dereferencing pointer to incomplete type”. I immediately checked a lot of information, but I didn’t see why. Finally, the problem was solved, and the principle was understood, so I recorded it.
His question is specifically like this.
Error on line 201 during make: dereferencing pointer to incomplete type.
First of all, let me talk about the meaning of this error. In layman’s terms, I tried to access the variable pointed to by the pointer, but found that the variable was an incomplete type, and more errors occurred in accessing the members of the structure union. It can be seen from the code that the variable pointed to by icmp_type is actually accessed from line 201, and line 200 has not been accessed yet.
So I guessed, is struct icmp not defined? Then I checked the /usr/include/netinet/ip_icmp.h file and found that there is a definition of struct icmp. It’s weird, isn’t it? After writing some demo tests, the final conclusion is that there is really no definition of struct icmp!
It’s even stranger to see this. Why is this conclusion? Looking closely at the /usr/include/netinet/ip_icmp.h file, you will find that the definition of struct icmp is contained in a macro j, as shown below:
When you see this, you should understand. When compiling, if -D__USE_BSD is not added to the compile command gcc …, then the definition of struct icmp will not be included, so the previous line 201 error is reported: Dereferencing pointer to incomplete type, and that’s how it led me to start wondering why it is clearly defined, but it is said to be an incomplete type. So in order to verify this conclusion, I wrote a small demo to test, and found that the compiler will pass if -D__USE_BSD is added, otherwise it will fail to compile.
In the process of solving this problem, I wrote a lot of demos, let’s summarize below.
1. If the error “dereferencing pointer to incomplete type ” is reported , first try to find out whether the definition of the structure variable in the line can be found, you can use the grep “struct xxx” /usr/include -R command to recursively search the /usr/include directory If found, you can #include in the .c file. If it is a non-standard header file, add the -I header file directory to the compile command, for example (-I/usr/local/xxx/include).
2. If the error “dereferencing pointer to incomplete type ” is still reported after #include , try to check the file carefully to see if the definition of the structure is wrapped by a compiled macro. If it is indeed in the package of a compiled macro, Add -D compilation macro in the compilation command (such as -D__USE_BSD)
After the above two steps, the error of “dereferencing pointer to incomplete type ” can basically be solved
- [Go] Solve panic: runtime error: invalid memory address or nil pointer dereference in golang
- Error 2 error c2491: XX: definition of dllimport static data member is not allowed
- How to Solve Error: “initializer element is not constant”
- Hash_map is deprecated and will be REMOVED. Please use unordered_map.
- Libtorch Error: Expected object of type Variable but found type CUDALongType for argument #2 ‘index’
- Regarding Pyhton regular error: sre_constants.error: nothing to repeat at position
- Error in Tensorflow using variables: List of Tensors when single Tensor expected
- Solve error LNK2001 about pcl::io::vtkPolyDataToPointCloud
- Error (active) e1696 cannot open source file “errno.H” rpgworld problems encountered in installing visual studio 2017
- Error: # error PCL requires C++ 14 or above
- Keil’s duplicate definition problem: Error: L6200E: Symbol F6x8 multiply defined
- QT solves error: use of deleted function and is private within this context
- Cmake Error The C compiler identification is GNU 7.5.0 — The CXX compiler identification is unknown
- Mac installation tree command error solution
- Visual Studio 2012 error C4996: ‘scanf’: This function or variable may be unsafe.
- How to Fix “initializer element is not constant” error
- C++ BUG: [Error] invalid array assignment
- Error running ‘PigSellCabinetApplication’: Command line is too long. Shorten command line for PigSellCabinetApplication or also for Spring Boot default configuration
- std::dynamic_pointer_cast Error: source type is not polymorphic
- NVM ERROR open \settings.txt: The system cannot find the file specified