Reputation: 341
I'm building a Makefile for a libpcap-based program. This Makefile will be used to compile the program in the OpenWrt SDK and then transfer the executable to my router. This is the 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
However, the following errors appear when I "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
I believe it has to do with the way I'm linking the cbuffer.c in the Makefile (this file is in the same folder as pcap_retrans.c), however I don't know how to fix it. Any ideas?
Upvotes: 0
Views: 692
Reputation: 136306
$(CC) $(CFLAGS) -c pcap_retrans.c cbuffer.c
looks suspect.
You may like to compile pcap_retrans.c
and cbuffer.c
into separate object files. And make pcap_retrans
depend on pcap_retrans.o
and cbuffer.o
. E.g.
pcap_retrans: pcap_retrans.o cbuffer.o
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
Just realized that -libc
actually links a library called libibc.so
or libibc.a
, not the standard C library. If that library is in directory <dir>
, you can link it in a couple of ways:
$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap <dir>/libibc.so
.$(CC) $(LDFLAGS) -o $@ $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc
. You can specify multiple -Wl,-rpath=
and this is where ld.so
(the runtime dynamic linker) will search for libibc.so
at run-time.Upvotes: 2