在 linux 上将 libwebrtc.a 与 C++ 链接时出错

Error linking libwebrtc.a with C++ on linux

我已经为 linux 构建了 webrtc,并使用

获得了 libwebrtc.a
gn gen out/Default --args=is_debug=false rtc_use_h264=true target_cpu="x64" target_os="linux" clang_use_chrome_plugins=false use_ghash=false symbol_level=2 use_custom_libcxx=false is_component_ffmpeg=true ffmpeg_branding="Chrome" rtc_include_tests=false rtc_build_examples=true rtc_build_tools=false use_rtti=true rtc_enable_protobuf=false rtc_use_h264=true rtc_link_pipewire=true proprietary_codecs=true

ninja -C . webrtc \
peerconnection_client

但是当我尝试使用我的 C++ 项目 link 静态 webrtc 时,我遇到了 4 个 linker 错误:

1. test.cpp:(.text._ZN10H264BypassedEncoderC2ERKN7cricket10VideoCodecERNS_14DCVAgentClientE+0x191):
    undefined reference to   
    `absl::EqualsIgnoreCase(std::basic_string_view<char,   
    std::char_traits<char> >, std::basic_string_view<char,   
    std::char_traits<char> >)'
 2. CMakeFiles/webRTCserver.dir/src/audio/testEncoder.cpp.o:(.data.rel.ro._ZTVN10testEncoderE[_ZTVN10testEncoderE]+0xb8):
    undefined reference to
    `webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int,
    std::optional<long>)'
 3. testDecoderFactory.cpp/testEncoderFactory.cpp:(.text._ZN10testEncoderFactory20GetSupportedEncodersEv+0x20b): undefined reference to
    `webrtc::SdpAudioFormat::SdpAudioFormat(std::basic_string_view<char,
    std::char_traits<char> >, int, unsigned long,
    std::map<std::__cxx11::basic_string<char, std::char_traits<char>,
    std::allocator<char> >, std::__cxx11::basic_string<char,
    std::char_traits<char>, std::allocator<char> >,
    std::less<std::__cxx11::basic_string<char, std::char_traits<char>,
    std::allocator<char> > >,
    std::allocator<std::pair<std::__cxx11::basic_string<char,
    std::char_traits<char>, std::allocator<char> > const,
    std::__cxx11::basic_string<char, std::char_traits<char>,
    std::allocator<char> > > > >&&)'
 4. H264BypassedEncoder.cpp.o:(.data.rel.ro._ZTVN10H264BypassedEncoderE[_ZTVN10H264BypassedEncoderE]+0x20):
    undefined reference to
    `webrtc::VideoEncoder::SetFecControllerOverride(webrtc::FecControllerOverride*)'

当我在 libwebrtc.a 中查找符号时,如下所示的错误 2:

nm --demangle libwebrtc.a | grep -i webrtc::AudioEncoder::OnReceivedUplinkBandwidth

我得到以下输出:

0000000000000000 T webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)
                 U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)

现在我不确定为什么 nm 会这样显示输出。但是然后使用 ar -d libwebrtc.a <*.o> 我删除了所有具有

的文件
U webrtc::AudioEncoder::OnReceivedUplinkBandwidth(int, absl::optional<long>)

我仍然遇到 linker 错误。

webrtc::VideoEncoder::SetFecControllerOverride 之外的其他未定义引用错误也是如此,我不确定哪个目标会提供。

问题:

编辑: 正如评论中建议尝试使用 is_clang=false,我做了同样的事情并在构建 libwebrtc.a 文件时遇到以下错误:

[5/2977] CXX obj/api/libjingle_peerconnection_api/sctp_transport_interface.o
FAILED: obj/api/libjingle_peerconnection_api/sctp_transport_interface.o 
g++ -MMD -MF obj/api/libjingle_peerconnection_api/sctp_transport_interface.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_NSS_CERTS=1 -DUSE_X11=1 -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -DCR_SYSROOT_HASH=e7c53f04bd88d29d075bfd1f62b073aeb69cbe09 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DWEBRTC_ENABLE_PROTOBUF=0 -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE -DRTC_ENABLE_VP9 -DHAVE_SCTP -DWEBRTC_USE_H264 -DWEBRTC_LIBRARY_IMPL -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0 -DWEBRTC_POSIX -DWEBRTC_LINUX -DABSL_ALLOCATOR_NOTHROW=1 -I../.. -Igen -I../../third_party/abseil-cpp -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -pipe -B../../third_party/binutils/Linux_x64/Release/bin -pthread -m64 -march=x86-64 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Wall -Werror -Wno-unused-local-typedefs -Wno-maybe-uninitialized -Wno-deprecated-declarations -Wno-comments -Wno-packed-not-aligned -Wno-missing-field-initializers -Wno-unused-parameter -O2 -fno-ident -fdata-sections -ffunction-sections -fno-omit-frame-pointer -g2 -fvisibility=hidden -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -std=gnu++11 -Wno-narrowing -Wno-class-memaccess -fno-exceptions --sysroot=../../build/linux/debian_sid_amd64-sysroot -fvisibility-inlines-hidden -Wnon-virtual-dtor -Woverloaded-virtual -c ../../api/sctp_transport_interface.cc -o obj/api/libjingle_peerconnection_api/sctp_transport_interface.o
In file included from /usr/include/c++/7/utility:68:0,
                 from ../../api/sctp_transport_interface.cc:11:
/usr/include/c++/7/x86_64-redhat-linux/bits/c++config.h:3:10: fatal error: bits/wordsize.h: No such file or directory
 #include <bits/wordsize.h>

我能够解决与链接构建相关的所有问题 libwebrtc.a 并且构建的可执行文件工作正常。

谁能告诉我其他调试错误 1,2 和 3 的方法,因为符号在 libwebrtc.a

Was using cxx_std_17 and the required was cxx_std_14

The issue lies in usage of cxx_std_17. In the code that I was building has used certain methods which were not available with default cxx_std_11 so I just used cxx_std_17 but it caused a lot of issues with libwebrtc.a. Even though webrtc style guide says that the code is valid c++17 but faced issues. In the same style guide it's mentioned that webrtc code is written is c++14 and that gave me idea to switch to cxx_std_14 and the errors resolved. As mentioned in same webrtc style guide that "Some older parts of the code violate the style guide in various ways." so I think some older code is there which caused the errors

链接器错误 1 ​​和 3 引用 .cpp 文件而不是错误 2 中的 .o 文件的原因可能是什么

I think the reason behind .cpp or .o is that if error originated in my code then it shows .cpp but if it's in 3rd party library, as was the case with a few linker errors which originated from libwebrtc.a, then it shows the error in .o file as the library only has compiled files

如何为 webrtc 构建找到 ninja 目标以解决错误 4 的未定义引用?

Issue was with the commit I was trying to build.

From webrtc release notes I found out the stable m77 branch head commit and found out the commit I was using didn't have the functions the linker error was referring to but the head commit of stable m77 branch have those functions.

注意: 一般来说,要获得包含所需符号的 .o 文件的目标,我们只需要执行 grep,它将列出定义符号相关函数的位置,ninja -t targets all 将显示所有目标。所以只需转到相应的 .ninja 文件并将所有 .o 文件复制到 webrtc.ninja 并再次构建 libwebrtc.a