NDK에는 ndk-build 및 CMake의 공식 지원이 포함되어 있습니다. 대부분의 사용자는 애플리케이션 코드 빌드를 위해 이러한 가이드 중 하나를 참조해야 합니다. 이 문서의 목적은 다른 빌드 시스템을 사용하는 기존 코드를 빌드하는 방법을 설명하는 것입니다. 이는 주로 OpenSSL 및 libbzip2와 같은 Android용이 아닌 제3자 종속 항목에 적용됩니다.
빌드 시스템에 네이티브 NDK 지원을 추가하려는 빌드 시스템 유지관리자는 대신 빌드 시스템 유지관리자 가이드를 참조해야 합니다.
개요
NDK의 Clang 컴파일러는 타겟 환경을 정의하는 데 필요한 최소한의 구성만으로 사용할 수 있습니다.
올바른 아키텍처에 맞게 빌드하려면
Clang을 호출할 때 -target로 타겟팅 예를 들어, 64비트 버전용 컴파일은
minSdkVersion이 21인 ARM Android의 경우 다음을 실행합니다.
또는 Clang의 타겟 접두사 진입점이 있습니다. 이는 다음과 같을 수 있습니다.
NDK 출시에 따라 clang으로 전달되는 심볼릭 링크나 스크립트
호스트 OS를 기반으로 합니다 --target를 사용하여 Clang을 직접 호출하는 것이
이는 가장 테스트된 워크플로이며 인수 전달이 사용되는 경우도 있습니다.
버그와 관련이 있습니다. Windows에서는 전달하는 데 필요한 추가 CreateProcess가
스크립트에서 실제 컴파일러까지
부정적인 영향을 미칩니다.
두 가지 경우 모두 다음 표에 따라 다운로드한 NDK와 일치하도록 $NDK를 NDK 경로 및 $HOST_TAG로 대체하세요.
NDK OS 변형
호스트 태그
macOS
darwin-x86_64
Linux
linux-x86_64
64비트 Windows
windows-x86_64
여기에서 접두사 또는 타겟 인수의 형식은 minSdkVersion을 나타내는 접미사가 포함된 타겟 3줄입니다. 이 접미사는 clang/clang++로만 사용되며 binutils 도구(예: ar 및 strip)는 minSdkVersion의 영향을 받지 않기 때문에 접미사가 필요하지 않습니다. Android에서 지원하는 타겟 3줄은 다음과 같습니다.
ABI
3줄
armeabi-v7a
armv7a-linux-androideabi
arm64-v8a
aarch64-linux-android
x86
i686-linux-android
x86-64
x86_64-linux-android
많은 프로젝트의 빌드 스크립트는 각 컴파일러가 하나의 OS/아키텍처 조합만 타겟팅하는 GCC 스타일 크로스 컴파일러를 기대하므로 -target을 깔끔하게 처리하지 않을 수 있습니다. 이 경우 일반적으로 -target를 포함할 수 있습니다.
인수 (예: CC="clang -target
aarch64-linux-android21)를 컴파일러 정의의 일부로 사용합니다. 드물지만 사용 중인 빌드 시스템이
해당 양식을 사용할 수 없는 경우 접두사가 3줄인 Clang 바이너리를 사용하세요.
Autoconf
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
이 샘플에서 선택한 도구는 NDK r22 이상에서 올바르게 작동합니다. 이전 NDK에는 다른 도구가 필요할 수 있습니다.
Autoconf가 아닌 Make 프로젝트
일부 Makefile 프로젝트는 autoconf 프로젝트와 동일한 변수를 재정의하여 크로스 컴파일을 허용합니다. 예를 들어 다음은 minSdkVersion 21로 Android x86-64용 libbzip2를 빌드하는 방법을 보여줍니다.
# 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
이 샘플에서 선택한 도구는 NDK r22 이상에서 올바르게 작동합니다. 이전 NDK에는 다른 도구가 필요할 수 있습니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2024-08-22(UTC)
[[["이해하기 쉬움","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"]],["최종 업데이트: 2024-08-22(UTC)"],[],[],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."]]