1、OpenWrt SDK下载路径:所有版本固件
①针对MT7628处理器下载SDK为: barrier_breaker / 14.07 / ramips / mt7620n 下载相应SDK,解压至任意目录本教程解压至(/usr/local/openwrt14.07下)。
②配置环境变量:vim /etc/profile 添加如下配置,source /etc/profile 刷新生效。
export STAGING_DIR=/usr/local/openwrt14.07/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir
export PATH=$PATH:$STAGING_DIR/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin
③检查环境变量是否配置成功:
方法一:通过echo $PATH 可查看
方法二:命令行输入# mipsel-openwrt-linux-gcc -v 出现以下提示,则配置成功。
root@guc:/home/guc/Desktop/pcapDemo# mipsel-openwrt-linux-gcc -v
Reading specs from /usr/local/openwrt14.07/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/specs
COLLECT_GCC=mipsel-openwrt-linux-gcc
COLLECT_LTO_WRAPPER=/usr/local/openwrt14.07/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../libexec/gcc/mipsel-openwrt-linux-uclibc/4.8.3/lto-wrapper
Target: mipsel-openwrt-linux-uclibc
Configured with: /BB/build/ramips/mt7620n/build_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/gcc-linaro-4.8-2014.04/configure --with-bugurl=https://dev.openwrt.org/ --with-pkgversion='OpenWrt/Linaro GCC 4.8-2014.04 r42625' --prefix=/BB/build/ramips/mt7620n/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2 --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=mipsel-openwrt-linux-uclibc --with-gnu-ld --enable-target-optspace --disable-libgomp --disable-libmudflap --disable-multilib --disable-nls --with-host-libstdcxx=-lstdc++ --with-float=soft --with-gmp=/BB/build/ramips/mt7620n/staging_dir/host --with-mpfr=/BB/build/ramips/mt7620n/staging_dir/host --disable-decimal-float --with-mips-plt --with-mpc=/BB/build/ramips/mt7620n/staging_dir/host --disable-libssp --disable-__cxa_atexit --with-headers=/BB/build/ramips/mt7620n/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/include --enable-languages=c,c++ --enable-shared --enable-threads --with-slibdir=/BB/build/ramips/mt7620n/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib
Thread model: posix
gcc version 4.8.3 (OpenWrt/Linaro GCC 4.8-2014.04 r42625)
2、libpcap源码下载:所有版本
①本教程下载版本为:libpcap-1.3.0,解压至任意目录。
②进入libpcap目录:./configure --build=x86_64 --host=mipsel-openwrt-linux --target=mipsel-openwrt-linux --with-pcap=linux 生成Makefile文件。
说明:--build 当前编译系统(64位);
--host/target 目标系统(mipsel-openwrt-linux)
--with-pcap packet的类型
③修改Makefile文件:vim Makefile
修改的prefix字段为:OpenWrt SDK如下目录
prefix = /usr/local/openwrt14.07/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2
④make ,接着make install ,即可生成mipsel架构处理器可用libpcap.so文件。
3、测试,并生成openwrt(mipsel架构)可用的程序
①测试代码:main.c
#include<stdio.h>
#include<pcap.h>
int main(void)
{
char *dev,errbuf[1024];
dev=pcap_lookupdev(errbuf);
if(dev==NULL){
printf("%s\n",errbuf);
return 0;
}
printf("Device: %s\n", dev);
return 0;
}
②编译程序 #mipsel-openwrt-linux-gcc main.c -o main -lpcap
③查看可执行程序的属性:#file main,可看到软件可运行的系统是MIPS,MIPS32
root@guc:/home/guc/Desktop/test# file main
main: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, interpreter /lib/ld-uClibc.so.0, with debug_info, not stripped
4.Makefile文件的高级用法:
cc=mipsel-openwrt-linux-gcc
target = main
#查找所有的.c 文件
source = $(shell find ./ -name "*.c")
deps = $(shell find ./ -name "*.h")
$(target): $(source)
$(cc) -o $(target) $(source) -lpcap
clean:
rm -rf $(target) *.pcap
通过上面写好的makefile文件,可用一个简单的#make指令生成可执行程序。
5、可能遇到的问题
①libpcap使用:linux下网络监听与发送数据包的方法(即libpcap、libnet两种类库的使用方法)
遇到问题请随时留言。