V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cnbatch
V2EX  ›  C++

GNU 工具链编译出来的 libbotan-2.a 形同虚设,无法用于静态链接

  •  
  •   cnbatch · 2022-12-02 17:07:33 +08:00 · 2666 次点击
    这是一个创建于 724 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在“宽带症候群”发了个试验品相关的帖子 /t/898299

    这个试验品用到了 Botan2 这个库,原本我是打算全部都用静态编译的,FreeBSD (用 clang )和 Windows (用 MSVC )都成功了,偏偏 Linux (用 GCC )和 NetBSD (也是 GCC )就失败。其他人同样尝试过在 musl 下静态编译,一样失败:
    https://github.com/cnbatch/udphop/issues/3

    其中 Windows 平台用的是 vcpkg 安装 Botan2 ,FreeBSD 、NetBSD 和 Linux 都用自带的包管理器安装。

    于是几天前发布的 Linux 版本二进制文件都是动态编译,分了 3 个版本:Fedora 最新的 Botan 2.19.3 、Debian 10 的 Botan 2.9 、Debian 11 的 Botan 2.17

    结果 musl 用户就无法用了。

    我给 Botan 的作者提了个 Issue ,问他怎样才能成功地使用 libbotan-2.a 做静态编译
    https://github.com/randombit/botan/issues/3085
    得到的回答是,用动态编译就能够编译成功

    这不就死循环无解了嘛

    还好,Botan 的作者还有另外一个解决办法,那就是生成 botan_all.h 和 botan_all.cpp ,直接 #include 起来

    唯一缺点就是生成的二进制文件过于巨大

    在使用 botan_all.h 和 botan_all.cpp 之前我还尝试过在 Linux 安装 clang 13 ,并指定用 clang 编译项目,结果仍然一样,libbotan-2.a 无法用于静态编译

    这也难怪 Fedora 版本(我在 rawhide 看到的)的 Botan2 及 devel 版本都不附带 libbotan-2.a ,这样就可以无条件使用动态链接库版本,避免编译失败


    是否还有其他办法可以在 GNU 工具链下用 libbotan-2.a 做静态链接?
    21 条回复    2022-12-04 21:33:11 +08:00
    lixile
        1
    lixile  
       2022-12-02 20:42:23 +08:00
    以我浅薄的理解 可能要在你当前的 gun 工具链下自行编译 libbotan.a 然后链接进去
    大概率是 ld gcc 对不同内核 或者实现不兼容导致的?
    zoumouse
        2
    zoumouse  
       2022-12-02 21:39:19 +08:00
    用作者回复中给你的命令会报什么错?
    cnbatch
        3
    cnbatch  
    OP
       2022-12-03 00:43:42 +08:00
    @lixile 用当前工具链,自己编译出来的 libbotan.a 照样无法用于后续的静态链接,而且无论是 Linux 还是 NetBSD 都一样
    cnbatch
        4
    cnbatch  
    OP
       2022-12-03 00:45:24 +08:00
    @zoumouse 作者是回避 libbotan.a 的链接问题,给出的命令可以运行成功,编译出来的程序是使用动态库的版本,等于没回答
    lixile
        5
    lixile  
       2022-12-03 10:01:58 +08:00
    @cnbatch 方便提供一份使用 make V=1 产生的编译日志吗 我想研究一下
    zoumouse
        6
    zoumouse  
       2022-12-03 11:51:46 +08:00
    @cnbatch 加了-static 会报错么,感觉应该没问题的
    changnet
        7
    changnet  
       2022-12-03 15:37:33 +08:00
    简单看了下 issuse ,都是一堆"undefined reference",我猜测应该是属于嵌套引用的问题

    简单地说就是一个程序引用 udphop ,而 udphop 引用 botan2 这个库,你希望静态编译程序的时候,udphop 也是静态编译,然后 botan2 也是静态编译

    问题在于,botan2 发布的时候,他并不知道你的编译参数。当某些参数不匹配的时候,它发布的.a 文件和你的程序就没法链接在一起。

    我试过静态链接 mongodb ,而 mongodb 依赖 libsasl2 ,同样的官方发布的 libsasl2.a 文件是编译不过的,得自己下载 libsasl2 的源码,用相应的参数编译一个再重新链接就可以。

    这个并不算 GNU 工具链的问题,VS 也是有这个问题的,比如有 mt 、md 模式的库,如果和你的程序不匹配,VS 一样链接不了
    cnbatch
        8
    cnbatch  
    OP
       2022-12-03 16:59:06 +08:00
    @lixile 由于 Log 太长,这里删掉了
    /usr/include/botan-2/botan/sha3.h:15:13: warning: this header will be made internal in the future
    15 | BOTAN_FUTURE_INTERNAL_HEADER(sha3.h)
    | ^~~~~~~~~~~~~~~~~~~~~~~~
    这样的警告,其他的都保留

    $ make VERBOSE=1
    /usr/bin/cmake -S/home/USERNAME/udphop -B/home/USERNAME/udphop/build --check-build-system CMakeFiles/Makefile.cmake 0
    /usr/bin/cmake -E cmake_progress_start /home/USERNAME/udphop/build/CMakeFiles /home/USERNAME/udphop/build//CMakeFiles/progress.marks
    make -f CMakeFiles/Makefile2 all
    make[1]: Entering directory '/home/USERNAME/udphop/build'
    make -f src/shares/CMakeFiles/SHAREDEFINES.dir/build.make src/shares/CMakeFiles/SHAREDEFINES.dir/depend
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    cd /home/USERNAME/udphop/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/USERNAME/udphop /home/USERNAME/udphop/src/shares /home/USERNAME/udphop/build /home/USERNAME/udphop/build/src/shares /home/USERNAME/udphop/build/src/shares/CMakeFiles/SHAREDEFINES.dir/DependInfo.cmake --color=
    make[2]: Leaving directory '/home/USERNAME/udphop/build'
    make -f src/shares/CMakeFiles/SHAREDEFINES.dir/build.make src/shares/CMakeFiles/SHAREDEFINES.dir/build
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    [ 11%] Building CXX object src/shares/CMakeFiles/SHAREDEFINES.dir/share_defines.cpp.o
    cd /home/USERNAME/udphop/build/src/shares && /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT src/shares/CMakeFiles/SHAREDEFINES.dir/share_defines.cpp.o -MF CMakeFiles/SHAREDEFINES.dir/share_defines.cpp.o.d -o CMakeFiles/SHAREDEFINES.dir/share_defines.cpp.o -c /home/USERNAME/udphop/src/shares/share_defines.cpp
    [ 22%] Linking CXX static library libSHAREDEFINES.a
    cd /home/USERNAME/udphop/build/src/shares && /usr/bin/cmake -P CMakeFiles/SHAREDEFINES.dir/cmake_clean_target.cmake
    cd /home/USERNAME/udphop/build/src/shares && /usr/bin/cmake -E cmake_link_script CMakeFiles/SHAREDEFINES.dir/link.txt --verbose=1
    /usr/bin/ar qc libSHAREDEFINES.a CMakeFiles/SHAREDEFINES.dir/share_defines.cpp.o
    /usr/bin/ranlib libSHAREDEFINES.a
    make[2]: Leaving directory '/home/USERNAME/udphop/build'
    [ 22%] Built target SHAREDEFINES
    make -f src/networks/CMakeFiles/NETCONNECTIONS.dir/build.make src/networks/CMakeFiles/NETCONNECTIONS.dir/depend
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    cd /home/USERNAME/udphop/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/USERNAME/udphop /home/USERNAME/udphop/src/networks /home/USERNAME/udphop/build /home/USERNAME/udphop/build/src/networks /home/USERNAME/udphop/build/src/networks/CMakeFiles/NETCONNECTIONS.dir/DependInfo.cmake --color=
    make[2]: Leaving directory '/home/USERNAME/udphop/build'
    make -f src/networks/CMakeFiles/NETCONNECTIONS.dir/build.make src/networks/CMakeFiles/NETCONNECTIONS.dir/build
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    [ 33%] Building CXX object src/networks/CMakeFiles/NETCONNECTIONS.dir/connections.cpp.o
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT src/networks/CMakeFiles/NETCONNECTIONS.dir/connections.cpp.o -MF CMakeFiles/NETCONNECTIONS.dir/connections.cpp.o.d -o CMakeFiles/NETCONNECTIONS.dir/connections.cpp.o -c /home/USERNAME/udphop/src/networks/connections.cpp
    [ 44%] Building CXX object src/networks/CMakeFiles/NETCONNECTIONS.dir/client.cpp.o
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT src/networks/CMakeFiles/NETCONNECTIONS.dir/client.cpp.o -MF CMakeFiles/NETCONNECTIONS.dir/client.cpp.o.d -o CMakeFiles/NETCONNECTIONS.dir/client.cpp.o -c /home/USERNAME/udphop/src/networks/client.cpp
    [ 55%] Building CXX object src/networks/CMakeFiles/NETCONNECTIONS.dir/server.cpp.o
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT src/networks/CMakeFiles/NETCONNECTIONS.dir/server.cpp.o -MF CMakeFiles/NETCONNECTIONS.dir/server.cpp.o.d -o CMakeFiles/NETCONNECTIONS.dir/server.cpp.o -c /home/USERNAME/udphop/src/networks/server.cpp
    [ 66%] Building CXX object src/networks/CMakeFiles/NETCONNECTIONS.dir/stun.cpp.o
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT src/networks/CMakeFiles/NETCONNECTIONS.dir/stun.cpp.o -MF CMakeFiles/NETCONNECTIONS.dir/stun.cpp.o.d -o CMakeFiles/NETCONNECTIONS.dir/stun.cpp.o -c /home/USERNAME/udphop/src/networks/stun.cpp
    [ 77%] Linking CXX static library libNETCONNECTIONS.a
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/cmake -P CMakeFiles/NETCONNECTIONS.dir/cmake_clean_target.cmake
    cd /home/USERNAME/udphop/build/src/networks && /usr/bin/cmake -E cmake_link_script CMakeFiles/NETCONNECTIONS.dir/link.txt --verbose=1
    /usr/bin/ar qc libNETCONNECTIONS.a CMakeFiles/NETCONNECTIONS.dir/connections.cpp.o CMakeFiles/NETCONNECTIONS.dir/client.cpp.o CMakeFiles/NETCONNECTIONS.dir/server.cpp.o CMakeFiles/NETCONNECTIONS.dir/stun.cpp.o
    /usr/bin/ranlib libNETCONNECTIONS.a
    make[2]: Leaving directory '/home/USERNAME/udphop/build'
    [ 77%] Built target NETCONNECTIONS
    make -f CMakeFiles/udphop.dir/build.make CMakeFiles/udphop.dir/depend
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    cd /home/USERNAME/udphop/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/USERNAME/udphop /home/USERNAME/udphop /home/USERNAME/udphop/build /home/USERNAME/udphop/build /home/USERNAME/udphop/build/CMakeFiles/udphop.dir/DependInfo.cmake --color=
    make[2]: Leaving directory '/home/USERNAME/udphop/build'
    make -f CMakeFiles/udphop.dir/build.make CMakeFiles/udphop.dir/build
    make[2]: Entering directory '/home/USERNAME/udphop/build'
    [ 88%] Building CXX object CMakeFiles/udphop.dir/src/main.cpp.o
    /usr/bin/c++ -I/usr/include/botan-2 -std=c++17 -MD -MT CMakeFiles/udphop.dir/src/main.cpp.o -MF CMakeFiles/udphop.dir/src/main.cpp.o.d -o CMakeFiles/udphop.dir/src/main.cpp.o -c /home/USERNAME/udphop/src/main.cpp
    cnbatch
        9
    cnbatch  
    OP
       2022-12-03 17:00:11 +08:00
    [100%] Linking CXX executable udphop
    /usr/bin/cmake -E cmake_link_script CMakeFiles/udphop.dir/link.txt --verbose=1
    /usr/bin/c++ CMakeFiles/udphop.dir/src/main.cpp.o -o udphop -lstdc++ -lbotan-2 src/shares/libSHAREDEFINES.a src/networks/libNETCONNECTIONS.a src/shares/libSHAREDEFINES.a
    /usr/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/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/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/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/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]+0x1f0): 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/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2c6): 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/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x372): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/bin/ld: share_defines.cpp:(.text._ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_gcm7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x410): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/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]+0x342): 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/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x418): 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/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x4c4): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/bin/ld: share_defines.cpp:(.text._ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN11aes_256_ocb7set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x562): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/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]+0x2f6): 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/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3cc): 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/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x478): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/bin/ld: share_defines.cpp:(.text._ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN8chacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x516): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/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]+0x23c): 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/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x312): 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/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3be): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/bin/ld: share_defines.cpp:(.text._ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN9xchacha207set_keyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x45c): undefined reference to `Botan::SymmetricAlgorithm::set_key(unsigned char const*, unsigned long)'
    /usr/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)'
    cnbatch
        10
    cnbatch  
    OP
       2022-12-03 17:00:21 +08:00
    /usr/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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[2]: Leaving directory '/home/USERNAME/udphop/build'
    make[1]: *** [CMakeFiles/Makefile2:134: CMakeFiles/udphop.dir/all] Error 2
    make[1]: Leaving directory '/home/USERNAME/udphop/build'
    make: *** [Makefile:91: all] Error 2
    cnbatch
        11
    cnbatch  
    OP
       2022-12-03 17:23:53 +08:00
    @zoumouse 就以我给 Botan 提 issue 时的测试代码为例
    https://github.com/randombit/botan/issues/3085

    即使加了 -static ,还是一样报错。

    @changnet 十分奇怪的是,我自己编译 Botan 源码自己链接,还是不行( Debian 11 和 Debian 最新测试版),但 alpine 用户可以。我按照 alpine 用户给出的经验操作去试,报错信息依然爆了一堆。

    而同样都是 .a 静态编译,clang 工具链一下子就完成了,非常顺畅; VS 解决起来相对也是好一点,vcpkg 同样可以指定编译成动态库还是静态库,我是两种库都备好,这样就可以根据自己的设置选择正确的版本,选对了基本都能编译成功。

    目前 clang 唯一不足的是对于 C++新标准的完善度相对较落后,其他方面我觉得比 GNU 好得多
    lixile
        12
    lixile  
       2022-12-03 17:32:00 +08:00
    额 你可能误解了 我是想看 Botan 的编译日志 而非链接失败的 udphop 编译
    cnbatch
        13
    cnbatch  
    OP
       2022-12-03 18:17:44 +08:00
    @lixile Log 太长,放在临时网盘了:
    https://file.io/SSNkzdCvnASo
    cnbatch
        14
    cnbatch  
    OP
       2022-12-04 02:24:54 +08:00
    上面那个好像失效了,再放一个:
    https://temp.sh/CAfSj/botan2.19.3.log
    pagxir
        15
    pagxir  
       2022-12-04 09:23:03 +08:00 via Android
    .a 创建 index 了么?还有连接是依赖顺序的,所以你得调整参数的顺序。动态库之所以没报错是因为允许未定义的符号然后在在运行时 ld 阶段处理。
    cnbatch
        16
    cnbatch  
    OP
       2022-12-04 18:20:00 +08:00
    @pagxir 首先可以肯定的是,botan 库在编译的时候已经附带 index 的选项,这是 Botan 自己生成的 makefile 开头:
    CXX = g++
    LINKER = $(CXX)
    AR = ar
    AR_OPTIONS = crs
    PYTHON_EXE = /usr/bin/python3


    为了简单起见,就以我给 Botan 作者贴出的 aes.cpp 做编译。

    第一次,用 Debian 自带软件包管理器安装出来的 Botan:
    c++ -lbotan-2 -I/usr/include/botan-2 -lstdc++ -lpthread aes.cpp
    c++ -I/usr/include/botan-2 -lbotan-2 -lstdc++ -lpthread aes.cpp
    c++ -I/usr/include/botan-2 -lstdc++ -lbotan-2 -lpthread aes.cpp
    c++ -I/usr/include/botan-2 -lstdc++ -lpthread -lbotan-2 aes.cpp

    c++ -static -lbotan-2 -I/usr/include/botan-2 -lstdc++ -lpthread aes.cpp
    c++ -static -I/usr/include/botan-2 -lbotan-2 -lstdc++ -lpthread aes.cpp
    c++ -static -I/usr/include/botan-2 -lstdc++ -lbotan-2 -lpthread aes.cpp
    c++ -static -I/usr/include/botan-2 -lstdc++ -lpthread -lbotan-2 aes.cpp
    c++ -static -I/usr/include/botan-2 -lstdc++ -lpthread aes.cpp -lbotan-2

    以上命令全部失败,undefined reference 一堆。唯一成功的就是作者给出的命令
    c++ -I/usr/include/botan-2 -lstdc++ -lpthread aes.cpp -lbotan-2
    这样会编译出动态库版本,而不是静态库版本。

    接着用我下载 Botan 源码手动编译的版本:
    c++ -L:./lib/libbotan-2.a -I./include/botan-2 -lstdc++ -lpthread aes.cpp
    c++ -I./include/botan-2 -L:./lib/libbotan-2.a -lstdc++ -lpthread aes.cpp
    c++ -I./include/botan-2 -lstdc++ -L:./lib/libbotan-2.a -lpthread aes.cpp
    c++ -I./include/botan-2 -lstdc++ -lpthread aes.cpp -L:./lib/libbotan-2.a

    c++ -static -L:./lib/libbotan-2.a -I./include/botan-2 -lstdc++ -lpthread aes.cpp
    c++ -static -I./include/botan-2 -L:./lib/libbotan-2.a -lstdc++ -lpthread aes.cpp
    c++ -static -I./include/botan-2 -lstdc++ -L:./lib/libbotan-2.a -lpthread aes.cpp
    c++ -static -I./include/botan-2 -lstdc++ -lpthread aes.cpp -L:./lib/libbotan-2.a

    以上命令依旧全部失败,仍然还是 undefined reference 一大堆。

    GCC 的版本:
    $ c++ --version
    c++ (Debian 10.2.1-6) 10.2.1 20210110
    Copyright (C) 2020 Free Software Foundation, Inc.
    cnbatch
        17
    cnbatch  
    OP
       2022-12-04 18:23:10 +08:00
    Debian 10 、11 、最新的测试版,试了我很多次,都是一堆 undefined reference ,心累
    pagxir
        18
    pagxir  
       2022-12-04 18:59:53 +08:00 via Android
    调整顺序吧,把 stdcxx 那个挪最后。
    ec0
        19
    ec0  
       2022-12-04 19:16:45 +08:00   ❤️ 1
    我好像在 arch 上静态编译 aes.cpp 成功了
    先安装 botan 发现没有 libbotan-2.a
    然后下载源码并编译出 libbotan-2.a
    然后执行
    g++ -o aaa aes.cpp -I /usr/include/botan-2/ -L Botan-2.19.3 -lbotan-2 -static
    就编译成功了......

    g++ --version
    g++ (GCC) 12.2.0
    ec0
        20
    ec0  
       2022-12-04 20:20:08 +08:00
    你的编译命令里面 -L 传的参数是 /lib/libbotan-2.a
    但是 -L 命令应该传的参数是静态库的目录
    所以如果你的静态库放在 /lib
    应该是 -L /lib 吧?
    cnbatch
        21
    cnbatch  
    OP
       2022-12-04 21:33:11 +08:00
    @pagxir
    @ec0

    好了,这下搞清楚原因在哪了,确实是 GCC 的锅。
    我在 Debian 测试版运行了一次 apt upgrade ,把所有能够更新的都更新一遍,其中就包括 GCC 。我记得应该有两个月没更新过了。

    升级完成后,原本 GCC 10 会失败的命令
    c++ aes.cpp -I./include/botan-2 -lstdc++ -L:./lib/ -lbotan-2 -static
    在最新版 GCC 12.2.0-9 一次性成功

    至于是什么 bug 就懒得查了。我只能说,GCC 真不是省油的灯。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1371 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:41 · PVG 07:41 · LAX 15:41 · JFK 18:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.