如何用libpcap库交叉编译OpenWRT的C程序?

时间:2017-06-08 10:43:00

标签: c makefile openwrt libpcap

我有一小段代码使用libpacp库:

ifacelookup.c

#include <stdio.h>
#include <pcap.h>

int main(int argc, char *argv[])
{
    char *dev, errbuf[PCAP_ERRBUF_SIZE];

    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    printf("Device: %s\n", dev);
    return(0);
}

makefile就是:

LIBS=-lpcap 
ifacelookup: ifacelookup.o
    $(CC) opensniff.o -o ifacelookup $(LDFLAGS) $(LIBS)
ifacelookupf.o: ifacelookup.c
    $(CC) $(CFLAGS) -c ifacelookup.c
clean:
    rm *.o ifacelookup

我在OpenWrt SDK上编译它并成功构建了一个.ipk包。 Makefile就是

include $(TOPDIR)/rules.mk

PKG_NAME:=ifacelookup
PKG_VERSION:=1.0.1
PKG_MAINTAINER:=MDK 
PKG_LICENSE:=GPL-2
PKG_BUILD_DEPENDS:=libpcap    # Added the dependancy 

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

define Package/ifacelookup
    SECTION:=utils
    CATEGORY:=Utilities
    DEPENDS:=+libpcap    # Added the dependent library
    TITLE:=Test raditap header fields.
endef

define Package/$(PKG_NAME)/description
    Test the network card for the radiotap fields it supports.
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Configure
endef

define Build/Compile
    $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
endef

define Package/$(PKG_NAME)/install
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/bin
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

尝试使用

在OpenWrt设备上安装此软件包时
 opkg install /tmp/ifacelookup_1.0.1_ar71xx.ipk  // transfered .ipk to /tmp folder using scp

我收到此错误消息:

Installing ifacelookup (1.0.1) to root...
Collected errors:
 * satisfy_dependencies_for: Cannot satisfy the following dependencies for ifacelookup:
 *  libpcap * 
 * opkg_install_cmd: Cannot install package ifacelookup.

出了什么问题?

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。要使此程序在OpenWrt上运行,应在设备上安装libpcap

opkg update
opkg install libpcap