linux和Android的Makefile和android.mk
1. makefile
1.1 gcc的参数
-Wall: 是打开警告开关,
-O: 代表默认优化,可选:-O0不优化,-O1低级优化,-O2中级优化,-O3高级优化,-Os代码空间优化。
-g: 是生成调试信息,生成的可执行文件具有和源代码关联的可调试的信息。
-I(大写的i):代表后面跟的目录为第一个寻找头文件的目录
-L: 代表后面跟的目录为第一个寻找库文件的目录
-l(小写的L):表示libxxx.so库文件
-static: 静态编译的
举例:
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
上面这句表示在编译hello.c时:
-I /home/hello/include表示将/home/hello/include目录作为第一个寻找头文件的目录,寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include
-L /home/hello/lib表示将/home/hello/lib目录作为第一个寻找库文件的目录,寻找的顺序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib
-lworld表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了“-static”表示寻找libworld.a静态库文件)
-c(小写): 只编译,不链接
-C(大写): 切换到指定目录再执行 make 过程,makefile 在这个指定目录里面,然后返回到当前目录继续读入、执行当前的Makefile。
2. android.mk
2.1 基本概念
可以生成一下几个类别:
JAVA代码的 | C/C++代码的 |
JAR包 | 二进制可执行文件:ELF (BUILD_EXECUTABLE) |
应用程序:APK | 动态库:.so (BUILD_SHARED_LIBRARY) |
静态库:.a (BUILD_STATIC_LIBRARY) |
常用的变量:
名字 |
含义 | 常用的方式 |
LOCAL_PATH |
当前模块的相对路径 | LOCAL_PATH := $(call my-dir) |
LOCAL_MODULE
|
编译生成的目标名称 | LOCAL_MODULE := test |
LOCAL_SRC_FILES | 编译该模块需要的源文件 | LOCAL_SRC_FILES := test.c |
LOCAL_C_INCLUDES |
包含C的头文件目录 | |
LOCAL_SHARED_LIBRARIES | 导入共享库 | LOCAL_SHARED_LIBRARIES += liblog |
2.2 C/C++可执行文件(ELF)
hejin@desk-ubuntu:/home/desk/myandroid$ ls external/test/ Android.mk test2.c test2.h test.c
分别查看四个源文件的内容:
hejin@desk-ubuntu:/home/desk/myandroid$ cat external/test/test.c #include <stdio.h> #include "test2.h" int main(){ printf("hello world\n"); test2(); return 0; }
hejin@desk-ubuntu:/home/desk/myandroid$ cat external/test/test2.c #include<stdio.h> void test2(){ printf("hello test2\n"); }
hejin@desk-ubuntu:/home/desk/myandroid$ cat external/test/test2.h void test2();
Android.mk:
hejin@desk-ubuntu:/home/desk/myandroid$ cat external/test/Android.mk LOCAL_PATH := $(call my-dir) #定义了当前模块的相对路径 include $(CLEAR_VARS) #清空当前环境变量, 除了LOCAL_PATH LOCAL_MODULE := test #编译生成的目标名称 LOCAL_SRC_FILES := test.c \ #编译该模块需要的源文件 test2.c include $(BUILD_EXECUTABLE) #编译所生成的目标文件格(在这里是可执行文件)
编译:
1. source build/envsetup.sh
2. lunch
3.
hejin@desk-ubuntu:/home/desk/myandroid$ mmm external/test/ ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=5.1.1 TARGET_PRODUCT=sabresd_6dq TARGET_BUILD_VARIANT=user TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a-neon TARGET_CPU_VARIANT=cortex-a9 TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_OS=linux HOST_OS_EXTRA=Linux-3.16.0-30-generic-x86_64-with-Ubuntu-14.04-trusty HOST_BUILD_TYPE=release BUILD_ID=2.1.0-ga-rc3 OUT_DIR=out ============================================ No private recovery resources for TARGET_DEVICE sabresd_6dq make: Entering directory `/home/desk/myandroid' target thumb C: test <= external/test/test.c target thumb C: test <= external/test/test2.c target Executable: test (out/target/product/sabresd_6dq/obj/EXECUTABLES/test_intermediates/LINKED/test) target Symbolic: test (out/target/product/sabresd_6dq/symbols/system/bin/test) target Strip: test (out/target/product/sabresd_6dq/obj/EXECUTABLES/test_intermediates/test) Install: out/target/product/sabresd_6dq/system/bin/test //将可执行文件安装到这个目录下 make: Leaving directory `/home/desk/myandroid' #### make completed successfully (1 seconds) ####
查看文件类型:(可以看出来最后为ELF的可执行文件类型)
hejin@desk-ubuntu:/home/desk/myandroid$ file out/target/product/sabresd_6dq/system/bin/test out/target/product/sabresd_6dq/system/bin/test: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), stripped
2.3 C/C++可执行程序(ELF)在android下运行
1.将上面的程序从Linux拿到Windows下
2. 放入android系统中
3. 进入android系统执行
2.4 C/C++动态库和静态库
1. 静态库
hejin@desk-ubuntu:/home/desk/myandroid/external/test2$ cat Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libtest LOCAL_SRC_FILES := src/test.c \ src/test2.c include $(BUILD_STATIC_LIBRARY)
hejin@desk-ubuntu:/home/desk/myandroid/external/test2$ mmm . 。。。省略。。。 No private recovery resources for TARGET_DEVICE sabresd_6dq make: Entering directory `/home/desk/myandroid' Import includes file: out/target/product/sabresd_6dq/obj/STATIC_LIBRARIES/libtest_intermediates/import_includes target thumb C: libtest <= external/test2/./src/test.c target thumb C: libtest <= external/test2/./src/test2.c Export includes file: external/test2/./Android.mk -- out/target/product/sabresd_6dq/obj/STATIC_LIBRARIES/libtest_intermediates/export_includes target StaticLib: libtest (out/target/product/sabresd_6dq/obj/STATIC_LIBRARIES/libtest_intermediates/libtest.a) make: Leaving directory `/home/desk/myandroid'
2. 动态库
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libtest LOCAL_SRC_FILES := src/test.c \ src/test2.c include $(BUILD_SHARED_LIBRARY)
2.5 添加头文件,系统库,第三方库,静态库
2.6 编译JAVA(未写)