目前android studio 還不支持 ndk開發(fā),如下圖是官網(wǎng)的說明:
成都創(chuàng)新互聯(lián)公司專注于臨滄網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供臨滄營(yíng)銷型網(wǎng)站建設(shè),臨滄網(wǎng)站制作、臨滄網(wǎng)頁設(shè)計(jì)、臨滄網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造臨滄網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供臨滄網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
由于android studio 目前還是測(cè)試版,所以不建議直接項(xiàng)目開發(fā)時(shí)使用,謝謝!
1. 在./development目錄下創(chuàng)建一目錄 如:myhello
2. 進(jìn)入hello目錄,在其下編寫自己的.c文件,如: myhello.c
#include stdio.h
int main()
{
printf("hello world\n");
exit(0);
//return 0;
}
3. 在hello目錄中,編寫Android.mk, 內(nèi)容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myhelloworld
LOCAL_SRC_FILES := myhello.c
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
4. 回到Android源代碼頂層目錄,進(jìn)行編譯,make myhelloworld
5. 生成的可執(zhí)行文件位于:out/target/product/lotus/system/bin/ 目錄下
6. adb push 到手機(jī) /data 目錄下,然后進(jìn)入adb shell,到data目錄下,執(zhí)行./myhelloworld 皆可
手動(dòng)編譯連接【arm-eabi-gcc 的目錄隨andorid的版本而有變化,還有就是需要鏈接的文件如果比較多時(shí),需要很多-l 就很麻煩了】
7、編譯成目標(biāo)文件:
#$(yourAndroid)/prebuilt/linux-x86/toolchain/[arm-eabi-4.2.1]/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include/ -I bionic/libc/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -g -c helloworld.c -o hello.o
8、生成可執(zhí)行代碼:
#$(yourAndroid)/prebuilt/linux-x86/toolchain/[arm-eabi-4.2.1]/bin/arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o helloworld -Lout/target/product/[generic]/obj/lib -Wl,-rpath-link=out/target/product/[generic]/obj/lib -lc hello.o -entry=main
其中[ ]中部分根據(jù)實(shí)際情況修改
**************************************************
實(shí)驗(yàn):
1. 建目錄(my Android)/development/test, 在該目錄下新建 Android.mk和fb_test.c文件
2. Android.mk文件
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myfbtest
LOCAL_SRC_FILES := fb_test.c
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
3. 以下為fb_test.c
#include fcntl.h
#include linux/fb.h
#include sys/mman.h
#include linux/kd.h
#include stdio.h
#define FBBIT_PER_PIXEL 32
#define FBBIT_PIXEL_IMAGE 16
#define PIXELS_WIDTH_BYTE 4
#define BYTE_PER_PIXEL 3
#define FB_GRAPHICS_PATH "/dev/graphics/fb0"
#define DEV_TTY0_PATH "/dev/tty0"
#define DISPLAY_ERROR -1
#define DISPLAY_SUCCESS 0
#define GET_BATTERYCAPACITY_ERR -1
#define MAX_STR 255
static struct {
int fd;
void *pixels;
struct fb_fix_screeninfo fixed;
struct fb_var_screeninfo var;
int align_xres;
} fb;
int getBatteryCapacity(void)
{
FILE *in;
char tmpStr[MAX_STR + 1];
char capfile[] = "/sys/class/power_supply/battery/capacity";
if (capfile == NULL)
return GET_BATTERYCAPACITY_ERR;
in = fopen(capfile, "rt");
if (in == NULL)
return GET_BATTERYCAPACITY_ERR;
if (fgets(tmpStr, MAX_STR, in) == NULL) {
printf("Failed to read battery capacity!\n");
fclose(in);
return GET_BATTERYCAPACITY_ERR;
}
printf("Battery capacity(ascii): %s\n", tmpStr);
fclose(in);
return 0;//atoi(tmpStr);
}
static int vt_set_graphicsmode(int graphics)
{
int fd, r;
fd = open(DEV_TTY0_PATH, O_RDWR | O_SYNC);
if (fd 0)
return DISPLAY_ERROR;
r = ioctl(fd, KDSETMODE, graphics);
close(fd);
return r;
}
int display_init(void)
{
fb.fd = open(FB_GRAPHICS_PATH, O_RDWR);
if (fb.fd 0)
return DISPLAY_ERROR;
if (ioctl(fb.fd, FBIOGET_FSCREENINFO, fb.fixed) 0)
return DISPLAY_ERROR;
if (ioctl(fb.fd, FBIOGET_VSCREENINFO, fb.var) 0)
return DISPLAY_ERROR;
fb.align_xres = fb.fixed.line_length /
(fb.var.bits_per_pixel BYTE_PER_PIXEL);
fb.pixels = mmap(0, fb.fixed.line_length * fb.var.yres_virtual,
PROT_READ | PROT_WRITE, MAP_SHARED, fb.fd, 0);
if (fb.pixels == MAP_FAILED)
return DISPLAY_ERROR;
vt_set_graphicsmode(KD_GRAPHICS);
memset(fb.pixels, 0, fb.fixed.line_length * fb.var.yres_virtual);
//display_update(fb.pixels, fb.align_xres, fb.var.yres);
fb.var.activate = FB_ACTIVATE_FORCE;
ioctl(fb.fd, FBIOPUT_VSCREENINFO, fb.var);
printf("display_init ok\n");
return DISPLAY_SUCCESS;
}
void display_on(void)
{
ioctl(fb.fd, FBIOBLANK, FB_BLANK_UNBLANK);
}
void display_off(void)
{
ioctl(fb.fd, FBIOBLANK, FB_BLANK_POWERDOWN);
}
int main()
{
display_init();
display_off();//關(guān)顯示屏
getBatteryCapacity();
sleep(5);
display_on();//開顯示屏
return 0;
}
C4droid\x0d\x0a\x0d\x0aC4droid是一個(gè)C / C + + IDE + C / C + +編譯器,GNU Makefile文件,SDL和Qt支持的Android。 C4droid支持ARM處理器(而不是設(shè)備與英特爾的x86和MIPS處理器)的設(shè)備。\x0d\x0a您\x0d\x0a可以創(chuàng)建自己的應(yīng)用程序在Android手機(jī)中,運(yùn)行(即使沒有上網(wǎng):編譯器為離線狀態(tài))和出口的可執(zhí)行文件(為終端的應(yīng)用程序)或APK(GUI使用的\x0d\x0a應(yīng)用程序)。此應(yīng)用程序使用TCC和uClibc(GCC仿生libc的一個(gè)插件),所以它有完整的ANSI C和ISO C99支持。 \x0d\x0aC4droid可用于教育目的或在C和C + +語言的練習(xí)。\x0d\x0aC4droid支持語法高亮,代碼完成和源代碼格式,所以它是一個(gè)非常方便的工具,在旅途中進(jìn)行編程。
編譯環(huán)境要求:下載Android的源碼,并執(zhí)行完一次完整的編譯。以下的所有命令均是在編譯后的源碼根目錄下執(zhí)行。
1. 編譯C code
同樣以hello.c為例:
#include stdio.h
#include stdlib.h
int main()
{
printf("hello, world!\n");
return 0;
}
執(zhí)行以下步驟生成動(dòng)態(tài)鏈編的binary文件:
生成目標(biāo)文件:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -include system/core/include/arch/linux-arm/AndroidConfig.h -c -o hello.o hello.c
生成可執(zhí)行程序:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm- eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o hello -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ out/target/product/generic/obj/lib/crtbegin_dynamic.o hello.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
用命令file查看生成的hello文件屬性:
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
可以證明此時(shí)的hello是一個(gè)動(dòng)態(tài)鏈編的文件。
2. 編譯native c++ 代碼
以hello_cpp為例:
hello_cpp.h
#ifndef HELLO_CPP_H
#define HELLO_CPP_H
class Hello
{
public:
Hello();
~Hello();
void printMessage(char* msg);
};
#endif
hello_cpp.cpp
#include stdio.h
#include "hello_cpp.h"
Hello::Hello()
{
}
Hello::~Hello()
{
}
void Hello::printMessage(char* msg)
{
printf("C++ example printing message: %s", msg);
}
int main(void)
{
Hello hello_obj;
hello_obj.printMessage("Hello world!\n");
return 0;
}
執(zhí)行以下命令完成:
編譯目標(biāo)文件:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -include system/core/include/arch/linux-arm/AndroidConfig.h -fno-exceptions -fno-rtti -c -o hello_cpp.o hello_cpp.cpp
編譯可執(zhí)行程序:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm- eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o hello_cpp -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ out/target/product/generic/obj/lib/crtbegin_dynamic.o hello_cpp.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
同樣用file查看hello_cpp的文件屬性:
hello_cpp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
但是很不幸的是,android自帶的toolchain不支持C++標(biāo)準(zhǔn)庫的開發(fā),即所有的std namespace下的類均無法使用,包括基本的string。
新聞名稱:android編譯c,android編譯clang錯(cuò)誤
本文網(wǎng)址:http://aaarwkj.com/article46/dsisghg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站收錄、網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)