c++ - 在 OpenWrt Makefile 中链接 pcap 库

标签 c++ makefile pcap openwrt

我的简单 pcap 测试应用程序使用以下命令编译并运行良好:gcc main.c -o test -lpcap

但是使用OpenWrt SDK时:make package/myapp/compile V=s

我收到错误消息:

main.c:(.text.startup+0x24): undefined reference to `pcap_lookupdev'
collect2: error: ld returned 1 exit status

根据我所读到的内容,我需要添加以下行:

LDFLAGS = -lpcap

到 Makefile 之一,但我不确定它应该放在哪里。让我困惑的是我可以使用 pcap 常量 PCAP_ERRBUF_SIZE,谁能告诉我为什么我可以访问它,但不能访问 pcap 函数?

main.c

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

int main(int argc, char **argv)
{
    printf("Hello PCAP!\n");
    char *dev, errbuf[PCAP_ERRBUF_SIZE];
    printf("%d\n\n", PCAP_ERRBUF_SIZE);

    // Make works without this part
    dev = pcap_lookupdev(errbuf);
    printf("Device: %s\n", dev);
    return 0;
}

生成文件

include $(TOPDIR)/rules.mk

PKG_NAME:=myapp
PKG_VERSION:=0.1
PKG_RELEASE:=1

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


define Package/myapp
    SECTION:=utils
    CATEGORY:=Utilities
    DEPENDS:=+libpcap
    TITLE:=Intro to PCAP for OpenWrt
endef

define Package/myapp/description
    Outputs device name
endef

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

define Package/myapp/install
    $(INSTALL_DIR) $(1)/usr/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/myapp $(1)/usr/bin/

    $(INSTALL_DIR) $(1)/etc/init.d/
    $(INSTALL_BIN) files/myapp.init $(1)/etc/init.d/myapp
    $(INSTALL_DIR) $(1)/etc/config
    $(INSTALL_CONF) files/myapp.config $(1)/etc/config/myapp
endef

$(eval $(call BuildPackage,myapp))

最佳答案

代码中使用的常量等在编译期间解析并来自头文件。因此找到了 pcap.h 头文件(显然是默认情况下)。

我希望 DEPENDS:=+libpcap 为您处理链接(我不知道为什么还有必要这样做,但是)。 (该行的格式正确吗?)

您实际上需要 LDLIBS 而不是 LDFLAGS (假设您正在使用内置规则和变量)。请参阅10.3 Implicit Variables它们各自的含义。

在不知道 make 框架的其余部分在做什么的情况下,我不能说 LDLIBSLDFLAGS 是否真的可以工作。该框架可能有自己的变量。

关于c++ - 在 OpenWrt Makefile 中链接 pcap 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380785/

相关文章:

Ubuntu "make"错误 : hg: not found

c - netinet/tcp.h 中的有效负载偏移值?

Python。类、结构、字典?

c - 在 iOS 设备上编写和访问应用程序文件

c++ - 在 VS2013 中构建 boost 库

linux - rm 最后有 & 符号和没有

Makefile 收集新目录中的文件列表

c++ - 如何使用多组初始条件运行测试?

c++ - 重载 << 运算符以处理指向字符串的指针

c++ - 无法在 VScode (macOS) 中将头文件链接到我的 main.cpp(clang 错误)