Home » Android » WebRTC: Undefined reference to "__real_malloc" in Android project linking with WebRTC library-Exceptionshub

WebRTC: Undefined reference to "__real_malloc" in Android project linking with WebRTC library-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment


My project is an NDK-based Android shared library that links in Chromium’s WebRTC shared library. When linking my project’s native code component, I see some “undefined reference” errors, but they are for symbols that should be generated by the linker itself:

    __real_malloc, et. al.

The undefined references stem from WebRTC object files:

base/allocator/allocator_shim_default_dispatch_to_linker_wrapped_symbols.cc:39: undefined reference to `__real_malloc'
base/trace_event/trace_log.cc:1615: undefined reference to `__executable_start'

My question: Why are these symbols undefined? How can I fix it? These are the only errors reported. There are no other errors or warnings.

I look forward to any insights from the community. I’ve been stuck on this for a long time! I’m new to Android development and probably doing something basic wrong.


Additional background information, in case it’s helpful.

WebRTC build environment:

  • Ubuntu 18.04
  • Toolchain: NDK r13/clang/llvm (installed via Depot Tools)
  • Cross-compile for Android

My own project build environment:

  • Windows 10
  • Toolchain: NDK r19/clang/llvm
  • Cross-compile for Android

I build my project using a Gradle+CMake configuration generated by Android Studio. First I have to build Chromium’s WebRTC library using their Ninja-based build system (called Depot Tools). Once that succeeds, I create a static library for the WebRTC component using a bash script:

    # Get the names of all the object files.
    find ./obj -name *.o >libwebrtc.list
    # Generate the static library.
    cat libwebrtc.list | xargs ar -crs libwebrtc.a
    # Add an index to the static library.
    ranlib libwebrtc.a

Now I have a static library with WebRTC symbols I can link into my project (my project is itself a shared library). As an aside, I wonder if there’s an “official” way to build this static WebRTC library. I couldn’t find any mention of it.

Next, I compile and link my project using the NDK’s externalNativeBuild to call my CMakeLists.txt script. It compiles everything with no warnings, but the linker fails to find a the symbols mentioned above.

How to&Answers:

I solved my own problem. For future reference: The Chromium WebRTC build does in fact create its own libwebrtc.a static library. I didn’t need to make it myself. Everything links successfully when I reference the “official” version of libwebrtc.a.