安卓系统编写一个动态链接库和测试文件的mk文件编写

在安卓系统下,编写一个动态链接库 .so  文件的Android.mk文件如下:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

# This is the target being built.
LOCAL_MODULE:= librpc
LOCAL_MODULE_TAGS := optional

# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
	librpc.cpp

LOCAL_LDLIBS += -llog

# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
	liblog

# Also need the JNI headers.
#LOCAL_C_INCLUDES += \
#	librpc.h

# No special compiler flags.
LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-sign-compare \
		-Wno-format -Wno-int-conversion -Wno-implicit-function-declaration -Wno-macro-redefined \
		-Wno-pointer-sign -Wno-missing-declarations -Wno-uninitialized

LOCAL_PRELINK_MODULE := false

include $(BUILD_SHARED_LIBRARY)

其中,注意几点:

1. 库的名字,编译的时候,不会主动在前边添加一个 lib, 所以要起名为 libxxx

2. 如果在 c或者 cpp文件里已经include 了 头文件,就没有必要在mk文件里写上了。

3. 具体编成一个静态库还是动态库,是通过最后的 

        include $(BUILD_SHARED_LIBRARY) 来决定的。

4. 头文件里,要有共享库给外边调用的函数的定义。

下面是编写测试代码的 mk 文件的编写;这个测试代码,是用来测试上面so库的;是一个可执行二进制文件,mk文件如下:

include $(CLEAR_VARS)
LOCAL_SRC_FILES := rpc_test.c
LOCAL_MODULE := rpc_test
LOCAL_STATIC_LIBRARIES := libcutils liblog
LOCAL_SHARED_LIBRARIES := librpc
LOCAL_FORCE_STATIC_EXECUTABLE := false
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -DRPC_TEST -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-format -Wno-implicit-function-declaration \
                -Wno-pointer-sign -Wno-missing-declarations -Wno-unused-function -Wno-uninitialized
include $(BUILD_EXECUTABLE)
#include $(BUILD_SHARED_LIBRARY)

其中注意几点:

1. 必须包含上面的共享库,不然没法链接,如下:

        LOCAL_SHARED_LIBRARIES := librpc

2.LOCAL_FORCE_STATIC_EXECUTABLE := false

        这行必须为 false, 或者不写;不能为 true.

3. LOCAL_CFLAGS := -DRPC_TEST -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-format -Wno-implicit-function-declaration \
                -Wno-pointer-sign -Wno-missing-declarations -Wno-unused-function -Wno-uninitialized

        上面这行,是忽略编译过程中产生的不重要错误警告的;

        例如:有一些定义的变量没有用到,编译时就会报错误而退出;这时如果有下面标志,则不会再报错误;

        LOCAL_CFLAGS := -DRPC_TEST -Wno-unused-parameter

        其它的例如,变量没有初始化,参数没有用到等,都是这种情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值