Search code examples
cmakefileopenwrtlibpcap

How to cross-compile a C program for OpenWRT with libpcap library?


I have this small piece of code which uses libpacp library:

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 for this is:

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

I compiled it on OpenWrt SDK and build a .ipk package successfully. Makefile for it is

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)))

When tried to install this package on OpenWrt device using

 opkg install /tmp/ifacelookup_1.0.1_ar71xx.ipk  // transfered .ipk to /tmp folder using scp

I got this error message:

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.

What gone wrong?


Solution

  • I got it working. To make this program run on OpenWrt, libpcap should be installed on the device

    opkg update
    opkg install libpcap