Autoconf 项目允许您指定与环境变量一起使用的工具链。例如,以下示例展示了如何在 Linux 上使用 API 级别 21 的 minSdkVersion 为 Android x86-64 构建 libpng。
# Check out the source.
gitclonehttps://github.com/glennrp/libpng-bv1.6.37
cdlibpng# Only choose one of these, depending on your build machine...exportTOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
exportTOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64# Only choose one of these, depending on your device...exportTARGET=aarch64-linux-android
exportTARGET=armv7a-linux-androideabi
exportTARGET=i686-linux-android
exportTARGET=x86_64-linux-android# Set this to your minSdkVersion.exportAPI=21# Configure and build.exportAR=$TOOLCHAIN/bin/llvm-ar
exportCC="$TOOLCHAIN/bin/clang--target=$TARGET$API"
exportAS=$CCexportCXX="$TOOLCHAIN/bin/clang++--target=$TARGET$API"
exportLD=$TOOLCHAIN/bin/ld
exportRANLIB=$TOOLCHAIN/bin/llvm-ranlib
exportSTRIP=$TOOLCHAIN/bin/llvm-strip
./configure--host$TARGET
make
# Check out the source.
gitclonehttps://gitlab.com/bzip/bzip2.git
cdbzip2
# Only choose one of these, depending on your build machine...exportTOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
exportTOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...exportTARGET=aarch64-linux-android
exportTARGET=armv7a-linux-androideabi
exportTARGET=i686-linux-android
exportTARGET=x86_64-linux-android
# Set this to your minSdkVersion.exportAPI=21# Build.
make\CC="$TOOLCHAIN/bin/clang--target=$TARGET$API"\AR=$TOOLCHAIN/bin/llvm-ar\RANLIB=$TOOLCHAIN/bin/llvm-ranlib\bzip2
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-08-22。"],[],[],null,["# Use the NDK with other build systems\n\n| **Note:** The content described on this page requires at least NDK r19. If you're using an older NDK, consider upgrading. If you're unable to upgrade, use `\u003cNDK\u003e/build/tools/make_standalone_toolchain.py`.\n\nThe NDK contains official support for [ndk-build](/ndk/guides/ndk-build) and [CMake](/ndk/guides/cmake). Most users should\nrefer to one of those guides for building application code. The purpose of\nthis document is to describe how to build existing code that uses other build\nsystems. This is often the case with third-party dependencies that are not\nAndroid-specific, such as OpenSSL and libbzip2.\n\nBuild system maintainers looking to add native NDK support to their build\nsystems should instead read the [Build System Maintainers Guide](https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md).\n\nOverview\n--------\n\nThe Clang compiler in the NDK is useable with only minimal configuration\nrequired to define your target environment.\n\nTo ensure that you build for the correct architecture, pass the appropriate\ntarget with `-target` when invoking Clang. For example, to compile for 64-bit\nARM Android with a `minSdkVersion` of 21, do the following: \n\n $ $NDK/toolchains/llvm/prebuilt/$HOST_TAG/bin/clang++ \\\n --target=aarch64-linux-android21 foo.cpp\n\nAlternatively, there are target-prefixed entry-points for Clang. These may be\neither symlinks or scripts that forward to clang, depending on the NDK release\nand host OS. Invoking Clang directly with `--target` will be more reliable, as\nthat is the most tested workflow, and there are occasionally argument forwarding\nbugs in the scripts. On Windows, the extra `CreateProcess` needed to forward\nfrom the script to the real compiler could potentially have a noticeable\nnegative impact on build speed. \n\n $ $NDK/toolchains/llvm/prebuilt/$HOST_TAG/bin/aarch64-linux-android21-clang++ \\\n foo.cpp\n\nIn both cases, replace `$NDK` with the path to the NDK and `$HOST_TAG` to match\nthe NDK you downloaded according to the following table:\n\n| NDK OS Variant | Host Tag |\n|----------------|------------------|\n| macOS | `darwin-x86_64` |\n| Linux | `linux-x86_64` |\n| 64-bit Windows | `windows-x86_64` |\n\n| **Note:** Despite the x86_64 tag in the Darwin name, those are fat binaries that include M1 support. The paths were not updated to reflect that support because doing so would have broken existing builds that encode those paths.\n\nThe format of the prefix or target argument here is the target triple with a\nsuffix denoting the `minSdkVersion`. This suffix is only used with\nclang/clang++; the binutils tools (such as `ar` and `strip`) do not require a\nsuffix because they are unaffected by `minSdkVersion`. Android's supported\ntarget triples are as follows:\n\n| ABI | Triple |\n|-------------|----------------------------|\n| armeabi-v7a | `armv7a-linux-androideabi` |\n| arm64-v8a | `aarch64-linux-android` |\n| x86 | `i686-linux-android` |\n| x86-64 | `x86_64-linux-android` |\n\n| **Note:** For 32-bit ARM, the compiler is prefixed with `armv7a-linux-androideabi`, but the binutils tools are prefixed with `arm-linux-androideabi`. For other architectures, the prefixes are the same for all tools.\n\nMany projects' build scripts will expect GCC-style cross compilers where each\ncompiler targets only one OS/architecture combination and so may not handle\n`-target` cleanly. In these cases, you can typically include the `-target`\nargument as part of the compiler definition (e.g. `CC=\"clang -target\naarch64-linux-android21`). In rare cases where the build system you're using is\nnot able to use that form, use the triple-prefixed Clang binaries.\n\nAutoconf\n--------\n\n| **Caution:** Autoconf projects are generally not buildable on Windows. Windows users can build these projects using the Linux NDK in a Linux VM. The [Windows Subsystem for Linux](/ndk/guides/specifically%20WSL2) may also work, but is not officially supported. WSL1 is known not to work.\n\nAutoconf projects allow you to specify the toolchain to use with environment\nvariables. For example, the following shows how to build `libpng` for Android\nx86-64 with a `minSdkVersion` of API level 21, on Linux. \n\n # Check out the source.\n git clone https://github.com/glennrp/libpng -b v1.6.37\n cd libpng\n # Only choose one of these, depending on your build machine...\n export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64\n export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64\n # Only choose one of these, depending on your device...\n export TARGET=aarch64-linux-android\n export TARGET=armv7a-linux-androideabi\n export TARGET=i686-linux-android\n export TARGET=x86_64-linux-android\n # Set this to your minSdkVersion.\n export API=21\n # Configure and build.\n export AR=$TOOLCHAIN/bin/llvm-ar\n export CC=\"$TOOLCHAIN/bin/clang --target=$TARGET$API\"\n export AS=$CC\n export CXX=\"$TOOLCHAIN/bin/clang++ --target=$TARGET$API\"\n export LD=$TOOLCHAIN/bin/ld\n export RANLIB=$TOOLCHAIN/bin/llvm-ranlib\n export STRIP=$TOOLCHAIN/bin/llvm-strip\n ./configure --host $TARGET\n make\n\nThe tools selected in this sample are correct for NDK r22 and newer. Older NDKs\nmay require different tools.\n\nNon-autoconf make projects\n--------------------------\n\n| **Caution:** Not all make projects support cross compiling, and not all do so in the same way. It is very likely that the project will not build without modifications. In those cases, refer to the [Build System Maintainers Guide](https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md) for instructions on porting the build to Android.\n\nSome makefile projects allow cross compilation by overriding the same variables\nthat you would with an autoconf project. As an example, the following shows how\nto build `libbzip2` for Android x86-64 with a `minSdkVersion` of 21. \n\n # Check out the source.\n git clone https://gitlab.com/bzip/bzip2.git\n cd bzip2\n\n # Only choose one of these, depending on your build machine...\n export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64\n export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64\n\n # Only choose one of these, depending on your device...\n export TARGET=aarch64-linux-android\n export TARGET=armv7a-linux-androideabi\n export TARGET=i686-linux-android\n export TARGET=x86_64-linux-android\n\n # Set this to your minSdkVersion.\n export API=21\n\n # Build.\n make \\\n CC=\"$TOOLCHAIN/bin/clang --target=$TARGET$API\" \\\n AR=$TOOLCHAIN/bin/llvm-ar \\\n RANLIB=$TOOLCHAIN/bin/llvm-ranlib \\\n bzip2\n\nThe tools selected in this sample are correct for NDK r22 and newer. Older NDKs\nmay require different tools."]]