Building my package with libpcap

Dear forum users,
this might be a very often asked question, but I am breaking my legs since 1-2 days and I dont get it.
I tried to build my c program with setting all up and running

mips-openwrt-linux-musl-gcc -o listen listen.c  -L /home/frood/Documents/git/OpenWrt-V2X/staging_dir/toolchain-mips_24kc_gcc-7.3.0_musl/include/ -I /home/frood/Documents/git/OpenWrt-V2X/staging_dir/target-mips_24kc_musl/usr/include/ -l pcap

And at the end of everything, I ended up creating my own package.
I followed

https://openwrt.org/docs/guide-developer/helloworld/chapter5

And here is my Makefile:

include $(TOPDIR)/rules.mk

# Name, version and release number
# The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR)
PKG_NAME:=listen
PKG_VERSION:=1.0
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=/home/frood/Documents/git/OpenWrt-V2X/listen

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/listen
  SECTION:=mypackages
  CATEGORY:=v2x
  TITLE:=listen
  DEPENDS:=+libpcap
endef

# Package description; a more verbose description on what our package does
define Package/listen/description
  Listening to V2X packets.
endef

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	LDFLAGS=-lpcap
	cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
	$(Build/Patch)
endef

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
	$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/listen.o -c $(PKG_BUILD_DIR)/listen.c
	$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/listen.o
endef

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/listen/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/listen $(1)/usr/bin
endef

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,listen))

A simple hello world works fine, but nit with libpcap.
BTW, libpcap is selected in the menuconfig:

General info: I am using the "famous" OpenWrt version of Francesco:

https://github.com/francescoraves483/OpenWrt-V2X

But this error is what I am ALWAYS getting. Always. Independent of which approach I try - with manual c cross compiling, with package....

mips-openwrt-linux-musl-gcc -L/home/frood/Documents/git/OpenWrt-V2X/staging_dir/target-mips_24kc_musl/usr/lib -L/home/frood/Documents/git/OpenWrt-V2X/staging_dir/target-mips_24kc_musl/lib -L/home/frood/Documents/git/OpenWrt-V2X/staging_dir/toolchain-mips_24kc_gcc-7.3.0_musl/usr/lib -L/home/frood/Documents/git/OpenWrt-V2X/staging_dir/toolchain-mips_24kc_gcc-7.3.0_musl/lib -znow -zrelro -o /home/frood/Documents/git/OpenWrt-V2X/build_dir/target-mips_24kc_musl/listen-1.0/listen /home/frood/Documents/git/OpenWrt-V2X/build_dir/target-mips_24kc_musl/listen-1.0/listen.o
/home/frood/Documents/git/OpenWrt-V2X/build_dir/target-mips_24kc_musl/listen-1.0/listen.o: In function `main':
listen.c:(.text.startup+0x36): undefined reference to `pcap_open_live'
listen.c:(.text.startup+0x52): undefined reference to `pcap_datalink'
listen.c:(.text.startup+0x60): undefined reference to `pcap_datalink'
listen.c:(.text.startup+0x6a): undefined reference to `pcap_datalink'
listen.c:(.text.startup+0x96): undefined reference to `pcap_compile'
listen.c:(.text.startup+0xa2): undefined reference to `pcap_geterr'
listen.c:(.text.startup+0xb4): undefined reference to `pcap_setfilter'
listen.c:(.text.startup+0xc0): undefined reference to `pcap_geterr'
listen.c:(.text.startup+0xd6): undefined reference to `pcap_loop'
listen.c:(.text.startup+0xdc): undefined reference to `pcap_close'
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:53: /home/frood/Documents/git/OpenWrt-V2X/build_dir/target-mips_24kc_musl/listen-1.0/.built] Error 1

I am confused about that the header files are fine and found - no error for

#include <pcap/pcap.h>

but for

session_handler = pcap_open_live(dev, 65535, 1, 1000, errbuf);

Is there anything I can do to solve this error?

Thanks and best regards, Food

After comparing with package tcpdump, I have added
DEPENDS:=+libpcap
.... same result :frowning:

Did you tried ?

TARGET_LDFLAGS+=-lpcap

in the general part of the Makefile not in the define Build/Prepare

Thanks you for the hint -
I have tried it, the "undefined reference" issue is gone, but I am getting the same error as if I link it manually:

/bin/ld: cannot find -lpcap
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:53: /home/frood/Documents/git/OpenWrt-V2X/build_dir/target-mips_24kc_musl/listen-1.0/.built] Error 1

(I have tried it manually like this these days: mips-openwrt-linux-musl-gcc -o listen listen.c -L /home/frood/Documents/git/OpenWrt-V2X/staging_dir/toolchain-mips_24kc_gcc-7.3.0_musl/include/ -I /home/frood/Documents/git/OpenWrt-V2X/staging_dir/target-mips_24kc_musl/usr/include/ -l pcap)

Best regards, Martin

Which leads to this post:

Adding both DEPENDS and TARGET_LDFLAGS, solves the issue:

Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/listen
SECTION:=mypackages
CATEGORY:=v2x
TITLE:=listen
DEPENDS:=+libpcap
TARGET_LDFLAGS+=-lpcap
endef

Ok, but how should someone know that?

Thanks you very much for the hint!!!!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.