将GDAL / OGR合并到iOS项目中 - 快速指南 [英] Incorporating GDAL/OGR into an iOS project - A quick guide

查看:288
本文介绍了将GDAL / OGR合并到iOS项目中 - 快速指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于:
GDAL 是一个出色的开源库,旨在管理复杂的GIS数据,栅格和矢量。它完全编译为 Mac OS (由William Kyngesburye提供)和其他平台,但不适用于iOS。

Here is the problem: GDAL is a fantastic open source library designed to manage complex GIS data, both raster as well as vector. It is fully compiled for the Mac OS (courtesy of William Kyngesburye) and other platforms but not for iOS.

浏览网页,您可以找到关于创建iOS库主题的(相对较旧的)信息,从 pseudogreen ,写于3年前。堆栈溢出也有点点碎片,例如iPhone上的 GDAL / OGR 提供额外的信息。

Browsing the net you can find bits and pieces of (relatively old) information on the topic of creating an iOS library, starting with the famous script from pseudogreen which was written over 3 years ago. There are also bits and pieces on stack-overflow such as GDAL / OGR on the iPhone which provide additional information.

本文旨在涵盖我采取的所有步骤,这使我在使用iOS6的简单iOS应用程序中实现GDAL / OGR的全功能集成和XCode 4.5.5

This article is meant to cover all the steps I took which led me to a fully functional integration of GDAL/OGR in a simple iOS app using iOS6 and XCode 4.5.5

推荐答案

注意



这个回复是写的前段时间和mo长时间使用Xcode 6及以上版本。请查看此链接以获取更新的答案这个问题。

Note

This response was written some time ago and mo longer works with Xcode 6 and up. Please check this link for a more current answer to this problem.

将GDAL纳入您的iOS应用程序需要5个步骤:

Incorporating GDAL into your iOS app is a 5 steps process:


  1. 从GDAL网站下载GDAL源代码

  2. 运行下面给出的configure / build / install脚本

  3. 将生成的静态库与包含文件一起添加到iOS项目中

  4. 与iOS项目中的其他库链接

  5. 开始编码...... GDAL和OGR教程是很好的起点

  1. Download the GDAL source code from the GDAL website
  2. Run the configure/build/install script given below
  3. Add the resulting static library into your iOS project along with the include files
  4. Link with additional libraries in your iOS project
  5. Start coding... the GDAL and OGR tutorials are good starting points



下载GDAL



GDAL是一个C ++开源库,可以从 www.gdal.org网站下载
在撰写本文时,最新版本为1.9.0。如果可能,你应该下载最新的稳定版本。

Downloading GDAL

GDAL is a C++ open source library which can be downloaded from the www.gdal.org web site. At the time of writing the latest version is 1.9.0. You should download the latest stable version if possible.

为了在iOS项目中使用GDAL,您需要将源代码编译为静态库(.a)。使用最新的iOS6支持的体系结构,您应该为以下体系结构创建静态库:

In order to use GDAL in your iOS project you need to compile the source code as a static library (.a). With the latest iOS6-supported architecture you should create the static library for the following architectures:


  • 模拟器的i386

  • armv7 for iPhone 3GS to iPhone 4S

  • armv7s for iPhone 5

以下脚本,改编自 pseudogreen 可以为单一架构编译源代码。

The following script, which is adapted from pseudogreen's does the trick of compiling the source code for a single architecture.

将此代码复制粘贴到文本编辑器中并保存它作为扩展名为.sh的文件:例如build_gdal_ios.sh。

Copy paste this code into a text editor and save it as file with .sh extension: for instance build_gdal_ios.sh.

要使用它,请将脚本复制到您下载gdal源代码的目录中并按如下方式运行:

To use it copy the script into the directory where you downloaded the gdal source code and run it as follows:


  • 为模拟器构建库:

  • To build the library for the simulator:

`sh build_gdal_ios.sh -p "location where you want to save the resulting files" simulator`


  • 为设备构建它:

  • To build it for the device:

    `sh build_gdal_ios.sh -p "location where you want to save the resulting files" -a "architecture" device`
    


  • 您还可以输入 sh build_gdal_ios.sh -h 获得帮助。

    You can also type sh build_gdal_ios.sh -h to get the help.

        #!/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编译器中的首选项或更改:

    Notice that this script has a few default parameters which you can change to reflect your preferences or changes in the SDK or LLVM Apple compiler:


    • default_gcc_version = 4.2

    • default_iphoneos_version = 6.0

    • default_macos_version = 10.8

    • default_architecture = armv7

    • default_prefix =$ {HOME} / Documents / GDALLibrary

    • default_gcc_version=4.2
    • default_iphoneos_version=6.0
    • default_macos_version=10.8
    • default_architecture=armv7
    • default_prefix="${HOME}/Documents/GDALLibrary"

    使用前面的脚本( build_gdal_ios.sh ),您可以一次构建一个体系结构。你需要编译为3,然后将所有这些库放在一个静态库文件下。

    Using the preceding script (build_gdal_ios.sh) allows you to build one architecture at a time... You need to compile for 3 and then bring all these libraries together under one single static library file.

    以下脚本允许这样做(将它保存到另一个名称,如build_gdal_all_ios.sh):

    The following script allows just that (save it to another name such as 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目录中:

    After running this script you will have your libraries saved in your ${HOME}/Documents/GDALLibrary directory in subfolders:


    • $ {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

    您现在可以使用可执行脂肪(用于吸脂术)将3个库加入到一个库中,如下所示:

    You can now use the executable lipo (for liposuction) to join the 3 libraries into a single one like so:

    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
    

    ...你已经完成......

    ... And you're done...

    这一步非常简单:


    1. 在Xcode(4.5)中创建一个新项目或打开要添加GDAL的项目

    2. 在文件浏览器中右键单击并选择将文件添加到项目

    3. 选择上面创建的libgdal.a以及其中一个包含目录中的包含文件(3个目录包含相同的文件)

    4. 将以下库添加到您的XCode项目中(从项目框架列表中):


      • libstdc ++。6.0.9.dylib

      • libz.dylib

      • libiconv.dylib

      • libsqlite3.dylib

      • libxml2。 dylib(如果是弧的未定义符号) hitecture armv7:
        _xmlCatalogResolveSystem等)

    1. Create a new project in Xcode (4.5) or open the one you want to add GDAL to
    2. In the file explorer right click and select "Add files to projects"
    3. Select the libgdal.a created above along with the include files in one of the include directories (the 3 directories contain the same files)
    4. Add the following libraries to your XCode project (from the project framework list):
      • libstdc++.6.0.9.dylib
      • libz.dylib
      • libiconv.dylib
      • libsqlite3.dylib
      • libxml2.dylib (if Undefined symbols for architecture armv7: "_xmlCatalogResolveSystem" etc)

    构建代码。所有应该编译没有麻烦。

    Build your code. All should compile without trouble.

    这里有一个技巧:你使用的是C ++库(和头文件)在Objective-C环境中。如果将一个GDAL头文件包含到.m文件中,XCode会抱怨C ++语法。

    There is a trick here: you are using a C++ library (and header files) in an Objective-C environment. If you include one of the GDAL header files into a .m file XCode will complain about the C++ syntax.

    这里有两个解决方案:


    1. 将所有GDAL代码写入.mm文件中,然后Xcode将其识别为Objective-C ++文件并进行编译

    2. 使用您的Objective-C文件中的类扩展,如Phil Jordan在其优秀文章混合使用Objective-C ++和C ++

    1. Write all your GDAL code inside .mm files which Xcode will then recognize as Objective-C++ files and will compile
    2. Use a class extension in your Objective-C files as described by Phil Jordan on his excellent article Mixing Objective-C++ and C++

    有一次,我将发布一些GDAL代码示例。但是后来......

    At one point I will be posting some GDAL code samples... but later...

    这篇关于将GDAL / OGR合并到iOS项目中 - 快速指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆