[時間:2016-05] [狀態:Open]
輸出消息
由於Android.mk使用的GNU Make的語法,可以方便的使用。ndk提供了一下三種格式的消息輸出:
- error: debug print + stop the build (輸出信息並停止構建)
- info: basic debug print (僅輸出消息)
- warning: same as info but displays the line number where it's been inserted (輸出消息並顯示當前行數)
具體用法如下:
$(error this is the error message that will stop the build process)
$(warning this the warning msg)
$(info this the info msg)
參考How to print a var in ndk-build
僅構建32位靜態庫、動態庫、可執行文件
Android.mk支持通過下面變量配置僅編譯32位。
LOCAL_32_BIT_ONLY := true
或者使用下面配置
TARGET_PREFER_32_BIT := true
或者使用LOCAL_MULTILIB變量配置32位、64位構建(會覆蓋全局的TARGET_PREFER_32_BIT)。其值可選擇:
- "both": build both 32-bit and 64-bit.
- "32": build only 32-bit.
- "64": build only 64-bit.
- "first": build for only the first arch (32-bit in 32-bit devices and 64-bit in 64-bit devices).
- "": the default; the build system decides what arch to build based on the module class and other LOCAL_ variables, such as LOCAL_MODULE_TARGET_ARCH, LOCAL_32_BIT_ONLY, etc.
參考Understanding 64-bit Builds(Android)
LOCAL_LDLIBS LOCAL_LDFLAGS LOCAL_SHARED_LIBRARIES區別在哪?
關於Android.mk中這三個變量的說明,找了下有幾篇文章內容類似,比如http://blog.sina.cn/dpool/blog/s/blog_5da93c8f0102vdpc.html、android集成第三方靜態庫的編譯方法、Android.mk的用法和基礎&&m、mm、mmm編譯命令,但說實話內容都差不多,沒有介紹更詳細的。
還是直接看英文的吧。
先看"Secrets of Android.mk"中的這三個變量的介紹:
LOCAL_SHARED_LIBRARIES
These are the libraries you directly link against. You don't need to pass transitively included libraries. Specify the name without the suffix。
用於指定需要直接鏈接的庫,庫的名字不需要后綴。同時會生成依賴關系,當庫不存在時會去編譯這個庫。
具體用法如下:
LOCAL_SHARED_LIBRARIES := \
libutils \
libui \
libaudio
LOCAL_LDLIBS
LOCAL_LDLIBS allows you to specify additional libraries that are not part of the build for your executable or library. Specify the libraries you want in -lxxx format; they're passed directly to the link line. However, keep in mind that there will be no dependency generated for these libraries. It's most useful in simulator builds where you want to use a library preinstalled on the host. The linker (ld) is a particularly fussy beast, so it's sometimes necessary to pass other flags here if you're doing something sneaky.
鏈接的庫不產生依賴關系,一般用於不需要重新編譯的庫,如庫不存在,則會報錯找不到。如果某一個庫既有動態庫又有靜態庫,那么鏈接的是動態庫而非靜態庫。
用法如下:
LOCAL_LDLIBS += -lcurses -lpthread
LOCAL_LDLIBS += -Wl,-z,origin
LOCAL_LDFLAGS
You can pass additional flags to the linker by setting LOCAL_LDFLAGS. Keep in mind that the order of parameters is very important to ld, so test whatever you do on all platforms.
傳遞給鏈接器一個一些額外的參數,比如想傳遞給外面的庫和庫路徑給ld,或者傳遞給ld linker的一些鏈接參數,-On,-EL{B}(大小端字節序)。
LOCAL_LDFLAGS += $(LOCAL_PATH)/lib/libavcodec.a
從stackoverflow中可以看到以下更詳細的說明:
LOCAL_LDLIBS and LOCAL_SHARED_LIBRARIES are both used to link libraries. However LOCAL_SHARED_LIBRARIES is looking for intermediate objects and if not found the library is being rebuilt.
LOCAL_LDLIBS expects to find final library.
They both work under SDK and NDK.
The main differences are the following:(LOCAL_LDLIBS vs. LOCAL_LDFLAGS)
LOCAL_LDFLAGS appear before the list of object files and libraries on the final linker command-line, this is where you want to put actual "flags" that affect linker behaviour.
LOCAL_LDLIBS appear after the list of object files and libraries on the final linked command-line, this is where you want to put actual system library dependencies.
The distinction exists because of the way static linking works on Unix, i.e. the order of object files, static libraries and shared libraries is very important to determine the final result, and sometimes you really to ensure that something appears before / after the other
所以在使用的時候建議按照下面規則:
- Put real linker flags into LOCAL_LDFLAGS(將實際需要編譯鏈接的符號放到LOCAL_LDFLAGS中)
- Put system library dependencies into LOCAL_LDLIBS (將對系統庫的依賴放到LOCAL_LDLIBS中)
- Only use LOCAL_LDLIBS for system library dependencies. If you want to point to another library, it's much better to list them in either LOCAL_STATIC_LIBRARIES and LOCAL_SHARED_LIBRARIES (even if this means defining a PREBUILT_XXX module for them), because this lets the build system work out dependencies and ordering automatically for you.(LOCAL_LDLIBS僅用於指定系統庫的依賴性。如果需要其他庫,建議使用LOCAL_STATIC_LIBRARIES/LOCAL_SHARED_LIBRARIES)
參考:
