我正在试验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(<ime, &local_tv_sec);
strftime(timestr, sizeof timestr, "%H:%M:%S", <ime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
“未定义的引用”消息意味着你没有与'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
@Alok我认为你是对的我只是设置库目录,而不是链接库。链接'wpcap.lib'和'Packet.lib'后,我得到'localtime_s'和'scanf_s'函数只有2个未定义的引用错误。这些功能在哪里? – 2010-02-18 09:57:02