使用C4droid和botbrew在andriod手机上编程

简单介绍如何安装使用C4droid和botbrew,以及如何在手机上用它们编写运行程序。
注意:
  1:C4droid和botbrew都需要手机拥有root权限才可以正常运行。
  2:本文章绝不提供任何破解程序的下载!

  Author       : He YiJun  (storysnail<at>gmail.com QQ:363559089)
  Develop Team : 7fane Team
  Editor       : Yang QiuXi
  Art Designer : He YiJun
  License      : 7fane Team  License 1.0
  Last Update  : 2013-03-09

 

一:使用C4droid

  1:
    到google play搜索C4droid,然后下载安装,现在最新的版本是3.98,当然这么好的软件一定要收些费用的,目前的价格是¥18.61。安装好C4droid点图标运行,它会提示要下载gcc、sdl、QT的插件,按照提示到google play下载和安装。
  2:
    c4driod自己的介绍文档,是英文的,很简单也比较全面,懒得翻译了,自己看吧!
C4droid is a C/C++ IDE + C/C++ compiler for Android with GNU Makefile, SDL and Qt support. C4droid supports devices with ARM processors only (not devices with Intel x86 or MIPS processor).
You can create your own applications on Android phone, run them (even without Internet access: compiler is offline) and export executables (for terminal apps) or APK (for apps with GUI usage). This app uses TCC and uClibc (or GCC with Bionic libc with a plugin), so it has full support of ANSI C and ISO C99. C4droid can be used for educational purposes or to practice in C/C++ language.
C4droid supports syntax highlighting, code completion and source code formatting, so it is a very handy tool for programming on the go.

To get C++ support you need:
0) Enough internal memory OR root rights. C4droid doesn't require root rights for devices with more than 50MB of free internal memory.
1) Install GCC plugin (C4droid will ask you to install it).
2) Select G++ compiler in C4droid preferences.
3*) Use iostream, not iostream.h
4*) Add "using namespace std;" to your program (before int main)

C4droid contains a Terminal Emulator with BusyBox, so you can use Makefiles without any changes, shell commands syntax is the same as on Linux PC.
To compile Makefile projects, open any file in the same folder with Makefile and long-click "Compile" to configure current directory (C4droid will create .c4droid configuration file when you'll press Ok). After configuring folder, you can press compile button and C4droid will build your project.
Don't forget to enter executable name and select compilation mode (and press ok also, else changes will be discarded)

C4droid supports SDL, NativeActivity and Qt for GUI now, install SDL plugin for C4droid to use it (only GCC/G++ compiler is supported, so you need GCC plugin).
(SDL is detected with #include "SDL.h", NativeActivity is detected with #include "android_native_app_glue.h", Qt is detected with #include "QtGui").
There is an experimental support for Qt using necessitas, but note that it's in alpha stage.
Qt support limitations:
1) Path to Ministro libraries is hardcoded (/data/data/org.kde.necessitas.ministro/files/qt/lib), so email me if it will change.
2) If you're compiling a single-file Qt app, you must add #include "yoursourcefilenamewithcpp.moc" to the end of file. If you're using .pro file for compilation, you don't need to do that.

Quick manual:
If you have found a bug or app is not working, email me.

Long click actions:  长按某一按键会有第二功能
Save button: save as.
Open button: recent files.
Run button: run with arguments.
Compile button: configure current directory.
Tab: close tab.

Result of compilation (binary or APK) can be exported using menu->export.
If you have "Illegal instruction" error, using internal compiler (TCC), install GCC plugin, GCC supports softfloat (by default).

C4droid will check license via the Internet.

Report bugs at n0n3m4@gmail.com.

Some components of this app are licensed under (L)GPL, email me to get the source code.
Android is a trademark of Google Inc.
Qt is a registered trademark of Digia.

  3:
    相信很多ChineseMan会使用网络上流传的破解版,不过那样做真的没有必要,因为本文第二部分就会介绍botbrew!也许有人会诡辩说botbrew写不了QT和SDL程序,那好,我建议你可以玩玩botbrew basil!

  4:
    我在手机上写了两个示例程序,get-www是一个单文件程序,与前几天写的《使用Code::blocks在windows下写网络程序》中的示例程序功能相同(链接地址是http://www.7fane.com/?p=248)。function-fetch是一个多文件程序,它的作用是从一个指定文件中检索出特定的内容。我使用C4droid和botbrew都成功编译运行了这两个示例程序,下面是在C4droid中的运行截图。示例程序的源代码会附在本文末尾。
get-www示例程序截图

c4-001 c4-002 c4-003 c4-004

 

function-fetch示例程序截图

c4-005 c4-006 c4-007 c4-008

二:使用botbrew

  1:
    到google play搜索botbrew,然后下载安装,botbrew是开源软件,可以访问其官方网址http://botbrew.com/来了解更多信息,当前版本为1.0.1.7,安装好botbrew点图标运行。
   botbrew-001
    botbrew的主界面和程序管理器类似,在其中可以选择安装很多程序,像gcc、python、perl、lua、lynx、make等。安装好所需软件后,进入终端模拟器(终端模拟器有很多,选择一个你喜欢的下载安装即可,有些android系统会默认带一个)。输入su,取得root权限,然后再输入botbrew,然后就可以输入软件的名字来运行了,不过因为是在root下,所以这种方法是非常危险的!你可以用botbrew建立普通账户来运行,详情请参考borbrew手册http://botbrew.com/manual.htm
说到botbrew就不得不提botbrew basil,它是botbrew的升级版,但目前还处于开发状态。它可以在手机上安装一个debian环境,你可以直接下载安装debian到data分区或sdcard,安装完一个35mb左右的debian环境后就可以安装各种软件了,gcc,gdb,python,jdk,Xserver(使用前需先关掉安卓的屏幕刷新setprop ctl.stop surfaceflinger),lxde。显然手机已然变成一台计算机了!打开终端模拟器,输入
chroot /botbrew/bin/bash
然后就进入了支持高亮的bash!

  2:使用botbrew的gcc编译和运行程序
    昨天Ling向我提出了抗议,说写了一个sqlite-test的测试程序,也成功编译了该程序,可偏偏无法运行,还会提示没有权限!

#./sqlite-test 
./sqlite-test: permission denied 

    然后她尝试了各种方法,用chmod修改权限,用chown修改用户和组,用export修改PATH等,不过都失败了!实际上问题并没有那么复杂,出现这个情况的原因是android只允许在特定文件夹下运行程序,例如/data,所以你需要获得系统的root权限来访问/data文件夹!如果Ling想使用botbrew的gcc编译和运行程序,那么就应该像下面这样做。

$su            //启动终端模拟器后用su命令提升权限 
#botbrew       //启动botbrew程序 
#cd /data      //进入/data目录 
#mkdir ling    //新建一个名为ling的目录,这样方便管理 
#cd ling       //进入ling 
#mv /sdcard/sqlite-test ./    //将sqlite-test目录以及其下的源文件移动到ling下 
#cd sqlite-test   //进入sqlite-test测试程序的目录 
#make             //编译测试程序 
#./sqlite-test    //测试程序就可以正常运行了! 

    有些朋友在看过下面的图片时可能会有疑问,不是说android只允许在特定文件夹下运行程序吗,可你的图片却显示你是直接在sdcard里面运行的测试程序!嗯,关于这个问题我只能回答“开源真好,在自己建造的系统里可以为所欲为!”

function-fetch示例程序截图

botbrew-002 botbrew-003 botbrew-004 botbrew-005 botbrew-006

 

get-www示例程序截图

botbrew-007 botbrew-008

 

三:两个示例程序的源代码

get-www示例程序源代码

/********************************************************************************
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
    get-www
                               main.c

    Develop Team     :    7fane Team
    Main Programmer  :    He YiJun  (storysnail<at>gmail.com QQ:363559089)
    License          :    7fane Team  License 1.0
    Last Update      :    2013-03-03
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
*********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

static int gw_connect(char *domain,int port)
{
  int sock_sd;
  int i;
  struct hostent *site_dns;
  struct sockaddr_in s_addr;
  site_dns = gethostbyname(domain);
  if(site_dns == NULL) {
    printf("gethostbyname error!\n");
    return -2;
  }
  printf("default ip:  %s\n",inet_ntoa(*((struct in_addr *)site_dns->h_addr)));
  for(i=0; i< site_dns->h_length/sizeof(int); i++) {
    printf("IP:%d:%s\n",i+1,inet_ntoa(*((struct in_addr *)site_dns->h_addr_list[i])));
  }
  sock_sd = socket(AF_INET,SOCK_STREAM,0);
  if(sock_sd < 0) {
    printf ("socket error!");
    return -1;
  }
  memset(&s_addr,0,sizeof(struct sockaddr_in));
  memcpy(&s_addr.sin_addr,site_dns ->h_addr_list[0],site_dns->h_length);
  s_addr.sin_family = AF_INET;
  s_addr.sin_port = htons(port);
  printf("s_addr ip:  %s",inet_ntoa(*((struct in_addr *)&s_addr.sin_addr)));
  return (connect(sock_sd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr))  < 0 ? -1 : sock_sd);
}

static int gw_send(int sock_sd,char *fmt,...)
{
  char buf [1024];
  va_list argptr;
  va_start(argptr,fmt);
  vsprintf(buf,fmt,argptr);
  va_end(argptr);
  printf("Send:\n%s\n",buf);
  return send(sock_sd,buf,strlen(buf),0);
}

void main(int argc,char **argv)
{
  int sock_sd;
  char rBuf[3];
  sock_sd = gw_connect("www.7fane.com",80);
  if(sock_sd < 0)  {
    printf("connect error!\n");
    return;
  }
  //注意:该网站只用于个人测试,在2013年11月末到期,
  //如果你在之后的日期使用,请使用其它网页地址
  gw_send(sock_sd,"GET http://www.7fane.com/test.html\n");   
  gw_send(sock_sd,"%c",10);
  while(read(sock_sd,rBuf,1) > 0)
    printf("%c",rBuf[0]);
  close(sock_sd);
  return;
}

function-fetch示例程序源代码

/******************************************************************************** 
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 

    function-fetch 
                               main.c 

    Develop Team     :    7fane Team 
    Main Programmer  :    He YiJun  (storysnail<at>gmail.com QQ:363559089) 
    License          :    7fane Team  License 1.0 
    Last Update      :    2013-03-03 


* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*********************************************************************************/ 

#include <stdio.h> 
#include <stdint.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <sys/param.h>    
#include <netinet/in.h> 
#include "ff-read.h"    

int main (int argc, char *argv[]) 
{ 
  char filename[MAXPATHLEN]; 
  int name_len; 

  switch (argc) { 
    case 1: 
      printf ("please input the dictionary's name: "); 
      if (!fgets (filename, MAXPATHLEN,stdin)) { 
        perror ("get string error"); 
        exit(1); 
      } 
      break; 
    case 2: 
      if (!strncpy (filename, argv[1], MAXPATHLEN)) { 
        perror ("copy string error"); 
        exit (1); 
      } 
      break; 
    default: 
      fprintf (stderr, "Usage: %s [dictionary name]\n", argv[0]); 
      exit(1); 
      break; 
  } 
  name_len = strlen (filename); 
  if (filename[name_len - 1] == '\n') { 
    filename[name_len - 1] = '\0'; 
    name_len--; 
  }

  strncpy (filename + name_len, ".txt", MAXPATHLEN - name_len); 
  printf("filename:  %s\n",filename); 
  ff_PrintInfo (filename); 

    return 0; 
} 

/******************************************************************************** 
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 

                               ff-read.c 

    Develop Team     :    7fane Team 
    Main Programmer  :    He YiJun  (storysnail<at>gmail.com QQ:363559089) 
    License          :    7fane Team  License 1.0 
    Last Update      :    2013-03-03 


* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*********************************************************************************/ 
#include <stdio.h> 
#include <stdint.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <sys/param.h> 
#include <netinet/in.h> 
#include "ff-read.h" 

int ff_PrintInfo (char *filename) 
{ 
  char buf[1024+1]; 
  char keyword[100]; 
  FILE * info_fd; 

  memset(buf,'\0',1024+1); 
  memset (keyword, '\0', 100); 

  info_fd = fopen (filename, "rb"); 
  if (info_fd == NULL) { 
    perror ("Open information file error"); 
    return(0); 
  } 

  while(!feof(info_fd)) { 
    if (!fgets (buf, 1024, info_fd)) { 
      break; 
    } 
    if (!strncmp (buf, "[ff-keyword]=", sizeof ("[ff-keyword]=") - 1)) { 
      sscanf (buf, "[ff-keyword]=%s\n", keyword); 
       printf ("keyword is %s\n", keyword); 
    } 
  } 

  fclose (info_fd); 
  return 1; 
} 

/******************************************************************************** 
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 

                               ff-read.h 

    Develop Team     :    7fane Team 
    Main Programmer  :    He YiJun  (storysnail<at>gmail.com QQ:363559089) 
    License          :    7fane Team  License 1.0 
    Last Update      :    2013-03-03 


* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*********************************************************************************/ 

#ifndef __FF_READ_H__ 
#define __FF_READ_H__ 

int ff_PrintInfo (char *filename); 

#endif 

[sourcecode language="bash"] 


/******************************************************************************** 
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 

#   /***************************************************************************** 
#   For function-fetch 
#   Develop Team :    7fane Team 
#   Programmer   :    He YiJun  (storysnail<at>gmail.com QQ:363559089)   
#   License      :    7fane Team  License 1.0 
#   Last Update  :    2013-03-03 

* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*********************************************************************************/ 
all:main 
CC=gcc 
CFLAGS = -Wall -ggdb 

OBJFILES = main.o ff-read.o 

main:$(OBJFILES) 
    $(CC) $(CFLAGS) $(OBJFILES) -o main 

main.o:main.c ff-read.h 
    $(CC) $(CFLAGS) -c main.c -o main.o 

ff-read.o:ff-read.c ff-read.h 
    $(CC) $(CFLAGS) -c ff-read.c -o ff-read.o 

clean: 
    rm -f *.o *~ main 

 

 

 

 

            7fane Team 协议 1.0  中文版

    当您阅读、理解并愿意遵守以下条款时,您就拥有了获取、使用、复制、分发或通过通信
网络传播7fane Team作品的权利。

1. 7fane Team的作品可以在非商业用途下免费使用。

2. 如果软件提供了源代码,那么你可以更改源代码或软件接口以适应你的应用。

3. 在未获得7fane Team的授权之前,你不可以在商业用途下使用7fane Team的作品,
   也不可以将7fane Team的作品用于营利为目的的活动。关于获得许可,请发送Email
   到storysnail@gmail.com以获取更多信息。

4. 你不得租赁,再许可,出售,转让,抵押7fane Team的作品和服务。

5. 你不得删除或修改7fane Team作品的版权信息和相关的链接,例如网址信息或“关于窗口”
   中的所有信息,除非您已经获得7fane Team的书面授权。

6. 你不可以通过修改7fane Team的作品来获得衍生作品,更不可以重新分配这些衍生作品。

7. 如果您不能遵守本协议,您的许可将被终止,您必须停止使用并删除7fane Team作品及其
   副本,并且不可以再继续获取、使用、复制、分发或通过通信网络传播7fane Team作品。

8. 7fane Team拥有并保留修改本协议的权利和在本协议修改后不另行通知的权利。修改后
   的新协议将适用于新的许可用户。

9. 7fane Team作品是作为不提供任何明确的或隐含的赔偿或担保的形式提供的。

10.7fane Team对于使用其作品而生成的任何信息不承担任何责任,也不会对传播或使用
   这些信息承担责任。

11. 用户出于自愿而使用本7fane Team作品,您必须了解使用的风险,我们不承诺对用户提供
   任何形式的技术支持、使用担保,所以你必须承担全部风险。
关于 "7fane Team作品" 的定义:
    "7fane Team作品" 包括文本、图像、音频视频、软件等由7fane Team创作的一切。

    版权所有 (c)2001-2012,7fane Team 保留所有权利。


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值