Search code examples
androidandroid-ndkstatic-linkinglibpcapandroid.mk

libpcap static linking errors (undefined reference to pcap APIs) in Android NDK-build


I am trying to run the packetspammer application on an Android nexus-7 wifi only tablet (code name: grouper). I downloaded the libpcap library and was able to generate the static library (libpcap.a). However, when I try to build the packetspammer app using NDK-build, I keep getting linker errors.

My folder structure looks like this:

packetspammer
|
jni
  |
  packetspammer source files
  Android.mk file that links in libpcap and builds the packetspammer binary
  <Directory>platform_external_libpcap 
       |
       libpcap.a file

Here is my android.mk file:

#Links the libpcap library
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := platform_external_libpcap/libpcap
LOCAL_SRC_FILES := platform_external_libpcap/libpcap.a
LOCAL_EXPORT_C_INCLUDES := platform_external_libpcap
include $(PREBUILT_STATIC_LIBRARY)

#Builds packetspammer app
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)

LOCAL_MODULE    := packetspammer
LOCAL_SRC_FILES := radiotap.c packetspammer.c 
LOCAL_STATIC_LIBRARIES  := platform_external_libpcap/libpcap.a
LOCAL_C_INCLUDES := ~/android/android-ndk-r10b/platforms/android-L/arch-arm/usr/include \
                ~/packetspammer/jni/platform_external_libpcap

TARGET_ARCH     := arm

include $(BUILD_SHARED_LIBRARY)

These are the error messages I get:

error: undefined reference to 'pcap_open_live'
error: undefined reference to 'pcap_datalink'
error: undefined reference to 'pcap_compile'
error: undefined reference to 'pcap_geterr'
error: undefined reference to 'pcap_setfilter'
error: undefined reference to 'pcap_geterr'
error: undefined reference to 'pcap_freecode'
error: undefined reference to 'pcap_setnonblock'
error: undefined reference to 'pcap_next_ex'
error: undefined reference to 'pcap_inject'

Can you tell me what is wrong with either the Android.mk file or with the process I follow. I looked at the NDK programmer's guide and followed the section "Building/Standalone toolchain" as well as "Building/Android.mk". I get the same linker error even with the standalone toolchain.

I found many posts that helped me solve several of my errors before I got stuck here. Here are the links to those:

undefined reference error while statically linking;

linking third party libs;

trouble linking static libraries;

using a pre-compiled static library;

libpcap for android;

compile libpcap using android ndk

porting JNetPcap


Solution

  • I was able to fix the issue. I followed the accepted answer in this SO question (compile libpcap using android ndk) and downloaded the libpcap from this site.

    I compiled and built the libpcap.a static library, linked it in the packetspammer android.mk file and got the final executable.