簡體   English   中英

Makefile for C程序使用libpcap(依賴問題)

[英]Makefile for C program using libpcap (dependency problems)

我正在為基於 libpcap 的程序構建 Makefile。 這個 Makefile 將用於在 OpenWrt SDK 中編譯程序,然后將可執行文件傳輸到我的路由器。 這是 Makefile:

path = /home/foo/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc- 7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib

pcap_retrans: pcap_retrans.o
$(CC) $(LDFLAGS) -o pcap_retrans pcap_retrans.o -L$(path) -lpcap -L -libc

pcap_retrans.o: pcap_retrans.c
$(CC) $(CFLAGS) -c pcap_retrans.c cbuffer.c

但是,當我“制作”時出現以下錯誤:

cc  -c pcap_retrans.c cbuffer.c
cc  -o pcap_retrans pcap_retrans.o -L/home/inesmll/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc-7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib -lpcap -L -libc
/usr/bin/ld: warning: libc.so, needed by 
/home/inesmll/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc-7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib/libpcap.so, not found (try using -rpath or -rpath-link)
pcap_retrans.o: In function `got_packet':
pcap_retrans.c:(.text+0x30a): undefined reference to `cbuffer_put'
pcap_retrans.c:(.text+0x334): undefined reference to `cbuffer_getretrans'
pcap_retrans.c:(.text+0x364): undefined reference to `cbuffer_getnumretrans'
pcap_retrans.o: In function `main':
pcap_retrans.c:(.text+0x818): undefined reference to `cbuffer_init'
pcap_retrans.c:(.text+0x87c): undefined reference to `cbuffer_free'
/usr/bin/ld: pcap_retrans: hidden symbol `atexit' in /usr/lib/x86_64-linux-gnu/libc_nonshared.a(atexit.oS) is referenced by DSO
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'pcap_retrans' failed
make: *** [pcap_retrans] Error 1

我相信這與我在 Makefile 中鏈接 cbuffer.c 的方式有關(此文件與 pcap_retrans.c 位於同一文件夾中),但是我不知道如何修復它。 有任何想法嗎?

$(CC) $(CFLAGS) -c pcap_retrans.c cbuffer.c看起來很可疑。

您可能希望將pcap_retrans.ccbuffer.c編譯成單獨的 object 文件。 並使pcap_retrans依賴於pcap_retrans.ocbuffer.o 例如

pcap_retrans: pcap_retrans.o cbuffer.o
    $(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap

%.o: %.c
    $(CC) -c $(CFLAGS) -o $@ $<

剛剛意識到-libc實際上鏈接了一個名為libibc.solibibc.a的庫,而不是標准的 C 庫。 如果該庫位於目錄<dir>中,您可以通過以下幾種方式鏈接它:

  1. 在 linker 命令中傳遞庫的完整路徑,例如$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap <dir>/libibc.so
  2. 在 linker 命令中指定 linker 路徑,例如$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc 您可以指定多個-Wl,-rpath= ,這是ld.so (運行時動態鏈接器)在運行時搜索libibc.so的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM