gpt4 book ai didi

c - 取消引用指向不完整类型 tcphdr->members 的指针

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:09 25 4
gpt4 key购买 nike

我使用套接字捕获 tcp 数据但构建失败。我的环境是visual studio 2017。我得到错误:

 1>/root/projects/frame_capture/framecapture.c:199:60:error : dereferencing pointer to incomplete type
1> int header_size = sizeof(struct ethhdr) + iphdrlen + (tcph->doff) * 4;

另外,visual studio 2017命令行为:

"gcc" -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-pointer-sign" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -std=c++14 -x c -Wall -fno-strict-aliasing -g2 -gdwarf-2 "g++" -O0 "3600000" -fthreadsafe-statics -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -frtti -fno-omit-frame-pointer -std=c11 -fexceptions -o "D:\linuxCproject\frame_capture\frame_capture\obj\x64\Debug\%(filename).o" 

我在 linux 中使用 gcc 构建它,一切顺利。Linux 代码:

[root@localhost netinet]# gcc -o o.out framecapture.c

我想知道如何在 visual studio 中解决这个问题。代码行:

unsigned short iphdrlen;

struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;

struct tcphdr *tcph = (struct tcphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr));

int header_size = sizeof(struct ethhdr) + iphdrlen + (tcph->doff) * 4;

但是我像这样包含 .h 文件:

#include<netinet/tcp.h>   //Provides declarations for tcp header

tcp.h 中的定义是这样的:

    # else /* !__FAVOR_BSD */
struct tcphdr
{
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
# if __BYTE_ORDER == __LITTLE_ENDIAN
u_int16_t res1:4;
u_int16_t doff:4;
u_int16_t fin:1;
u_int16_t syn:1;
u_int16_t rst:1;
u_int16_t psh:1;
u_int16_t ack:1;
u_int16_t urg:1;
u_int16_t res2:2;
# elif __BYTE_ORDER == __BIG_ENDIAN
u_int16_t doff:4;
u_int16_t res1:4;
u_int16_t res2:2;
u_int16_t urg:1;
u_int16_t ack:1;
u_int16_t psh:1;
u_int16_t rst:1;
u_int16_t syn:1;
u_int16_t fin:1;
# else
# error "Adjust your <bits/endian.h> defines"
# endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
# endif /* __FAVOR_BSD */

最佳答案

最后,我通过探索解决了这个问题。

主要问题是构建命令。

project -> peoperties -> C/C++ -> Language -> C Language standard 选择正确的标准,例如 C99/C89 或其他,我选择解决编译问题的 Default。将c++语言标准设置为与C相同,我也选择默认。

之后我得到命令行:

"gcc" -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-pointer-sign" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -Wall -fno-strict-aliasing -g2 -gdwarf-2 "g++" -O0 "3600000" -fthreadsafe-statics -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -frtti -fno-omit-frame-pointer -fexceptions -o "D:\linuxCproject\icmprequest\icmprequest\obj\x64\Debug\%(filename).o" 

提示:不同的标准在处理!__FAVOR_BSD时是不同的。

关于c - 取消引用指向不完整类型 tcphdr->members 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com