Compile android NDK without Eclipse

本文介绍如何使用 Android NDK r8 和 Cygwin 1.7.15-1 在 Windows 环境下编译生成静态库。通过设置环境变量和配置文件,可以在 Android SDK 4.0.3 (API 15) 上创建一个可复用的静态库。

This article's purpose is to generate a static/shared library which can be used in android ndk.

Software Version:

   Cygwin 1.7.15-1

   Android SDK 4.0.3 (API 15)

   Android NDK r8

 


1.Install Android-ndk and Cygwin

Install NDK.Download the zip file and unzip it to the destination folder.my is D:\Android\android-ndk-r8

  Edit file \cygwin\home\user_name\.bash_profile,if you can't find the file in this location,copy the cygwin\etc\skel\.bash_profile to here,add the following to the end: 

复制代码

# Android NDK

  NDK=/cygdrive/<yourdrive>

  export NDK

My:

  ndk=/cygdrive/D/Android/android-ndk-r8

  export ndk

复制代码

  ndk is a custom name ,you can change it.  

  Save and restart cygwin.Go to D:\Android\android-ndk-r8\samples\hello-jni\jni :

   cd $ndk/samples/hello-jni/jni

If you can't use $ndk,use /cygdrive/D/Android/android-ndk-r8

2.Compile

execute command:$ndk/ndk-build or /cygdrive/D/Android/android-ndk-r8/ndk-build in your project folder,make sure you folder like the following:

or you'll get error about directory.You just need to put your c/c++ code in the jni folder and write a Android.mk similar with the demo,about the others,you can copy from the demo.And the you can find the library in libs folder:

Pay attention to these content of google document:

 Required development tools

  • For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
  • A recent version of awk (either GNU Awk or Nawk) is also required.
  • For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.

 If you installed the wrong version,you may get the error of the following pictrue:

   

My project : 

first.c:

#include "first.h"

int  first(int  x, int  y)
{
    return x + y;
}

first.h:

#ifndef FIRST_H
#define FIRST_H

extern int first(int  x, int  y);

#endif /* FIRST_H */

Android.mk:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := libtwolib-first
LOCAL_SRC_FILES := first.c

include $(BUILD_SHARED_LIBRARY)

Then you can use the first method of libtwolib-first.so in your  c/c++ program.

转载于:https://www.cnblogs.com/qiengo/archive/2012/12/20/2826191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值