天天看點

libpcap交叉編譯到mipsel架構處理器MT7628/n(在Ubuntu系統下,編譯出openwrt系統可運作庫)1、OpenWrt SDK下載下傳路徑:所有版本固件2、libpcap源碼下載下傳:所有版本3、測試,并生成openwrt(mipsel架構)可用的程式4.Makefile檔案的進階用法:5、可能遇到的問題

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 出現以下提示,則配置成功。

[email protected]:/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

[email protected]:/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兩種類庫的使用方法)

遇到問題請随時留言。