安裝
android studio下面ndk目錄的clang
export PATH="/Users/chennan/Library/Android/sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/darwin-x86_64/bin:$PATH"
執行命令
預編譯
clang -target arm-linux-android21 -E hello.c -o hello.i
預編譯的文件,就是將導入的頭文件以及宏展開。
編譯
clang -target arm-linux-android21 -S hello.i -o hello.s
這一步生成.S的匯編文件
匯編
clang -target arm-linux-android21 -c hello.s -o hello.o
也可以將匯編文件直接編譯成可執行文件
clang -target arm-linux-android21 arm_hello.s -o arm_hello
arm_hello.s是hello.s的一個副本,刪除了一些偽代碼和注釋以及對結果影響不大的代碼。
鏈接
有的時候會生成多個o文件,這個時候需要將他們鏈接起來生成可執行文件
clang -target arm-linux-android21 hello.o -o hello
之后可以將文件push到手機里面執行了。
如果不需要了解中間過程可以執行下面命令,直接生成可執行的文件。
clang -target arm-linux-android21 hello.c -o hello