之前接触过使用CMake编译IOS的项目,开始编译Android库时候,不是很熟悉Android.mk的编译方式,所以继续沿用CMake编译Android的库。使用gcc编译出来的库在Android stutio上使用时候App有些是可以直接用,但有些动态库会有一些奇奇怪怪的错误。所以换了最新版本的NDK,并采用clang编译,这下编译出来的库在Android stutio上使用是没有什么问题了。现将两种编译方式都总结如下,供大家参考。

 

一、android-ndk-r14b编译(gcc)

 

1. NDK环境配置

 

(1)下载android-ndk-r14b-linux-x86_64.zip

(2)linux下解压android-ndk-r14b-linux-x86_64.zip压缩包到 /usr/android 目录下(解压的目录不局限于这个)

 

#unzip android-ndk-r14b-linux-x86_64.zip

(3)配置NDK,编辑/etc/profile,添加NDK路径。

 

 

# vim /etc/profile

export ANDROID_NDK_HOME=/usr/android/android-ndk-r14b
export PATH=$ANDROID_NDK_HOME:$PATH

(4)输入命令 source /etc/profile,使配置生效。

 

 #source /etc/profile

 

2. cmake工具链

(1)下载android.toolchain.cmake

(2) 修改工具链android.toolchain.cmake,将1325行改为:

 

set( ANDROID_LINKER_FLAGS "--sysroot=${ANDROID_SYSROOT}" )

注:如果不添加"--sysroot=${ANDROID_SYSROOT}",可能会有编译错误:缺少crtbegin_so.o、crtend_so.o等。

 

 

3. cmake脚本(CMakelists.txt)

 

#****************************This is Project Info****************************
PROJECT(avxallocate)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)

#****************************This is Compile Flag****************************
 if(DEBUG)        
 message( STATUS "==============================debug==============================" )        
 ADD_DEFINITIONS(-g)
 ADD_DEFINITIONS(-D AVX_DEBUG_LEVEL=AVX_DEBUG_DEBUG)
else(DEBUG)   
 message( STATUS "==============================release==============================" )         
 ADD_DEFINITIONS(-O2)
 ADD_DEFINITIONS(-D AVX_DEBUG_LEVEL=AVX_DEBUG_INFO)
 endif()

ADD_DEFINITIONS("-Wall")
ADD_DEFINITIONS("-fPIC")
ADD_DEFINITIONS("-Wl,-lm")
ADD_DEFINITIONS("-std=gnu++11")
SET(CMAKE_CXX_FLAGS "-Wall -fPIC -Wl,-Bsymbolic -lm -D__STDINT_LIMITS -D__STDINT_MACROS -D__ANDROID__ -DANDROID")

#****************************This is Include File/Directories****************************
#INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/)
INCLUDE_DIRECTORIES(../../libextern/Android/include)

#****************************This is Source File/Directories****************************
SET(DIR_SRCS ../AVXAllocate.cpp)

#****************************This is Library Directories****************************
LINK_DIRECTORIES(./)
LINK_DIRECTORIES(../../libextern/Android/lib)

#****************************This is 3Party Library****************************
#LINK_LIBRARIES(pthread)

#****************************This is Output Directory****************************

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)

#****************************This is Output Library****************************
ADD_LIBRARY(avxallocate SHARED ${DIR_SRCS} )
TARGET_LINK_LIBRARIES(avxallocate)

 

4. Shell脚本(ShellScript)

#!/bin/bash
rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake

if [[ “$@“ =~ "-d" ]];then
        echo "----------------------------cmake debug----------------------------"
cmake -DDEBUG=ON -DCMAKE_TOOLCHAIN_FILE=../../bulid/Android/toolchain/android.toolchain.cmake \
      -DANDROID_NDK=$ANDROID_NDK_HOME                \                  \
      -DANDROID_ABI="armeabi-v7a"          \
	  -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9\
	  -DANDROID_NATIVE_API_LEVEL=21\
	  -DANDROID_STL=gnustl_static\     
	  .
else      
        echo "----------------------------cmake release----------------------------"
cmake -DDEBUG=NO -DCMAKE_TOOLCHAIN_FILE=../../bulid/Android/toolchain/android.toolchain.cmake \
      -DANDROID_NDK=$ANDROID_NDK_HOME                \                   \
      -DANDROID_ABI="armeabi-v7a"          \
	  -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9\
	  -DANDROID_NATIVE_API_LEVEL=21\
	  -DANDROID_STL=gnustl_static\
      .	  
fi
	  
make 

rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake

5. 编译

 

        将CMake脚本和Shell脚本放在同一目录下,赋予ShellScript执行权限,执行./ShellScript命令,生成的.so文件在指定生成目录下。

 

一、android-ndk-r16编译(clang)

 

1. NDK环境配置
        和android-ndk-r14b配置步骤一样,下载地址为:android-ndk-r16-linux-x86_64.zip

2. cmake工具链

        使用ndk自带的工具链android.toolchain.cmake(在目录android-ndk-r16-linux-x86_64\android-ndk-r16\build\cmake下)

3. cmake脚本(CMakelists.txt)

 

#****************************This is Project Info****************************
PROJECT(avxallocate)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)

#****************************This is Compile Flag****************************
 if(DEBUG)        
 message( STATUS "==============================debug==============================" )        
 ADD_DEFINITIONS(-g)
 ADD_DEFINITIONS(-D AVX_DEBUG_LEVEL=AVX_DEBUG_DEBUG)
else(DEBUG)   
 message( STATUS "==============================release==============================" )         
 ADD_DEFINITIONS(-O2)
 ADD_DEFINITIONS(-D AVX_DEBUG_LEVEL=AVX_DEBUG_INFO)
 endif()

#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__ANDROID__ -DANDROID")

#****************************This is Include File/Directories****************************
#INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/)
INCLUDE_DIRECTORIES(../../libextern/Android/include)

#****************************This is Source File/Directories****************************
SET(DIR_SRCS ../AVXAllocate.cpp)

#****************************This is Library Directories****************************
LINK_DIRECTORIES(./)
LINK_DIRECTORIES(../../libextern/Android/lib)

#****************************This is 3Party Library****************************
#LINK_LIBRARIES(pthread)

#****************************This is Output Directory****************************

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../bin/Android)

#****************************This is Output Library****************************
ADD_LIBRARY(avxallocate SHARED ${DIR_SRCS} )
TARGET_LINK_LIBRARIES(avxallocate)


4. Shell脚本(ShellScript)

 

 

#!/bin/bash
rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake

if [[ “$@“ =~ "-d" ]];then
        echo "----------------------------cmake debug----------------------------"
cmake -DDEBUG=ON -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
      -DANDROID_NDK=$ANDROID_NDK_HOME \
      -DANDROID_ABI=armeabi-v7a \
      -DANDROID_TOOLCHAIN=clang \
      -DANDROID_PLATFORM=android-21 \
      -DANDROID_STL=c++_shared \
	  .   
else      
        echo "----------------------------cmake release----------------------------"
cmake -DDEBUG=NO -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
      -DANDROID_NDK=$ANDROID_NDK_HOME \
      -DANDROID_ABI=armeabi-v7a \
      -DANDROID_TOOLCHAIN=clang \
      -DANDROID_PLATFORM=android-21 \
      -DANDROID_STL=c++_shared \
	  .  
fi
	  
make 

rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake


5. 编译

 

        将CMake脚本和Shell脚本放在同一目录下,赋予ShellScript执行权限,执行./ShellScript命令,生成的.so文件在指定生成目录下。

 

 

 

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐