2010-02-18 161 views
2

我正在試驗Windows的WinPcap 4.1.1庫,但我無法設法編譯甚至是庫提供的示例源文件。Winpcap MinGW編譯錯誤

我得到這些錯誤:

'PCAP_OPENFLAG_PROMISCUOUS' 未申報(在一次使用此功能)
'PCAP_SRC_IF_STRING' 未聲明(在一次使用此功能)

而且一堆警告:

隱式函數聲明'localtime_s'
i功能mplicit聲明「pcap_findalldevs_ex」
的功能「pcap_open」
的功能隱式聲明隱式聲明「scanf_s」

我用Google搜索了一下,發現我應該添加一行#define HAVE_REMOTE(我沒有線索它做什麼),但它在更多的錯誤會導致這樣的:

未定義的引用「pcap_open」
未定義的引用「pcap_findalldevs_ex」
取消定義d引用'localtime_s'

"pcap.h"似乎被正確包含(eclipse不報告任何包括錯誤)。 我已將* .lib文件複製到MinGW/lib direcotry中,並在Path and Symbols->Library Paths(eclipse項目屬性)中設置此路徑

我不知道接下來要嘗試什麼。任何想法都歡迎。 在此先感謝

下面的代碼:

#include "pcap.h" 

/* prototype of the packet handler */ 
void packet_handler(u_char *param, const struct pcap_pkthdr *header, 
const u_char *pkt_data); 

int main() 
{ 
pcap_if_t *alldevs; 
pcap_if_t *d; 
int inum; 
int i=0; 
pcap_t *adhandle; 
char errbuf[PCAP_ERRBUF_SIZE]; 

     /* Retrieve the device list on the local machine */ 
     if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) 
     { 
       fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); 
       exit(1); 
     } 

     /* Print the list */ 
     for(d=alldevs; d; d=d->next) 
     { 
       printf("%d. %s", ++i, d->name); 
       if (d->description) 
         printf(" (%s)\n", d->description); 
       else 
         printf(" (No description available)\n"); 
     } 

     if(i==0) 
     { 
       printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); 
       return -1; 
     } 

     printf("Enter the interface number (1-%d):",i); 
     scanf_s("%d", &inum); 

     if(inum < 1 || inum > i) 
     { 
       printf("\nInterface number out of range.\n"); 
       /* Free the device list */ 
       pcap_freealldevs(alldevs); 
       return -1; 
     } 

     /* Jump to the selected adapter */ 
     for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); 

     /* Open the device */ 
     if ((adhandle= pcap_open(d->name,      // name of the device 
                 65536,      // portion of the packet to capture 
                           // 65536 guarantees that the whole packet will be captured 
on all the link layers 
                 PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode 
                 1000,       // read timeout 
                 NULL,       // authentication on the remote machine 
                 errbuf      // error buffer 
                 )) == NULL) 
     { 
       fprintf(stderr,"\nUnable to open the adapter. %s is not supported by 
WinPcap\n", d->name); 
       /* Free the device list */ 
       pcap_freealldevs(alldevs); 
       return -1; 
     } 

     printf("\nlistening on %s...\n", d->description); 

     /* At this point, we don't need any more the device list. Free it */ 
     pcap_freealldevs(alldevs); 

     /* start the capture */ 
     pcap_loop(adhandle, 0, packet_handler, NULL); 

     return 0; 
} 


/* Callback function invoked by libpcap for every incoming packet */ 
void packet_handler(u_char *param, const struct pcap_pkthdr *header, 
const u_char *pkt_data) 
{ 
     struct tm ltime; 
     char timestr[16]; 
     time_t local_tv_sec; 

     /* 
     * unused variables 
     */ 
     (VOID)(param); 
     (VOID)(pkt_data); 

     /* convert the timestamp to readable format */ 
     local_tv_sec = header->ts.tv_sec; 
     localtime_s(&ltime, &local_tv_sec); 
     strftime(timestr, sizeof timestr, "%H:%M:%S", &ltime); 

     printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len); 

} 
+2

「未定義的引用」消息意味着你沒有與'libpcap'鏈接。 'pcap-config --libs'會給你鏈接所需的標誌。 http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/pcap-config.1.html – 2010-02-18 09:33:49

+1

@Alok我認爲你是對的我只是設置庫目錄,而不是鏈接庫。鏈接'wpcap.lib'和'Packet.lib'後,我得到'localtime_s'和'scanf_s'函數只有2個未定義的引用錯誤。這些功能在哪裏? – 2010-02-18 09:57:02

回答

4

您可以取代你localtime_s電話:

localtime_r(&local_tv_sec, &ltime); 

(注交換參數。)

而且,取代你scanf_s請致電scanf

localtime_s()scanf_s()是Microsoft特有的擴展,並且在MinGW中不可用。

+0

我得到了與'localtime_r'相同的錯誤,但我使用'localtime'代替了這種方式'ltime = localtime(&local_tv_sec);'它工作。 – 2010-02-18 10:25:09

+0

是的,看起來'localtime_r'在MinGW中不可用。如果你不關心線程安全性,那麼用適當的'localtime'調用替換它就好了。 – 2010-02-18 10:36:50

+0

另外,我假設你將'ltime'的類型改爲指針。 – 2010-02-18 10:37:14

4

這有點鬆散相關,但我發現它克服了問題,所以我想貢獻一下,所以其他人正在尋找這個線程實際上找到解決方案。

您傳遞給GCC 的參數的順序是重要,您需要指定您的「。ç-lwpcap 「」之前的文件」,否則你會得到鏈接錯誤:喜歡

iflist.c:(.text+0x9d): undefined reference to `pcap_freealldevs' 

sendpack.c:(.text+0x178): undefined reference to `pcap_close' 

這沒有錯,你的MinGW安裝Windows 7或XP,64位或32位,這是傳遞給GCC的參數只是順序

所以我用於編譯實際的命令是:

cd C:\install\winpcap\WpdPack_4_1_2\WpdPack\Examples-pcap\iflist 
gcc -I ../../include -L ../../lib iflist.c -lwpcap -o iflist.exe 

我希望這有助於。