fatal error C1189: #error 错误,找不到什么问题。。。

baidu926 2010-11-30 03:05:38
试了很久才把WinPcap的环境搞好。。。。
调试又出问题,一直是这样不知道怎么搞了都

// udp001.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "remote-ext.h "
#include "pcap.h"
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
//4字节的IP地址
typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
//IPv4 首部
typedef struct ip_header
{
u_char ver_ihl; //版本(4 bits) + 首部长度(4 bits)
u_char tos; //服务类型(Type of service)
u_short tlen; //总长(Total length)
u_short identification; //标识(Identification)
u_short flags_fo; //标志位(Flags)(3 bits) + 段偏移量(Fragment offset)(13 bits)
u_char ttl; //存活时间(Time to live)
u_char proto; //协议(Protocol)
u_short crc; //首部校验和(Header checkSum)
ip_address saddr; //源地址(Source address)
ip_address daddr; //目的地址(Destination address)
u_int op_pad; //选项与填充(Option + Padding)
}ip_header;
//UDP 首部
typedef struct udp_header
{
u_short sport; //源端口(Source port)
u_short dport; //目的端口(Destination port)
u_short len; //UDP数据包长度(Datagram length)
u_short crc; //校验和(Checksum)
}udp_header;
//函数原型
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];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;
//获得设备列表
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
printf("Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
//打印列表
for(d = alldevs; d != NULL; d = d->next)
{
printf("%d. %s", ++i, d->name);
if(d->description)
printf(" (%s)\n", d->description);
}
printf("Enter the interface number (1-%d):", i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//跳转到已选设备
for(d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
//打开适配器
if((adhandle = pcap_open(d->name, 65536, 0, 1000, NULL, errbuf)) == NULL)
{
printf("\nUnable to open the adapter. %s is not supported by WinPcap\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//检查数据链路层,为了简单,我们只考虑以太网
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
printf("\nThis program works only on Ethernet networks.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
if(d->addresses != NULL)
//获得接口第一个地址的掩码
netmask = ((struct sockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
//如果接口没有地址,那么我们假设一个C类的掩码
netmask = 0xffffff;
//编译过滤器
if(pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) < 0)
{
printf("\nUnable to compile the packet filter. Check the syntax.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//设置过滤器
if(pcap_setfilter(adhandle, &fcode) < 0)
{
printf("\nError setting the filter.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
printf("\nlistening on %s...\n", d->description);
//释放设备列表
pcap_freealldevs(alldevs);
//开始捕捉
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport, dport;
time_t local_tv_sec;
local_tv_sec = header->ts.tv_sec;

//打印数据包的时间戳和长度
printf("%s. len: %d", ctime(&local_tv_sec), header->len);
//获取IP数据包头部的位置
ih = (ip_header *)(pkt_data + 14);
//获取UDP首部的位置
ip_len = (ih->ver_ihl & 0xf) * 4;

uh = (udp_header *)((u_char*)ih + ip_len);
sport = ntohs(uh->sport);
dport = ntohs(uh->dport);
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}

>------ 已启动生成: 项目: udp001, 配置: Debug Win32 ------
1>正在编译...
1>udp001.cpp
1>d:\wpdpack\include\remote-ext.h(39) : fatal error C1189: #error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\udp001\udp001\Debug\BuildLog.htm”
1>udp001 - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
...全文
10796 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shmily520 2012-05-02
  • 打赏
  • 举报
回复
我按LZ 3L的方法 却多出来很多warning[Quote=引用 14 楼 的回复:]
引用 3 楼 ouyh12345 的回复:
Just define HAVE_REMOTE and then include pcap.h

在设置里添加HAVE_REMOTE或在stdafx.h函数里#define HAVE_REMOTE
然后
#include "pcap.h"
弄错了,尴尬...这个是对的。
[/Quote]
wushaoqiu07 2012-01-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ouyh12345 的回复:]
Just define HAVE_REMOTE and then include pcap.h

在设置里添加HAVE_REMOTE或在stdafx.h函数里#define HAVE_REMOTE
然后
#include "pcap.h"
[/Quote]弄错了,尴尬...这个是对的。
wushaoqiu07 2012-01-17
  • 打赏
  • 举报
回复
成功编译,谢谢了。[Quote=引用 2 楼 baidu926 的回复:]
去掉问题更多了
1>------ 已启动生成: 项目: udp001, 配置: Debug Win32 ------
1>正在编译...
1>udp001.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\udp001\udp001\udp001.cpp(66) : ……
[/Quote]
wushaoqiu07 2012-01-17
  • 打赏
  • 举报
回复
3楼正解,成功编译,谢谢。
  • 打赏
  • 举报
回复
好帖。。。。。
baidu926 2010-11-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ouyh12345 的回复:]

Just define HAVE_REMOTE and then include pcap.h

在设置里添加HAVE_REMOTE或在stdafx.h函数里#define HAVE_REMOTE
然后
#include "pcap.h"
[/Quote]按这个做法现在可以编译通过了。。。谢谢了
baidu926 2010-11-30
  • 打赏
  • 举报
回复
刚刚找了个开发包里 的举例运行了一下也是同样的错误提示。。。。。。
这应该可以排除是代码的问题吧,是不是我的vc还是那里没设置好呢
ouyh12345 2010-11-30
  • 打赏
  • 举报
回复
右击工程--属性--C++--预处理器--预处理器定义
baidu926 2010-11-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ouyh12345 的回复:]

编译选项里设置
[/Quote]找不到这个选项呢,今天才刚装上vc2008很不熟悉。。。

fatal error C1189: #error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h
这个我不知道HAVE_REMOTE要定义在那里,难道是要在头文件里定义,问题是头文件是给定的我不好去改动它吧
jacklzw88 2010-11-30
  • 打赏
  • 举报
回复
fatal error C1189: #error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h

先按照这个说的试试。
ouyh12345 2010-11-30
  • 打赏
  • 举报
回复
编译选项里设置
baidu926 2010-11-30
  • 打赏
  • 举报
回复
搞了很久,代码是没有问题的吧,一直是在设置环境处的问题。。。。
3楼说的在设置里添加时什么意思,不太明白。。。。。
ouyh12345 2010-11-30
  • 打赏
  • 举报
回复
Just define HAVE_REMOTE and then include pcap.h

在设置里添加HAVE_REMOTE或在stdafx.h函数里#define HAVE_REMOTE
然后
#include "pcap.h"
baidu926 2010-11-30
  • 打赏
  • 举报
回复
去掉问题更多了
1>------ 已启动生成: 项目: udp001, 配置: Debug Win32 ------
1>正在编译...
1>udp001.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\udp001\udp001\udp001.cpp(66) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vc2008\vc\include\stdio.h(306) : 参见“scanf”的声明
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\udp001\udp001\udp001.cpp(77) : error C3861: “pcap_open”: 找不到标识符
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\udp001\udp001\Debug\BuildLog.htm”
1>udp001 - 1 个错误,1 个警告
  • 打赏
  • 举报
回复
Please do not include this file directly
也就是说去掉 #include "remote-ext.h " 这行

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧