mac os x 将C文件编译成.so库

应用背景

将C语言写好的代码提供给unity调用,项目要在Android手机上运行,所以要将C代码编译成.so库。因为要用到opencv对图像操作,这里把opencvsdk也放进去了。


步骤:

1、下载安装Android studio(其实也可以不下载,楼主为了省事,直接安装Android studio,目的还是为了安装ndk,sdk);

2、配置文件

2.1 jni文件及路径(jni文件下载路径:http://download.csdn.net/detail/boy313918205/9724639)

jni路径:/Users/xlsj/Desktop/dalaofei/MarkerLessARAndroid/jni

mac os x 将C文件编译成.so库_第1张图片


2.2 NDK路径:/Users/xlsj/Desktop/dalaofei/NingBoSubway/android-ndk-r10b

这里使用NDK是:android-ndk64-r10b-darwin-x86_64.tar

mac os x 将C文件编译成.so库_第2张图片


2.3 opencv sdk路径:/Users/xlsj/Desktop/dalaofei/OpenCV-2.4.9-android-sdk

这里下载使用的opencv是OpenCV-2.4.9-android-sdk。

mac os x 将C文件编译成.so库_第3张图片


一下是jni文件中的文件及代码,在步骤2中下载的jni文件中已经包含。

3、Android.mk文件中代码:


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
MAIN_DIR:=$(LOCAL_PATH)
OPENCV_LIB_TYPE:=STATIC
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")  
#try to load OpenCV.mk from default install location  
include /Users/xlsj/Desktop/dalaofei/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk
else  
include $(OPENCV_MK_PATH)  
endif 
LOCAL_PATH:=$(MAIN_DIR)
LOCAL_MODULE:=OpenCV
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)

OpenCV_CAMERA_MODULES:=on
OpenCV_INSTALL_MODULES:=off
OpenCV_LIB_TYPE:=STATIC


include /Users/xlsj/Desktop/dalaofei/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk


LOCAL_MODULE    := MarkerLessARAndroid
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_SRC_FILES := $(LOCAL_PATH)/ARMarkerLessAndroid.cpp


LOCAL_SHARED_LIBRARIES+=OpenCV


LOCAL_LDLIBS += -llog -ldl -DNDEBUG
LOCAL_LDFLAGS += -pthread -fopenmp 
LOCAL_CFLAGS += -DNDEBUG -O1 -O2 -O3 -Os -Ofast -ffunction-sections -fdata-sections -mfloat-abi=softfp -mfpu=neon
LOCAL_CPPFLAGS := -std=c++11 -pthread -frtti -fexceptions -DNDEBUG -O1 -O2 -O3 -Os -Ofast -ffunction-sections -fdata-sections -mfloat-abi=softfp -mfpu=neon -fopenmp
LOCAL_ARM_NEON := true
LOCAL_ARM_MODE := arm


include $(BUILD_SHARED_LIBRARY)

4、

Application.mk文件中代码:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -mfloat-abi=softfp -mfpu=neon -std=gnu++0x -Wno-deprecated \
-ftree-vectorize -ffast-math -fsingle-precision-constant --std=c++11
NDK_TOOLCHAIN_VERSION := 4.9
APP_CFLAGS := --std=c++11
APP_ABI :=armeabi-v7a
APP_OPTIM := release
APP_SHORT_COMMANDS      := true
APP_PLATFORM := android-19


5、

build.sh中代码:

#!/bin/bash

# To build a new jni .so

# and copy the file to where it should stay


FILENAME="libMarkerARAndroid.so"


PROJECT_PATH=$(dirname $(cd $(dirname ${BASH_SOURCE[0]} ); pwd ) )

DEST_PATH="/Users/xlsj/Desktop/dalaofei/MarkerLessARAndroid/jni"

NDK_PATH="/Users/xlsj/Desktop/dalaofei/NingBoSubway/android-ndk-r10b"


if [ $(basename $(pwd) ) !="jni" ];then

  export NDK_PROJECT_PATH=$PROJECT_PATH

fi


${NDK_PATH}/ndk-build


if [ ! -e ${PROJECT_PATH}/libs/armeabi-v7a/${FILENAME} ];then

  echo" Compile process failed, no .so file. "

  echo"--------------------------------------"

else

  echo" Has compiled. "

  echo"---------------"


  if [ -e ${DEST_PATH}/armeabi-v7a/${FILENAME} ];then

    rm --preserve ${DEST_PATH}/armeabi-v7a/${FILENAME}

echo" The old jni .so has been deleted. "

echo"-------------------------------"

  fi

  cp ${PROJECT_PATH}/libs/armeabi-v7a/${FILENAME} ${DEST_PATH}/armeabi-v7a/${FILENAME}

fi


#${NDK_PATH}/ndk-build clean

echo" Has done and cleaned."


:<

!EOF!


以上代码中有opencv,ndk等路径需要修改


6 在终端中运行build.sh

在苹果主界面菜单按顺序:前往——>实用工具——>终端;

在终端中输入build.sh路劲并运行。

如图:

mac os x 将C文件编译成.so库_第4张图片

运行结果,jni同级目录中生产“libs”和“obj”文件。

mac os x 将C文件编译成.so库_第5张图片


将运行文件中的.so文件拷贝中unity中便可以实用。



你可能感兴趣的:(工程项目)