the-tcpdump-group / libpcap

the LIBpcap interface to various kernel packet capture mechanism
https://www.tcpdump.org/
Other
2.72k stars 854 forks source link

build statically linked sbin/rpcapd failed #1122

Open leleliu008 opened 2 years ago

leleliu008 commented 2 years ago

https://www.tcpdump.org/release/libpcap-1.10.1.tar.gz

cmakew \
        -DCMAKE_EXE_LINKER_FLAGS=-static \
        -DINET6=ON \
        -DBDEBUG=OFF \
        -DENABLE_REMOTE=ON \
        -DDISABLE_USB=OFF \
        -DDISABLE_SEPTEL=OFF \
        -DDISABLE_NETMAP=OFF \
        -DDISABLE_DBUS=OFF \
        -DDISABLE_BLUETOOTH=OFF \
        -DDISABLE_DAG=ON \
        -DDISABLE_TC=ON \
        -DDISABLE_SNF=ON \
        -S .
        -B build.d
/opt/ppkg/installing/libpcap/bin/wrapper-cc -fPIC -fno-common -Os -DNDEBUG -I/opt/ppkg/installing/libpcap/inc -I/opt/ppkg/installed/flex/include -I/opt/ppkg/installed/openssl/include  -L/opt/ppkg/installing/libpcap/lib -Wl,-rpath,/opt/ppkg/installed/libpcap/lib -L/opt/ppkg/installed/flex/lib -Wl,-rpath,/opt/ppkg/installed/flex/lib -L/opt/ppkg/installed/bison/lib -Wl,-rpath,/opt/ppkg/installed/bison/lib -L/opt/ppkg/installed/openssl/lib -Wl,-rpath,/opt/ppkg/installed/openssl/lib -L/opt/ppkg/installed/perl/lib -Wl,-rpath,/opt/ppkg/installed/perl/lib  -Wl,--as-needed -Wl,-z,muldefs -Wl,--allow-multiple-definition -Wl,-S -O3 -DNDEBUG -static    -rdynamic testprogs/fuzz/CMakeFiles/fuzz_pcap.dir/onefile.c.o testprogs/fuzz/CMakeFiles/fuzz_pcap.dir/fuzz_pcap.c.o -o run/fuzz_pcap  libpcap.a  -Wl,-Bstatic  -lssl  -lcrypto  -Wl,-Bdynamic  -ldl
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: attempted static link of dynamic object `/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/libc.so'
collect2: error: ld returned 1 exit status

Screenshot from 2022-08-27 05-07-35

the reason is ${OPENSSL_LIBRARIES} is used

when -static is given, cmake will automatically convert libraries's fullpath (stored in ${OPENSSL_LIBRARIES}) to -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -ldl

I suggest you use OpenSSL::SSL and OpenSSL::Crypto cmake imported targets instead of ${OPENSSL_LIBRARIES} cmake variable.

cmake don't convert libraries's fullpath (stored in OpenSSL::SSL and OpenSSL::Crypto) to -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -ldl

Reference: https://cmake.org/cmake/help/latest/module/FindOpenSSL.html

leleliu008 commented 2 years ago

patch:

before compile:

sed -i 's|${OPENSSL_LIBRARIES}|OpenSSL::SSL OpenSSL::Crypto|g' CMakeLists.txt

after installing:

sed -i 's|-lOpenSSL::SSL|-lssl|'       libpcap.pc
sed -i 's|-lOpenSSL::Crypto|-lcrypto|' libpcap.pc