Linux下libpcap的安装

本文详细介绍了如何在Ubuntu 16.0环境下安装Libpcap 1.4.0版本,包括从官方下载、解压配置到解决依赖问题等步骤,并提供了测试安装是否成功的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1, 环境: Ubuntu16.0 + libpcap-1.4.0.tar.gz

2,Libpcap下载: 官方地址-http://www.tcpdump.org/#latest-release 即可下载最新版本的libpcap。

3,解压libpcap-1.4.0.tar.gz: tar -zxvf libpcap-1.4.0.tar.gz

4,配置生成makefile文件:cd到libpcap解压目录libpcap-1.4.0, 执行 ./configure。执行到这一步可能会遇到缺少flex包的问题:

configure: error: Your operating system's lex is insufficient to compile
 libpcap.  flex is a lex replacement that has many advantages, including
 being able to compile libpcap.  For more information, see
 http://www.gnu.org/software/flex/flex.html .

解决办法:安装flex包---- sudo apt-get install flex

5,make

可能会遇到yacc包错误:

yacc -d grammar.y
make: yacc: Command not found
Makefile:460: recipe for target 'grammar.c' failed
make: *** [grammar.c] Error 127

解决办法:安装byacc包--- sudo apt-get install -y byacc

6,安装: sudo make install

7,完成安装: 可用 man pcap查看帮助

main.c

    #include <pcap.h>  
    #include <stdio.h>  
      
    int main()  
    {  
      char errBuf[PCAP_ERRBUF_SIZE], * device;  
        
      device = pcap_lookupdev(errBuf);  
        
      if(device)  
      {  
        printf("success: device: %s\n", device);  
      }  
      else  
      {  
        printf("error: %s\n", errBuf);  
      }  
        
      return 0;  
    }  

可以成功编译,不过运行的时候却提示找不到libpcap.so.1,因为libpcap.so.1默认安装到了/usr/local/lib下, 我们做一个符号链接到/usr/lib/下即可:ln -s /usr/local/lib/libpcap.so.1  /usr/lib/


test.c

    #include<stdio.h>
    #include<string.h>
    #include<pcap.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<netinet/if_ether.h>
    #include<netinet/ip.h>
    #include<netinet/udp.h>
    #include<netinet/tcp.h>
    #include<netinet/ip_icmp.h>

    #define max 1024


   int main(int argc,char *argv[])
    {
        pcap_if_t *alldev;
        pcap_if_t *p;
        char error[100];
    
    
        int i=0,num; 
        if(pcap_findalldevs(&alldev,error)==-1)
        {
            printf("find all devices is error\n");
            return 0;
        }
        for(p=alldev;p;p=p->next)
        {
            printf("%d:%s\n",++i,p->name);
            if(p->description)
            {
                printf("%s\n",p->description);
            }
        }
        return 1;
    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeroKern

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值