windows 7上使用android-ndk-r8e 编译jrtplib 3.9.1

在查了很多资料后,

发现windows 平台上用ndk编译jrtplib的资料真是不好找

github上的一个哥们做的很有参考价值:https://github.com/jimjh/JRTPLib-for-Android

但一直没在windows上搞定,实在没办法,开虚拟机在ubuntu上把github上的代码clone下来,

编译成功后又研究了一下,总归最后在Windows上搞定了


================================================分割线=====================================================


操作系统:windows 7

需要软件:

1 android-ndk-r8e

2 msys 2.4 (说明:这个是我安装railsinstaller-2.1.0.exe 自带的)

3 cmake 2.8.2


首先,还是jthread的编译,如果之前你用cmake和vs2010编译过这个jthread,

建议你重新换个目录把干净的源代码拷过来备用,不要混合这两个平台

在jthread目录下:

1 新建一个android.sh文件,

2 新建一个android-build目录以放置生成好的库及头文件

src/pthread/jthread.cpp的125行,找到pthread_cancel(threadid);并注释之,替换成 pthread_kill(threadid, SIGUSR1); 

原因么,是因为android用的自家产的Bionic libc而不是标准glibc,具体可wikipedia之。

4 打开CMakeLists.txt,注释掉这一段:

# find_package(Threads)
# if (NOT CMAKE_USE_WIN32_THREADS_INIT)
# 	if (NOT CMAKE_USE_PTHREADS_INIT)
# 		message(FATAL_ERROR "Can find neither pthread support nor Win32 thread support")
# 	endif (NOT CMAKE_USE_PTHREADS_INIT)
# endif (NOT CMAKE_USE_WIN32_THREADS_INIT)


输入以下内容(主要是参考的上面那个github的内容,根据新版的ndk做了修改,怕出问题又直接写的绝对路径,请根据实际情况修改)

我对原作的修改最主要的就是对cmake加了一行参数:

-G "MSYS Makefiles"

#!/bin/sh
# Written by Nxf <[email protected]>
# 
# Builds the JThread library for Android
export NDK_ROOT="/D/Software/Develop/Android/android-ndk-r8e"
export PATH="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/:${PATH}"
SYS_ROOT="${NDK_ROOT}/platforms/android-8/arch-arm"
PREF="arm-linux-androideabi-"

OUT_DIR="`pwd`/android-build"
C_FLAGS="-lstdc++ -lsupc++ \
        -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/include \
        -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/include \
        -L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi"

#set -e

export CC="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}gcc  --sysroot=${SYS_ROOT}"
export CXX="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}g++  --sysroot=${SYS_ROOT}"
export LD="$${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/{PREF}ld  --sysroot=${SYS_ROOT}"
export CPP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}cpp  --sysroot=${SYS_ROOT}"
export AS="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}as"
export OBJCOPY="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}objcopy"
export OBJDUMP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}objdump"
export STRIP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}strip"
export RANLIB="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}ranlib"
export CCLD="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}gcc  --sysroot=${SYS_ROOT}"
export AR="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}ar"

cmake	\
		-DCMAKE_SYSTEM_NAME="Generic" \
        -DCMAKE_CXX_FLAGS="${C_FLAGS}"\
		-DCMAKE_FIND_ROOT_PATH="${SYS_ROOT}" \
		-DCMAKE_INSTALL_PREFIX="${OUT_DIR}" \
		-G "MSYS Makefiles" .
		
		

/D/Software/Develop/Android/android-ndk-r8e/prebuilt/windows/bin/make.exe
/D/Software/Develop/Android/android-ndk-r8e/prebuilt/windows/bin/make.exe install

cd ${OUT_DIR}/lib && \
${AR} -x libjthread.a && \
${CXX} ${C_FLAGS} -shared -Wl,-soname,libjthread.so -o libjthread.so  *.obj && \
rm *.obj

exit 0


在msys命令行下运行这个android.sh就能生成jthread类库了


同理,到jrtplib文件夹下:

1 新建android.sh

2 新建android_build文件夹

3 打开CMakeLists.txt,找到第125行并注释掉: add_subdirectory(examples) 

原因是我编译的时候发现样例编译有问题,干脆就不用了,反正库文件用不到这个


编辑android.sh内容如下:

(需要注意的是jthread库的目录位置,要根据实际情况调整)

#!/bin/sh
# 
# Builds the JThread library for Android
export NDK_ROOT="/D/Software/Develop/Android/android-ndk-r8e"
export PATH="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/:${PATH}"
SYS_ROOT="${NDK_ROOT}/platforms/android-8/arch-arm"
PREF="arm-linux-androideabi-"

OUT_DIR="`pwd`/android-build"

# C_FLAGS="-lstdc++ -lsupc++ \
#         -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/include \
#         -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/include \
#         -L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi"

C_FLAGS="       -lc \
                        -lgcc \
                        -lstdc++ -lsupc++ \
                        -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/include \
                        -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/include \
                        -L../jthread-1.3.1/android-build/lib \
                        -L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi"


#set -e

export CC="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}gcc  --sysroot=${SYS_ROOT}"
export CXX="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}g++  --sysroot=${SYS_ROOT}"
export LD="$${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/{PREF}ld  --sysroot=${SYS_ROOT}"
export CPP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}cpp  --sysroot=${SYS_ROOT}"
export AS="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}as"
export OBJCOPY="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}objcopy"
export OBJDUMP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}objdump"
export STRIP="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}strip"
export RANLIB="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}ranlib"
export CCLD="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}gcc  --sysroot=${SYS_ROOT}"
export AR="${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/${PREF}ar"

cmake	\
		-DCMAKE_SYSTEM_NAME="Generic" \
        -DCMAKE_CXX_FLAGS="${C_FLAGS}"\
		-DCMAKE_FIND_ROOT_PATH="${SYS_ROOT}" \
		-DJTHREAD_INCLUDE_DIRS="../JThread-Android/android-build/include" \
        -DJTHREAD_LIBRARIES="../JThread-Android/android-build/lib" \
        -DCMAKE_PREFIX_PATH="../JThread-Android/android-build" \
		-DCMAKE_INSTALL_PREFIX="${OUT_DIR}" \
		-G "MSYS Makefiles" .
		
		

/D/Software/Develop/Android/android-ndk-r8e/prebuilt/windows/bin/make.exe
/D/Software/Develop/Android/android-ndk-r8e/prebuilt/windows/bin/make.exe install

cd ${OUT_DIR}/lib && \
# ${AR} -x libjthread.a && \
# ${CXX} ${C_FLAGS} -shared -Wl,-soname,libjthread.so -o libjthread.so  *.obj && \
${AR} -x libjrtplib.a && \
${CXX} ${C_FLAGS} -shared -Wl,-soname,libjrtplib.so -o libjrtplib.so  *.obj && \
rm *.obj

exit 0


运行android.sh即可生成库文件。


最后生成的动态和静态链接库文件就可以用到android项目里面做开发了。

下一步,android调用jrtplib与pc机进行通讯。


你可能感兴趣的:(android,开源,NDK,JRtplib,jthread)