[Android] cross compile settings

โ˜๐Ÿป cross compile

Android์—์„œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์„ x64 ์ปดํ“จํ„ฐ์—์„œ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค.

QtCreator๋ฅผ ์ด์šฉํ•ด์„œ ์ปดํŒŒ์ผ ๋ฐฉ์‹์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ปดํŒŒ์ผ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

  • arm64
    /home/hoho/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -target aarch64-linux-android23 -o hello-world hello-world.c
    
  • arm32
    /home/hoho/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -target armv7a-linux-androideabi23 -o hello-world hello-world.c
    

ํ•˜์ง€๋งŒ ๋งค๋ฒˆ ์ด๋ ‡๊ฒŒ ๊ธด ๋ช…๋ น์–ด๋ฅผ ์น˜๋Š” ๊ฒƒ์ด ๋ณต์žกํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ์‰ฝ๊ฒŒ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ์‹์„ ๋‹ค๋ฃจ๊ณ ์ž ํ•œ๋‹ค.

cmake,.profile ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์ด๋ฒˆ article ์—์„œ๋Š” ~/.bashrc ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ ์ž ํ•œ๋‹ค.

โ˜๐Ÿป ๊ฐ„ํŽธํ™”(c)

clang๊ณผ clang++์€ ๊ฐ๊ฐ c์™€ cpp๋กœ ๋œ ํ”„๋กœ๊ทธ๋žจ์„ ์ปดํŒŒ์ผํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์ด์ค‘์— ๋จผ์ € c์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 Android NDK
ANDROID_NDK_ROOT=/home/hoho/Android/Sdk/ndk/25.1.8937393
ANDROID_TOOLCHAIN=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin

function compile_android() {
    local arch=$1
    local compiler=$2
    
    for file in *.c
    do
        filename="${file%.*}"
        case $arch in
            x86_64)
                $compiler $file -o $filename.x86_64
                ;;
            arm64)
                $compiler -target aarch64-linux-android23 $file -o $filename.arm64
                ;;
            arm32)
                $compiler -target armv7a-linux-androideabi23 $file -o $filename.arm32
                ;;
        esac
    done
}

NDK ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ , ์•„ํ‚คํ…์ฒ˜๋งˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ๋‹ฌ๋ฆฌํ•˜์—ฌ ๋“ฑ๋กํ•˜์˜€๋‹ค.
๊ทธ ํ›„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.

alias android-x86_64-cc="compile_android x86_64 gcc"
alias android-arm64-cc="compile_android arm64 $ANDROID_TOOLCHAIN/clang"
alias android-arm32-cc="compile_android arm32 $ANDROID_TOOLCHAIN/clang"

โ˜๐Ÿป ๊ฐ„ํŽธํ™”(cpp)

cpp๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ANDROID_NDK_ROOT=/home/hoho/Android/Sdk/ndk/25.1.8937393
ANDROID_TOOLCHAIN=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin

function compile_android_cpp() {
    local arch=$1
    local compiler=$2

    for file in *.cpp
    do
        filename="${file%.*}"
        case $arch in
            x86_64)
                $compiler $file -o $filename.x86_64
                ;;
            arm64)
                $compiler -target aarch64-linux-android23 $file -o $filename.arm64
                ;;
            arm32)
                $compiler -target armv7a-linux-androideabi23 $file -o $filename.arm32
                ;;
        esac
    done
}

๊ทธ ํ›„์— ๋ช…๋ น์–ด๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ–ˆ๋‹ค.

alias android-x86_64-cpp="compile_android_cpp x86_64 g++"
alias android-arm64-cpp="compile_android_cpp arm64 $ANDROID_TOOLCHAIN/clang++"
alias android-arm32-cpp="compile_android_cpp arm32 $ANDROID_TOOLCHAIN/clang++"

์ปดํŒŒ์ผํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

โ˜๐Ÿป ๊ฒฐ๋ก 

cpp ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ์—๋Š” dependency๋ฅผ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. dependency์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ์— ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ค.

Categories:

Updated:

Leave a comment