windows Winpcap MinGW 编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2287526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winpcap MinGW compile error
提问by Petr Peller
I'm experimenting with WinPcap 4.1.1 libraries for Windows, but I can't manage to compile even example source provided with the library.
我正在试验适用于 Windows 的 WinPcap 4.1.1 库,但我什至无法编译该库提供的示例源代码。
I'm getting these errors:
我收到这些错误:
'PCAP_OPENFLAG_PROMISCUOUS' undeclared (first use in this function)
'PCAP_SRC_IF_STRING' undeclared (first use in this function)
'
PCAP_OPENFLAG_PROMISCUOUS' 未声明(首次在此函数中使用)'PCAP_SRC_IF_STRING' 未声明(首次在此函数中使用)
And bunch of warnings:
还有一堆警告:
implicit declaration of function 'localtime_s'
implicit declaration of function 'pcap_findalldevs_ex'
implicit declaration of function 'pcap_open'
implicit declaration of function 'scanf_s'
函数“localtime_s”的
隐式声明 函数“pcap_findalldevs_ex”的
隐式声明 函数“pcap_open”的
隐式声明 函数“scanf_s”的 隐式声明
I Googled up a bit and found that I should add a line #define HAVE_REMOTE
(I have no clue what does it do) but it leads in much more errors like this:
我用谷歌搜索了一下,发现我应该添加一行#define HAVE_REMOTE
(我不知道它是做什么的)但它会导致更多这样的错误:
undefined reference to 'pcap_open'
undefined reference to 'pcap_findalldevs_ex'
undefined reference to 'localtime_s'
对“pcap_open”的
未定义引用对“pcap_findalldevs_ex”的
未定义引用对“localtime_s”的未定义引用
The "pcap.h"
seems to be included properly (eclipse does not report any including errors).
I have copied the *.lib files into MinGW/lib direcotry and set this path in the Path and Symbols->Library Paths
(eclipse project properties)
在"pcap.h"
似乎是适当地包括(日食没有报告任何包括错误)。我已将 *.lib 文件复制到 MinGW/lib 目录并在Path and Symbols->Library Paths
(eclipse 项目属性)中设置此路径
I have no idea what to try next. Any ideas are welcome. Thanks in advance
我不知道接下来要尝试什么。欢迎任何想法。提前致谢
Here's the code:
这是代码:
#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);
}
回答by Alok Singhal
You can replace your localtime_s
call with:
您可以将您的localtime_s
电话替换为:
localtime_r(&local_tv_sec, <ime);
(Note the swapped arguments.)
(注意交换的参数。)
Also, replace your scanf_s
call with scanf
.
另外,将您的scanf_s
电话替换为scanf
.
localtime_s()
and scanf_s()
are Microsoft-specific extensions, and are not available in MinGW.
localtime_s()
并且scanf_s()
是 Microsoft 特定的扩展,在 MinGW 中不可用。
回答by Pik Master
This is somewhat loosely related, but I found it problematic to overcome, so I want to contribute, so other people looking into this thread actually find the solution.
这有点松散相关,但我发现克服它有问题,所以我想贡献,所以其他人查看这个线程实际上找到了解决方案。
The order of parameters you are passing to GCC isimportant, you need to specify your ".c" file before the "-lwpcap", otherwise you will get the linking errors: like
你传递给 GCC 的参数顺序很重要,你需要在“-lwpcap”之前指定你的“.c”文件,否则你会得到链接错误:像
iflist.c:(.text+0x9d): undefined reference to `pcap_freealldevs'
or
或者
sendpack.c:(.text+0x178): undefined reference to `pcap_close'
It's nothing wrong with your mingw installation, Windows 7 or XP, 64-bit or 32-bit, it's just the order of the arguments passed to GCC.
您的 mingw 安装、Windows 7 或 XP、64 位或 32 位都没有问题,这只是传递给 GCC 的参数的顺序。
So the actual command I used for compiling was:
所以我用于编译的实际命令是:
cd C:\install\winpcap\WpdPack_4_1_2\WpdPack\Examples-pcap\iflist
gcc -I ../../include -L ../../lib iflist.c -lwpcap -o iflist.exe
I hope this helps.
我希望这有帮助。