Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udphop 如何静态编译"-static" #3

Closed
bhzhu203 opened this issue Dec 2, 2022 · 3 comments
Closed

udphop 如何静态编译"-static" #3

bhzhu203 opened this issue Dec 2, 2022 · 3 comments

Comments

@bhzhu203
Copy link

bhzhu203 commented Dec 2, 2022

在 alpine 中静态编译会报

[ 88%] Linking CXX executable udphop
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): warning: relocation against `_ZTVN5Botan5SHA_3E' in read-only section `.text._ZN5Botan5SHA_3D2Ev[_ZN5Botan5SHA_3D5Ev]'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::SHA_3::~SHA_3()':
share_defines.cpp:(.text._ZN5Botan5SHA_3D2Ev[_ZN5Botan5SHA_3D5Ev]+0xf): undefined reference to `vtable for Botan::SHA_3'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::SHA_3_256::SHA_3_256()':
share_defines.cpp:(.text._ZN5Botan9SHA_3_256C2Ev[_ZN5Botan9SHA_3_256C5Ev]+0x19): undefined reference to `Botan::SHA_3::SHA_3(unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::SHA_3_384::SHA_3_384()':
share_defines.cpp:(.text._ZN5Botan9SHA_3_384C2Ev[_ZN5Botan9SHA_3_384C5Ev]+0x19): undefined reference to `Botan::SHA_3::SHA_3(unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `aes_256_gcm::set_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x1ed): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2c3): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x375): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x413): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `aes_256_ocb::set_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x33f): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x415): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x4c7): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x565): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `chacha20::set_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2f3): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3c9): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x47b): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x519): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `xchacha20::set_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x239): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x30f): undefined reference to `Botan::AEAD_Mode::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Botan::Cipher_Dir, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3c1): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x45f): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)':
share_defines.cpp:(.text._ZN5Botan16secure_allocatorIhE10deallocateEPhm[_ZN5Botan16secure_allocatorIhE10deallocateEPhm]+0x28): undefined reference to `Botan::deallocate_memory(void*, unsigned long, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::secure_allocator<unsigned char>::allocate(unsigned long)':
share_defines.cpp:(.text._ZN5Botan16secure_allocatorIhE8allocateEm[_ZN5Botan16secure_allocatorIhE8allocateEm]+0x1d): undefined reference to `Botan::allocate_memory(unsigned long, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o): in function `Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)':
share_defines.cpp:(.text._ZN5Botan16secure_allocatorImE10deallocateEPmm[_ZN5Botan16secure_allocatorImE10deallocateEPmm]+0x28): undefined reference to `Botan::deallocate_memory(void*, unsigned long, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x28): undefined reference to `Botan::SHA_3::add_data(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x30): undefined reference to `Botan::SHA_3::final_result(unsigned char*)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x38): undefined reference to `Botan::SHA_3::clone() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x40): undefined reference to `Botan::SHA_3::provider[abi:cxx11]() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x48): undefined reference to `Botan::SHA_3::clear()'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x50): undefined reference to `Botan::SHA_3::name[abi:cxx11]() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_384E[_ZTVN5Botan9SHA_3_384E]+0x60): undefined reference to `Botan::SHA_3::copy_state() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x28): undefined reference to `Botan::SHA_3::add_data(unsigned char const*, unsigned long)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x30): undefined reference to `Botan::SHA_3::final_result(unsigned char*)'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x38): undefined reference to `Botan::SHA_3::clone() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x40): undefined reference to `Botan::SHA_3::provider[abi:cxx11]() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x48): undefined reference to `Botan::SHA_3::clear()'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x50): undefined reference to `Botan::SHA_3::name[abi:cxx11]() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTVN5Botan9SHA_3_256E[_ZTVN5Botan9SHA_3_256E]+0x60): undefined reference to `Botan::SHA_3::copy_state() const'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTIN5Botan9SHA_3_384E[_ZTIN5Botan9SHA_3_384E]+0x10): undefined reference to `typeinfo for Botan::SHA_3'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: src/shares/libSHAREDEFINES.a(share_defines.cpp.o):(.data.rel.ro._ZTIN5Botan9SHA_3_256E[_ZTIN5Botan9SHA_3_256E]+0x10): undefined reference to `typeinfo for Botan::SHA_3'
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/udphop.dir/build.make:100: udphop] Error 1
make[1]: *** [CMakeFiles/Makefile2:134: CMakeFiles/udphop.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
@cnbatch
Copy link
Owner

cnbatch commented Dec 2, 2022

这是GNU链接工具的锅,就连Botan作者都无法解决。
我给Botan作者提了个Issue:randombit/botan#3085 ,问他怎样做才能静态链接。结果得到的回答是,请用动态链接库。

反倒是FreeBSD(用clang)和Windows(用MSVC)都可以正常使用静态链接的版本。

如果需要在Linux(以及NetBSD)使用静态链接,需要按照 https://botan.randombit.net/handbook/building.html 的 The Amalgamation Build 小节的介绍,制作出巨大无比的 botan_all.h 和botan_all.cpp 直接内嵌到项目中。
根据我的使用经验,步骤是:

第一步:

git clone https://github.com/randombit/botan.git
cd botan
./configure.py --amalgamation --disable-modules=pkcs11

第二步:

把生成好的两个 botan_all 复制到 udphop/src/shares

第三步:

修改 udphop/src/shares/CMakeLists.txt,找到

add_library(${THISLIB_NAME} STATIC share_defines.cpp)

修改为

add_library(${THISLIB_NAME} STATIC "share_defines.cpp" "botan_all.cpp")

第四步:

打开 udphop/src/shares/aead.hpp,把那一大串 #include <botan_XXXX> 删掉,改成 #include "botan_all.h"

第五步:

打开 udphop/CMakeLists.txt,把Linux那一行的 include_directories("/usr/include/botan-2") 删掉

这样就可以静态编译了。只是编译出来的二进制十分巨大,比FreeBSD的和Windows的都大得多。

@bhzhu203
Copy link
Author

bhzhu203 commented Dec 3, 2022

这是GNU链接工具的锅,就连Botan作者都无法解决。 我给Botan作者提了个Issue:randombit/botan#3085 ,问他怎样做才能静态链接。结果得到的回答是,请用动态链接库。

反倒是FreeBSD(用clang)和Windows(用MSVC)都可以正常使用静态链接的版本。

如果需要在Linux(以及NetBSD)使用静态链接,需要按照 https://botan.randombit.net/handbook/building.html 的 The Amalgamation Build 小节的介绍,制作出巨大无比的 botan_all.h 和botan_all.cpp 直接内嵌到项目中。 根据我的使用经验,步骤是:

第一步:

git clone https://github.com/randombit/botan.git
cd botan
./configure.py --amalgamation --disable-modules=pkcs11

第二步:

把生成好的两个 botan_all 复制到 udphop/src/shares

第三步:

修改 udphop/src/shares/CMakeLists.txt,找到

add_library(${THISLIB_NAME} STATIC share_defines.cpp)

修改为

add_library(${THISLIB_NAME} STATIC "share_defines.cpp" "botan_all.cpp")

第四步:

打开 udphop/src/shares/aead.hpp,把那一大串 #include <botan_XXXX> 删掉,改成 #include "botan_all.h"

第五步:

打开 udphop/CMakeLists.txt,把Linux那一行的 include_directories("/usr/include/botan-2") 删掉

这样就可以静态编译了。只是编译出来的二进制十分巨大,比FreeBSD的和Windows的都大得多。

十分巨大 是不是可以 strip --strip-debug 或者直接strip 再加upx打包

我这边自己尝试了一下,编译好动态的加载的udphop,把动态依赖库全部复制到当前目录,然后用patchelf修改成加载为当前路径的库

./patchelf --set-interpreter ./ld-linux-x86-64.so.2 udphop 
./patchelf --add-needed libc.so.6  udphop 
./patchelf --add-needed ./libc.so.6  udphop 
./patchelf --add-needed ./libstdc++.so.6  udphop 
./patchelf --add-needed ./libbotan-2.so.19  udphop 
./patchelf --add-needed ./libm.so.6  udphop 
./patchelf --add-needed ./libpthread.so.0  udphop 
./patchelf --add-needed ./libgcc_s.so.1  udphop 
./patchelf --add-needed ./libdl.so.2  udphop 
./patchelf --add-needed ./librt.so.1  udphop 

感谢作者解答

@bhzhu203
Copy link
Author

bhzhu203 commented Dec 3, 2022

已经成功编译静态链接的udphop:

1.一定要用 botan-2x的源码
2.查找udphop源码里的CMake files 添加 -static 编译参数

grep -r 'target_link_libraries' ./

On Modern CMake (3.x+ - target_link_libraries doc), you can apply the flag to specific targets, in this way:
target_link_libraries(your_target_name -static)

#original
-rw-rw-r-- 1 bhzhu bhzhu 23M 12月 3 10:53 udphop-static

#strip
-rw-rw-r-- 1 bhzhu bhzhu 7.6M 12月 3 10:53 udphop-static

#upx
-rwxrwxr-x 1 bhzhu bhzhu 2.2M 12月 3 10:53 udphop-static

@cnbatch cnbatch closed this as completed Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants