我正在为基于 libpcap 的程序构建 Makefile。这个Makefile将用于编译OpenWrt SDK中的程序,然后将可执行文件传输到我的路由器。这是生成文件:
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
但是,当我“make”时出现如下错误:
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.c
和 cbuffer.c
到单独的目标文件中。并制作pcap_retrans
取决于 pcap_retrans.o
和 cbuffer.o
.例如
pcap_retrans: pcap_retrans.o cbuffer.o
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
刚刚意识到-libc
实际上链接了一个名为 libibc.so
的库或 libibc.a
,而不是标准的 C 库。如果该库在目录 <dir>
中,您可以通过几种方式链接它:
- 在链接器命令中传递库的完整路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap <dir>/libibc.so
. - 在链接器命令中指定链接器路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc
.您可以指定多个-Wl,-rpath=
这就是ld.so
(运行时动态链接器)将搜索libibc.so
在运行时。
关于c - 使用 libpcap 的 C 程序的 Makefile(依赖性问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489144/