问题
I would like to build a OpenCV from source using a libc++ STL library, instead of default GNU STL. LibC++ offers better C++11 and C++14 support. Is it possible to do that?
回答1:
I've tested this with OpenCV 2.4.7 and Android NDK r10d.
First, you need to download OpenCV source. Unpack the source and replace the platforms/android/android.toolchain.cmake with version that suppports libc++.
Now, open modules/core/include/opencv2/core/operations.hpp
and change line 69 from
(defined __GNUC__ && defined _STLPORT_MAJOR)
to
(defined __GNUC__ && (defined _STLPORT_MAJOR || defined _LIBCPP_VERSION))
Next, in folder platforms/scripts create a script cmake_android_arm_libcxx.sh with following contents:
#!/bin/sh
cd `dirname $0`/..
mkdir -p build_android_arm
cd build_android_arm
cmake -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 -DANDROID_STL=c++_static -DANDROID_NATIVE_API_LEVEL=android-8 -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_DOCS=OFF -DBUILD_FAT_JAVA_LIB=OFF -DBUILD_JASPER=OFF -DBUILD_OPENEXR=OFF -DBUILD_PACKAGE=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_TIFF=OFF -DBUILD_WITH_DEBUG_INFO=OFF -DBUILD_opencv_androidcamera=OFF -DBUILD_opencv_contrib=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_legacy=OFF -DBUILD_opencv_ml=OFF -DBUILD_opencv_nonfree=OFF -DBUILD_opencv_objdetect=OFF -DBUILD_opencv_photo=OFF -DBUILD_opencv_stitching=OFF -DBUILD_opencv_ts=OFF -DBUILD_opencv_video=OFF -DBUILD_opencv_videostab=OFF -DCMAKE_C_FLAGS_RELEASE="-Os -DNDEBUG -fvisibility=hidden -ffunction-sections -fstack-protector-all" -DCMAKE_CXX_FLAGS_RELEASE="-Os -DNDEBUG -fvisibility=hidden -ffunction-sections -fstack-protector-all -fvisibility-inlines-hidden" -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_EIGEN=OFF -DWITH_JASPER=OFF -DWITH_OPENEXR=OFF -DWITH_TIFF=OFF -DWITH_TBB=ON -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake $@ ../..
If you want, you can tweak the parameters of the script (i.e. what is built and how).
Finally, export the path to your NDK build folder
export ANDROID_NDK=~/android-sdks/android-ndk-r10d/
and execute the script:
sh ./scripts/cmake_android_arm_libcxx.sh
Now enter to build_android_arm
folder and build the OpenCV:
cd build_android_arm
make -j9
The libjpeg, libpng and other 3rd party libraries will appear in platforms/build_android_arm/3rdparty/lib
and opencv libraries will appear in platforms/build_android_arm/lib
folder.
This has been tested on Mac OS X 10.10, OpenCV 2.4.7 and Android NDK r10d.
回答2:
Here is a script which I used to build OpenCV 3.0 with clang and libc++ for arm64 (for other ABI's just change toolchain name):
#!/bin/sh
export ANDROID_NDK=~/android-sdks/android-ndk
cmake -DANDROID_TOOLCHAIN_NAME=aarch64-linux-android-clang3.5 -DANDROID_STL=c++_static -DANDROID_ABI="arm64-v8a" -DANDROID_NATIVE_API_LEVEL=android-8 -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_DOCS=OFF -DBUILD_FAT_JAVA_LIB=OFF -DBUILD_JASPER=OFF -DBUILD_OPENEXR=OFF -DBUILD_PACKAGE=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_TIFF=ON -DBUILD_WITH_DEBUG_INFO=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_python2=OFF -DBUILD_opencv_world=OFF -DCMAKE_C_FLAGS_RELEASE="-Os -DNDEBUG -fvisibility=hidden -ffunction-sections -fstack-protector-all" -DCMAKE_CXX_FLAGS_RELEASE="-Os -DNDEBUG -fvisibility=hidden -ffunction-sections -fstack-protector-all -fvisibility-inlines-hidden" -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_EIGEN=OFF -DWITH_JASPER=OFF -DWITH_OPENEXR=OFF -DWITH_TIFF=ON -DWITH_TBB=ON -DWITH_CUDA=OFF -DWITH_CUFFT=OFF -DWITH_WEBP=OFF -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DCMAKE_TOOLCHAIN_FILE=$1/platforms/android/android.toolchain.cmake $1
来源:https://stackoverflow.com/questions/27397258/how-to-build-opencv-for-android-using-libc-stl-library