使用 libpcap 的 C 程序的 Makefile(依赖性问题)
Makefile for C program using libpcap (dependency problems)
我正在为基于 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
实际上 link 是一个名为 libibc.so
或 libibc.a
的库,而不是标准 C 库。如果该库位于目录 <dir>
中,您可以通过以下几种方式 link 它:
- 在 linker 命令中传递库的完整路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap <dir>/libibc.so
.
- 在 linker 命令中指定 linker 路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc
。您可以指定多个 -Wl,-rpath=
,这是 ld.so
(运行 时间动态 linker)将在 运行 时间搜索 libibc.so
的地方.
我正在为基于 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
实际上 link 是一个名为 libibc.so
或 libibc.a
的库,而不是标准 C 库。如果该库位于目录 <dir>
中,您可以通过以下几种方式 link 它:
- 在 linker 命令中传递库的完整路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap <dir>/libibc.so
. - 在 linker 命令中指定 linker 路径,例如
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc
。您可以指定多个-Wl,-rpath=
,这是ld.so
(运行 时间动态 linker)将在 运行 时间搜索libibc.so
的地方.