OpenWrt Forum Archive

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

The content of this topic has been archived on 16 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

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

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?

(Last edited by haccks on 8 Jun 2017, 16:49)

Just install libpcap package before your program installation:

opkg update
opkg install libpcap
123serge123 wrote:

Just install libpcap package before your program installation:

opkg update
opkg install libpcap

Thanks. It worked. I am able to install the package. Now the problem is it installed libpcap 1.5.3 on chaos calmer. I modified the code  and added this

   

 fprintf(stderr, "pcap_can_set_rfmon: %d\n", pcap_can_set_rfmon(handle) );

Now this always returns zero, though the wlan card can go into monitor mode. How to fix this?

The discussion might have continued from here.