pristineio / webrtc-build-scripts

A set of build scripts useful for building WebRTC libraries for Android and iOS.
BSD 3-Clause "New" or "Revised" License
1.13k stars 447 forks source link

Need help compiling an Android plugin (.so) which uses WebRTC #117

Closed eguendelman closed 9 years ago

eguendelman commented 9 years ago

I've successfully built the Android WebRTC libraries using the scripts in this repository. Now what I really want to do is compile an Android C++ library (.so) which uses WebRTC together with a bunch of other code for my use. I want to use the typical ndk-build system (with Android.mk, Application.mk) to create this plugin. My main struggle has generally been knowing which C++ runtime (APP_STL) to choose, and maybe someone can help me with this.

Specifically my Android library will not only use WebRTC, but also the boost libraries as well as other third party libraries. So I need to find a way that all of them can be linked together with consistent C++ runtime...

Before dealing with the hard case of mixing WebRTC with boost, I wanted to verify that I know how to build an Android library which only links to WebRTC. So I created the dummy cpp file, foo.cpp, seen below, and eventually determined the right .mk files that allow it to build with ndk-build.

foo.cpp

#include "webrtc/base/thread.h"
extern "C" void foo() { rtc::Thread *thread = new rtc::Thread; }

Application.mk:

NDK_TOOLCHAIN := arm-linux-androideabi-4.9
APP_ABI := armeabi-v7a
APP_STL := c++_static

Android.mk:

LOCAL_PATH := $(call my-dir)
WEBRTC_ROOT = /home/vagrant/mybuild/webrtc/src
WEBRTC_LIB_ROOT = $(WEBRTC_ROOT)/out_android_armeabi-v7a/Release/obj

include $(CLEAR_VARS)  
LOCAL_CFLAGS    := -DNDEBUG -DWEBRTC_POSIX -DWEBRTC_LINUX -DWEBRTC_ANDROID -std=gnu++11
LOCAL_LDLIBS    :=  -llog -L$(WEBRTC_LIB_ROOT)/webrtc/base \
                   -lrtc_base -lrtc_base_approved \
                   -L$(NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \
                   -lc++_static

LOCAL_MODULE    := foo
LOCAL_SRC_FILES := ../foo.cpp
LOCAL_C_INCLUDES:= $(WEBRTC_ROOT)
include $(BUILD_SHARED_LIBRARY)

This is the combination that seemed to work for me. Other choices of APP_STL seemed to fail... Does that seem right? Do I have to use c++_static in order to be consistent with the way the WebRTC libraries were built?

But now I'm struggling with mixing it with boost. Until now I've been dealing with boost by building using CrystaX NDK which seemed to have simpler support for boost. When I've done this in the past, I've generally set APP_STL:=gnustl_static, which seems like it may be inconsistent with the way we built WebRTC... Does that mean I will need to configure WebRTC to build with CrystaX too? Does anyone know of a simpler solution to add the boost libraries to the mix?

Any help would be appreciated.

Eran.

ArikYa commented 9 years ago

Eran, I'm not sure I can help you, WebRTC is meant to be built using the scripts and not the NDK. Can't you just link the built libraries in your project? Can't they be mixed together after they are actually built?

eguendelman commented 9 years ago

Thanks. I did (hopefully) solve this issue in the end.

Again the issue was that I needed to use both WebRTC and boost in my project, and the former is built (by default) with llvm-libc++, while the latter is built with gnustl. And mixing them together caused problems.

So what I ended up doing is creating a thin, C-style wrapper around the functions I needed from WebRTC. I built this wrapper into a shared library (.so), by statically linking to a long list of WebRTC .a files, and also making sure to link with libc++_static to be consistent with the WebRTC compilation. Then I used this .so with the rest of my boost-dependent project by compiling with crystax instead of the stock NDK and linking with gnustl. I saw "hopefully solved" because it does produce some warning related to the fact that my code mixes two types of runtimes, but so far it seems to work okay despite these warnings...

ArikYa commented 9 years ago

glad to hear you overcame the issue

oldmtn commented 8 years ago

I caught this problem too. So I have to build all third party lib to c++_static which cost me a lot of time.

aagmangour commented 8 years ago

Thanks for the solution ,that was a great help.