代码之家  ›  专栏  ›  技术社区  ›  Kuberchaun

使用libpq将iPhone应用程序连接到PostgreSQL

  •  1
  • Kuberchaun  · 技术社区  · 15 年前

    我需要为iPhone创建一个应用程序,它将使用libpq连接到PostgreSQL 8.4数据库。问题是我不能得到一个简单的iphone链接到libpq进行编译。不过,我可以得到一个与之相当的应用程序,它是一个普通的Mac桌面应用程序,可以编译并连接到PostgreSQL,而不会出现任何问题。我在Xcode3.2上,在雪豹上奔跑。

    我正在为ARM和x86_构建libpq。ARM是为真正的iPhone而构建的,x86_64是为iPhone模拟器所使用的。然后我创建一个包含两个文件的胖二进制文件,最后得到一个名为libpq的文件。这个文件是我在普通Mac应用程序中使用的文件,它工作正常,在尝试构建iPhone应用程序时会引起问题。

    这是我构建libpq时的构建脚本。

    #!/bin/bash
    
    DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
    SDKROOT=$DEVROOT/SDKs/iPhoneOS3.0.sdk
    
    rm -rf  /Users/bob/mylibs
    mkdir /Users/bob/mylibs #Store there compiled libs
    make clean
    
    #Build ARM library
    ./configure --host=arm-apple-darwin --without-readline --disable-ipv6 CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-4.0.1 CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/4.0.1/include/ -I$SDKROOT/usr/include/" CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT" CPP="$DEVROOT/usr/bin/cpp $CPPFLAGS" LD=$DEVROOT/usr/bin/ld
    make -C src/interfaces/libpq
    cp /Users/bob/Downloads/postgresql-8.4.1/src/interfaces/libpq/libpq.a /Users/bob/mylibs/libpq.arm
    
    #Then build i386 library
    make clean && ./configure  && make -C src/interfaces/libpq
    cp src/interfaces/libpq/libpq.a  /Users/bob/mylibs/libpq.i386
    
    #Then make fat binary
    $DEVROOT/usr/bin/lipo -arch armv6 /Users/bob/mylibs/libpq.arm -arch x86_64 /Users/bob/mylibs/libpq.i386 -create -output  /Users/bob/mylibs/libpq
    

    下面是我尝试在Xcode中编译iPhone应用程序时的构建日志。

    Build iPhonePg of project iPhonePg with configuration Debug
    
    Ld build/Debug-iphonesimulator/iPhonePg.app/iPhonePg normal i386
    cd /Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg
    setenv MACOSX_DEPLOYMENT_TARGET 10.5
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk -L/Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/build/Debug-iphonesimulator -L../../../../mylibs -L/Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg -L/Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/../../../../mylibs -F/Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/build/Debug-iphonesimulator -filelist /Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/build/iPhonePg.build/Debug-iphonesimulator/iPhonePg.build/Objects-normal/i386/iPhonePg.LinkFileList -mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework CoreGraphics /Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/libpq -o /Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/build/Debug-iphonesimulator/iPhonePg.app/iPhonePg
    
    ld: warning: in /Users/bob/Documents/Programming/PragProgrammerIphoneSDK/iPhonePg/libpq, missing required architecture i386 in file
    Undefined symbols:
      "_PQclear", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
      "_PQerrorMessage", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
      "_PQconnectdb", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
      "_PQfinish", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
      "_PQstatus", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
      "_PQexec", referenced from:
          -[iPhonePgAppDelegate applicationDidFinishLaunching:] in iPhonePgAppDelegate.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    

    有没有人会遇到这种情况?

    谢谢 Skest3000

    3 回复  |  直到 12 年前
        1
  •  5
  •   Louis Gerbarg    15 年前

    看起来你并没有在图书馆链接。您已将它所在的目录添加到搜索路径中,但我看不到 -lpq 链接线上的任何地方。

    另外,模拟器环境只有32位,x86 libs不能与模拟器二进制文件一起工作。

        2
  •  1
  •   Kuberchaun    15 年前

    好的,绕过这个问题,谢谢路易斯的评论。我通过更改生成i386版本的makefile行来实现这一点。我加了cflags=-arch i386“看起来像这样

    #Then build i386 library
    make clean && ./configure CFLAGS="-arch i386" && make -C src/interfaces/libpq
    

    我现在有另一个问题,我将发布一个新问题,以引用此链接。

        3
  •  1
  •   Nenad    14 年前

    您可以尝试使用pgsqlkit for ios,从 http://www.postgresqlformac.com