cross-compiling for iPhone dev

本文介绍了如何使用Shell脚本来在iPhone上进行跨平台编译,以实现开源库的构建。通过配置提供的代码片段,可以为iOS项目构建tesseract-ocr等库,并提供了一个简单的示例项目来演示整个过程。

转自:http://robertcarlsen.net/2009/07/15/cross-compiling-for-iphone-dev-884

Update: Proof-of-concept demo. Also, updated the script for building with the 10.6 SDK.

Update #2: Source code for demo project released.

Update #3: script for use with tesseract v3 posted.

I recently had need to use an open-source library in an iPhone project. Recalling the earlier work necessary in compiling the libraries needed foropenFrameworks I started looking for a more generic way to build for iPhone development. Thankfully, LateNiteSoft wrote a great article about using a shell script to cross-compile linux projects, building a Universal Binary with versions for the Simulator and Device.

I configured their provided code snippets to build tesseract-ocr for iPhone, referring to the set-up for freetype and freeimage to fill in some c++ gaps. Anyway, the library seems to have built correctly. I’ll know for sure when I incorporate it into a project, soon.

To use it, copy the script into the project directory, next to the configure script. For a simple project which generates one monolithic library, edit the LIBFILE variable to reflect the location and name of the library. I’ve only used this for static libraries…other work may be necessary to correctly generate dynamic libraries (however, the iPhone SDK prohibits linking to dynamic libraries, so in this case it seems moot). Run ./build_fat.sh to kick off the process. Look for the compiled libraries in the “lnsout” directory. There’s no error checking, so caveat emptor. :)

Cross-compile shell script follows:

#!/bin/sh
 
# build_fat.sh
#
# Created by Robert Carlsen on 15.07.2009.
# Updated 6.12.2009 to build i386 (simulator) on an x86_64 platform (10.6 SDK)
# build an arm / i386 lib of standard linux project
#
# adopted from:
# http://latenitesoft.blogspot.com/2008/10/iphone-programming-tips-building-unix.html
#
# initially configured for tesseract-ocr
 
# Set up the target lib file / path
# easiest to just build the package normally first and watch where the files are created.
LIBFILE=ccmain/libtesseract_full
 
# Select the desired iPhone SDK
export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneOS3.0.sdk
 
# Set up relevant environment variables
export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/4.0.1/include/ -I$SDKROOT/usr/include/ -miphoneos-version-min=2.2"
export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT"
export CPP="$DEVROOT/usr/bin/cpp $CPPFLAGS"
export CXXFLAGS="$CFLAGS"
 
# Dynamic library location generated by the Unix package
LIBPATH=$LIBFILE.dylib
LIBNAME=`basename $LIBPATH`
 
export LDFLAGS="-L$SDKROOT/usr/lib/ -Wl,-dylib_install_name,@executable_path/$LIBNAME"
 
# Static library that will be generated for ARM
LIBPATH_static=$LIBFILE.a
LIBNAME_static=`basename $LIBPATH_static`
 
# TODO: add custom flags as necessary for package
./configure CXX=$DEVROOT/usr/bin/arm-apple-darwin9-g++-4.0.1 CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-4.0.1 LD=$DEVROOT/usr/bin/ld --host=arm-apple-darwin
 
make -j4
 
# Copy the ARM library to a temporary location
mkdir -p lnsout
cp $LIBPATH_static lnsout/$LIBNAME_static.arm
 
# Do it all again for native cpu
make distclean
 
# Restore default environment variables
unset CPPFLAGS CFLAGS CPP LDFLAGS CXXFLAGS DEVROOT SDKROOT
 
export DEVROOT=/Developer
export SDKROOT=$DEVROOT/SDKs/MacOSX10.6.sdk
 
export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/i686-apple-darwin10/4.0.1/include/ -I$SDKROOT/usr/include/ -mmacosx-version-min=10.5"
export CFLAGS="$CPPFLAGS -pipe -no-cpp-precomp -isysroot $SDKROOT -arch i386"
export CPP="$DEVROOT/usr/bin/cpp $CPPFLAGS"
export CXXFLAGS="$CFLAGS"
 
 #Overwrite LDFLAGS
# Dynamic linking, relative to executable_path
# Use otool -D to check the install name
export LDFLAGS="-Wl,-dylib_install_name,@executable_path/$LIBNAME"
 
# TODO: error checking
./configure
make -j4
 
# Copy the native library to the temporary location
cp $LIBPATH_static lnsout/$LIBNAME_static.i386
 
# Create fat lib by combining the two versions
/usr/bin/lipo -arch arm lnsout/$LIBNAME_static.arm -arch i386 lnsout/$LIBNAME_static.i386 -create -output lnsout/$LIBNAME_static
 
unset CPPFLAGS CFLAGS CPP LDFLAGS CPP CXXFLAGS DEVROOT SDKROOT


For reference, here is the original script (written for use with 10.5):


#!/bin/sh
 
# build_fat.sh
#
# Created by Robert Carlsen on 15.07.2009.
# build an arm / i686 lib of standard linux project
#
# adopted from:
# http://latenitesoft.blogspot.com/2008/10/iphone-programming-tips-building-unix.html
#
# initially configured for tesseract-ocr
 
# Set up the target lib file / path
# easiest to just build the package normally first and watch where the files are created.
LIBFILE=ccmain/libtesseract_full
 
# Select the desired iPhone SDK
export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneOS2.2.sdk
 
# Set up relevant environment variables
export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/4.0.1/include/ -I$SDKROOT/usr/include/"
export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT"
export CPP="$DEVROOT/usr/bin/cpp $CPPFLAGS"
export CXXFLAGS="$CFLAGS"
 
# Dynamic library location generated by the Unix package
LIBPATH=$LIBFILE.dylib
LIBNAME=`basename $LIBPATH`
 
export LDFLAGS="-L$SDKROOT/usr/lib/ -Wl,-dylib_install_name,@executable_path/$LIBNAME"
 
# Static library that will be generated for ARM
LIBPATH_static=$LIBFILE.a
LIBNAME_static=`basename $LIBPATH_static`
 
# TODO: add custom flags as necessary for package
./configure CXX=$DEVROOT/usr/bin/arm-apple-darwin9-g++-4.0.1 CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-4.0.1 LD=$DEVROOT/usr/bin/ld --host=arm-apple-darwin
 
make -j4
 
# Copy the ARM library to a temporary location
mkdir -p lnsout
cp $LIBPATH_static lnsout/$LIBNAME_static.arm
 
# Do it all again for native cpu
make distclean
 
# Restore default environment variables
unset CPPFLAGS CFLAGS CPP LDFLAGS CXXFLAGS
 
# Overwrite LDFLAGS
# Dynamic linking, relative to executable_path
# Use otool -D to check the install name
export LDFLAGS="-Wl,-dylib_install_name,@executable_path/$LIBNAME"
 
# TODO: error checking
./configure
make -j4
 
# Copy the native library to the temporary location
cp $LIBPATH_static lnsout/$LIBNAME_static.i386
 
# Create fat lib by combining the two versions
$DEVROOT/usr/bin/lipo -arch arm lnsout/$LIBNAME_static.arm -arch i386 lnsout/$LIBNAME_static.i386 -create -output lnsout/$LIBNAME_static



交叉编译 `mbedtls` 是一个常见的需求,特别是在嵌入式系统开发中,需要为特定的目标架构生成库文件。以下是详细的交叉编译步骤,适用于不同的工具链和目标架构。 ### 交叉编译 mbedtls 的通用步骤 1. **获取 mbedtls 源码** 首先,克隆 mbedtls 的官方仓库,并切换到合适的版本标签。例如,使用 `v3.3.0` 版本: ```bash git clone https://github.com/Mbed-TLS/mbedtls.git cd mbedtls git checkout tags/v3.3.0 ``` 2. **创建构建目录** 为了保持源码目录的整洁,建议在源码目录外创建一个构建目录: ```bash mkdir build && cd build ``` 3. **配置 CMake** 使用 `cmake` 配置交叉编译环境。需要指定交叉编译器、禁用测试以及安装路径。例如,使用 `aarch64-linux-gnu-gcc` 工具链: ```bash cmake -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DENABLE_TESTING=OFF -DCMAKE_INSTALL_PREFIX=/opt/ .. ``` 如果使用的是 `riscv-nuclei-elf-gcc` 工具链,并且需要指定特定的架构和 ABI,可以这样配置: ```bash cmake -DCMAKE_C_COMPILER=riscv-nuclei-elf-gcc -DENABLE_TESTING=OFF -DCMAKE_INSTALL_PREFIX=/opt/ -DFORCE_RISCV_ARCH=rv32imac -DFORCE_RISCV_ABI=ilp32 .. ``` 4. **编译和安装** 使用 `make` 命令进行编译,并安装到指定的目录: ```bash make && make install ``` 5. **手动配置编译选项** 在某些情况下,可能需要手动修改 `include/mbedtls/config.h` 文件来禁用或启用某些功能。例如,注释掉以下宏定义: ```c // #define MBEDTLS_FS_IO // #define MBEDTLS_NET_C // #define MBEDTLS_PSA_CRYPTO_STORAGE_C // #define MBEDTLS_PSA_ITS_FILE_C // #define MBEDTLS_TIMING_C ``` 同时,定义以下宏以禁用平台熵: ```c #define MBEDTLS_NO_PLATFORM_ENTROPY ``` 6. **指定编译器标志** 如果使用 `make` 直接编译,可以通过设置 `CC` 环境变量来指定交叉编译器,并传递额外的编译标志: ```bash export CC="riscv-nuclei-elf-gcc -march=rv32imac -mabi=ilp32" make ``` 同样,可以指定 `CFLAGS` 和 `LDFLAGS` 来确保编译和链接时使用正确的架构和 ABI: ```bash make CC="riscv-nuclei-elf-gcc" CFLAGS="-march=rv32imac -mabi=ilp32" LDFLAGS="-march=rv32imac -mabi=ilp32" ``` 7. **安装库文件** 编译完成后,使用 `make install` 将生成的库文件安装到指定的目录中: ```bash make install ``` ### 注意事项 - **工具链选择**:确保使用的工具链与目标架构兼容。例如,`aarch64-linux-gnu-gcc` 适用于 ARM64 架构,而 `riscv-nuclei-elf-gcc` 适用于 RISC-V 架构。 - **配置选项**:根据目标平台的需求,合理配置 `config.h` 文件,禁用不必要的功能以减少库的大小。 - **编译标志**:在交叉编译时,务必指定正确的架构和 ABI,以避免链接错误。 通过以上步骤,应该能够成功交叉编译 mbedtls 并生成适用于目标平台的库文件。如果在编译过程中遇到问题,建议检查工具链的安装和配置,以及 `config.h` 文件中的选项设置[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值