笔记
此响应是前段时间编写的,并且更适用于 Xcode 6 及更高版本。请检查此链接以获取有关此问题的最新答案。
介绍
将 GDAL 整合到您的 iOS 应用程序中需要 5 个步骤:
- 从 GDAL 网站下载 GDAL 源代码
- 运行下面给出的配置/构建/安装脚本
- 将生成的静态库与包含文件一起添加到您的 iOS 项目中
- 与您的 iOS 项目中的其他库链接
- 开始编码... GDAL 和 OGR 教程是很好的起点
下载GDAL
GDAL 是一个 C++ 开源库,可以从www.gdal.org 网站下载。在撰写本文时,最新版本是 1.9.0。如果可能,您应该下载最新的稳定版本。
运行脚本为iOS和模拟器编译GDAL
为了在您的 iOS 项目中使用 GDAL,您需要将源代码编译为静态库 (.a)。使用支持 iOS6 的最新架构,您应该为以下架构创建静态库:
- i386 用于模拟器
- 适用于 iPhone 3GS 到 iPhone 4S 的 armv7
- 适用于 iPhone 5 的 armv7s
为 1 个架构构建的基本脚本
以下脚本改编自pseudogreen的脚本,可以为单一架构编译源代码。
将此代码复制粘贴到文本编辑器中,并将其保存为扩展名为 .sh 的文件:例如 build_gdal_ios.sh。
要使用它,请将脚本复制到下载 gdal 源代码的目录中并按如下方式运行它:
您也可以键入sh build_gdal_ios.sh -h
以获得帮助。
#!/bin/bash
################################################################################
#
# Copyright (c) 2008-2009 Christopher J. Stawarz
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################
# Disallow undefined variables
set -u
default_gcc_version=4.2
default_iphoneos_version=6.0
default_macos_version=10.8
default_architecture=armv7
default_prefix="${HOME}/Documents/iOS_GDAL"
GCC_VERSION="${GCC_VERSION:-$default_gcc_version}"
export IPHONEOS_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET:-$default_iphoneos_version}"
export MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET:-$default_macos_version}"
DEFAULT_ARCHITECTURE="${DEFAULT_ARCHITECTURE:-$default_architecture}"
DEFAULT_PREFIX="${HOME}/Documents/iOS_GDAL"
echo Default architecture: $DEFAULT_ARCHITECTURE
usage ()
{
cat >&2 << EOF
Usage: ${0##*/} [-ht] [-p prefix] [-a arch] target [configure_args]
-h Print help message
-p Installation prefix (default: \$HOME/Documents/iOS_GDAL...)
-t Use 16-bit Thumb instruction set (instead of 32-bit ARM)
-a Architecture target for compilation (default: armv7)
The target must be "device" or "simulator". Any additional arguments
are passed to configure.
The following environment variables affect the build process:
GCC_VERSION (default: $default_gcc_version)
IPHONEOS_DEPLOYMENT_TARGET (default: $default_iphoneos_version)
MACOSX_DEPLOYMENT_TARGET (default: $default_macos_version)
DEFAULT_PREFIX (default: $default_prefix)
EOF
}
prefix="${DEFAULT_PREFIX}"
echo Prefix: $prefix
while getopts ":hp:a:t" opt; do
case $opt in
h ) usage ; exit 0 ;;
p ) prefix="$OPTARG" ;;
t ) thumb_opt=thumb ;;
a ) DEFAULT_ARCHITECTURE="$OPTARG" ;;
\? ) usage ; exit 2 ;;
esac
done
shift $(( $OPTIND - 1 ))
if (( $# < 1 )); then
usage
exit 2
fi
target=$1
shift
case $target in
device )
arch="${DEFAULT_ARCHITECTURE}"
platform=iPhoneOS
extra_cflags="-m${thumb_opt:-no-thumb} -mthumb-interwork"
;;
simulator )
arch=i386
platform=iPhoneSimulator
extra_cflags="-D__IPHONE_OS_VERSION_MIN_REQUIRED=${IPHONEOS_DEPLOYMENT_TARGET%%.*}0000"
;;
* )
echo No target found!!!
usage
exit 2
esac
platform_dir="/Applications/Xcode.app/Contents/Developer/Platforms/${platform}.platform/Developer"
platform_bin_dir="${platform_dir}/usr/llvm-gcc-${GCC_VERSION}/bin"
platform_sdk_dir="${platform_dir}/SDKs/${platform}${IPHONEOS_DEPLOYMENT_TARGET}.sdk"
prefix="${prefix}/${arch}/${platform}.platform/${platform}${IPHONEOS_DEPLOYMENT_TARGET}.sdk"
echo library will be exported to $prefix
export CC="${platform_bin_dir}/llvm-gcc-${GCC_VERSION}"
export CFLAGS="-arch ${arch} -pipe -Os -gdwarf-2 -isysroot ${platform_sdk_dir} ${extra_cflags}"
export LDFLAGS="-arch ${arch} -isysroot ${platform_sdk_dir}"
export CXX="${platform_bin_dir}/llvm-g++-${GCC_VERSION}"
export CXXFLAGS="${CFLAGS}"
export CPP="${platform_bin_dir}/llvm-cpp-${GCC_VERSION}"
export CXXCPP="${CPP}"
./configure \
--prefix="${prefix}" \
--host="${arch}-apple-darwin" \
--disable-shared \
--enable-static \
--with-unix-stdio-64=no \
"$@" || exit
make install || exit
cat >&2 << EOF
Build succeeded! Files were installed in
$prefix
EOF
请注意,此脚本有一些默认参数,您可以更改这些参数以反映您的偏好或 SDK 或 LLVM Apple 编译器中的更改:
- default_gcc_version=4.2
- default_iphoneos_version=6.0
- default_macos_version=10.8
- 默认架构=armv7
- default_prefix="${HOME}/Documents/GDALLibrary"
现在为多种架构构建
使用前面的脚本build_gdal_ios.sh
(
以下脚本允许这样做(将其保存为另一个名称,例如 build_gdal_all_ios.sh):
#!/bin/bash
make clean
./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary -a armv7 device
make clean
./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary -a armv7s device
make clean
./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary simulator
运行此脚本后,您的库将保存在 ${HOME}/Documents/GDALLibrary 目录的子文件夹中:
- ${HOME}/Documents/GDALLibrary/i386/iPhoneSimulator.platform/iPhoneSimulator6.0.sdk/lib
- ${HOME}/Documents/GDALLibrary/armv7/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib
- ${HOME}/Documents/GDALLibrary/armv7s/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib
您现在可以使用可执行的 lipo(用于吸脂)将 3 个库合并为一个,如下所示:
lipo ${HOME}/Documents/GDALLibrary/i386/iPhoneSimulator.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a ${HOME}/Documents/GDALLibrary/armv7/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a ${HOME}/Documents/GDALLibrary/armv7s/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a -output ${HOME}/Documents/GDALLibrary/libgdal.a -create
......你完成了......
将静态库添加到您的 XCode 项目
这一步相当简单:
- 在 Xcode (4.5) 中创建一个新项目或打开您想要添加 GDAL 的项目
- 在文件资源管理器中右键单击并选择“将文件添加到项目”
- 选择上面创建的 libgdal.a 以及包含目录之一中的包含文件(3 个目录包含相同的文件)
- 将以下库添加到您的 XCode 项目(来自项目框架列表):
- libstdc++.6.0.9.dylib
- libz.dylib
- libiconv.dylib
- libsqlite3.dylib
- libxml2.dylib(如果架构 armv7 的未定义符号:“_xmlCatalogResolveSystem”等)
构建您的代码。一切都应该顺利编译。
开始编码
这里有一个技巧:您在 Objective-C 环境中使用 C++ 库(和头文件)。如果将 GDAL 头文件之一包含到 .m 文件中,XCode 将抱怨 C++ 语法。
在这里,您有两个解决方案:
- 将所有 GDAL 代码写入 .mm 文件中,然后 Xcode 将其识别为 Objective-C++ 文件并进行编译
- 在您的 Objective-C 文件中使用类扩展,如 Phil Jordan 在其出色的文章Mixing Objective-C++ 和 C++中所述
在某一时刻,我将发布一些 GDAL 代码示例......但稍后......