使用 Libpq 将 iPhone 应用程序连接到 PostgreSQL
我需要为 iPhone 创建一个应用程序,它将使用 libpq 连接到 PostgreSQL 8.4 数据库。问题是我无法获得一个链接到 libpq 进行编译的简单 iPhone。不过,我可以获得等效的应用程序,即常规 Mac 桌面应用程序,可以毫无问题地编译并连接到 PostgreSQL。我在 Snow Leopard 上运行 Xcode 3.2。
我正在为arm和x86_84构建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
还有其他人遇到这个问题可以帮忙吗?
谢谢 启航3000
I need to create an application for the iPhone that will connect to a PostgreSQL 8.4 database using libpq. The problem is I can't get a simple iPhone that links to libpq to compile. I am however able to get the equivalent application that is a regular Mac desktop app to compile and connect to PostgreSQL without any issues. I'm on Xcode 3.2 running on Snow Leopard.
I'm building libpq for both arm and for x86_84. The arm build is for the real iPhone and the x86_64 is for the iPhone simulator to use. I then create a fat binary that contains both files and end up with a file named libpq. This file is the one I use in the regular Mac app and it works fine and causes issues when trying to build the iPhone app.
Here is my build script when I build 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
Here is the build log when I try to compile the iPhone app from within Xcode.
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
Anyone else run into this that can help out?
Thanks
StartShip3000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您实际上并未在库中链接。您已将其所在的目录添加到搜索路径中,但我在链接行的任何位置都没有看到
-lpq
。此外,模拟器环境仅为 32 位,x86_64 库无法与模拟器二进制文件一起使用。
It looks like you are not actually linking in the library. You have added the directory it is in to your search path, but I don't see
-lpq
anywhere on your link line.Also, the simulator environment is 32 bit only, x86_64 libs won't work with simulator binaries.
好的,谢谢路易斯的评论,解决了这个问题。我通过更改构建 i386 版本的 makefile 行来做到这一点。我添加了 CFLAGS=-arch i386" 看起来像这样
我现在有另一个问题,我将发布一个新问题以引用此链接。
Ok got around the issue thanks from the comment by Louis. I did this by changing makefile line that built the i386 version. I added CFLAGS=-arch i386" to look like this
I now have another issue that I will post a new question to an reference this link.
您可以尝试从 http://www.postgresqlformac.com 使用适用于 iOS 的 PGSQLKit
You can try using PGSQLKit for iOS from http://www.postgresqlformac.com