簡體   English   中英

取消指向不完整類型tcphdr-> members的指針

[英]dereferencing pointer to incomplete type tcphdr->members

我使用套接字捕獲tcp數據,但構建失敗。 我的環境是Visual Studio2017。輸出錯誤:

 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 */

最后,我通過探索解決了這個問題。

主要問題是構建命令。

工程->專家-> C / C ++->語言-> C語言標准選擇正確的標准,如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時,不同的標准會有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM