为 iOS(设备和模拟器)构建静态库并在 XCode 中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5633242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build static library for iOS (Device and Simulator) and use in XCode
提问by EliteTUM
I'm new to XCode-/iOS-Development(not programming itself) and want to make some simple Apps to get used to it.
我是 XCode-/iOS-Development(不是编程本身)的新手,想制作一些简单的应用程序来习惯它。
My current project is a simple QR-Code-Generation. For this task I want to use the Open Source Library libqrencode. ( http://fukuchi.org/works/qrencode/index.en.html) I did not use open source libraries in my older coding projects but I guess I understood most of the "mechanics" ;)
我目前的项目是一个简单的二维码生成。对于这个任务,我想使用开源库 libqrencode。( http://fukuchi.org/works/qrencode/index.en.html) 我没有在较早的编码项目中使用开源库,但我想我了解大部分“机制”;)
Until now I tried to build the static library and include it in my XCode-Project. But when compiling code using functions from the library I get Error-Messages.
到目前为止,我尝试构建静态库并将其包含在我的 XCode 项目中。但是当使用库中的函数编译代码时,我收到错误消息。
1st Problem:
第一个问题:
I try to build for iPhone-Device like this:
我尝试像这样为 iPhone 设备构建:
#### Build libqrencode for iPhoneOS
export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export HOST=arm-apple-darwin10
export TARGET=arm-apple-darwin10
export PREFIX=$HOME/Desktop/MyProjects/cocoa/libqrencode/Release-iphoneos
export AR=$DEVROOT/usr/bin/ar
export CPP=$DEVROOT/usr/bin/cpp
export CXXPP=$DEVROOT/usr/bin/cpp
export CXX=$DEVROOT/usr/bin/arm-apple-darwin10-llvm-g++-4.2
export LD=$DEVROOT/usr/bin/ld
export NM=$DEVROOT/usr/bin/nm
export RANLIB=$DEVROOT/usr/bin/ranlib
./configure --prefix=$PREFIX --host=$HOST --target=$TARGET --disable-shared --enable-static
makedepend
make
make install
But when I include it in my XCode-Project and try to use two simple Functions from that library I get the following error:
但是当我将它包含在我的 XCode 项目中并尝试使用该库中的两个简单函数时,我收到以下错误:
Ld build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv6/myQRencode normal armv6
cd /Users/johnny/Desktop/MyProjects/myQRencode
setenv IPHONEOS_DEPLOYMENT_TARGET 4.2
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk -L/Users/johnny/Desktop/MyProjects/myQRencode/build/Release-iphoneos -L/Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Release-iphoneos/lib -F/Users/johnny/Desktop/MyProjects/myQRencode/build/Release-iphoneos -filelist /Users/johnny/Desktop/MyProjects/myQRencode/build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv6/myQRencode.LinkFileList -dead_strip -miphoneos-version-min=4.2 -framework Foundation -framework UIKit -framework CoreGraphics -lqrencode -o /Users/johnny/Desktop/MyProjects/myQRencode/build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv6/myQRencode
ld: warning: in /Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Release-iphoneos/lib/libqrencode.a, file was built for unsupported file format which is not the architecture being linked (armv6)
Undefined symbols:
"_QRcode_encodeString", referenced from:
_main in main.o
"_QRcode_free", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Ld build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv7/myQRencode normal armv7
cd /Users/johnny/Desktop/MyProjects/myQRencode
setenv IPHONEOS_DEPLOYMENT_TARGET 4.2
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv7 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk -L/Users/johnny/Desktop/MyProjects/myQRencode/build/Release-iphoneos -L/Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Release-iphoneos/lib -F/Users/johnny/Desktop/MyProjects/myQRencode/build/Release-iphoneos -filelist /Users/johnny/Desktop/MyProjects/myQRencode/build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv7/myQRencode.LinkFileList -dead_strip -miphoneos-version-min=4.2 -framework Foundation -framework UIKit -framework CoreGraphics -lqrencode -o /Users/johnny/Desktop/MyProjects/myQRencode/build/myQRencode.build/Release-iphoneos/myQRencode.build/Objects-normal/armv7/myQRencode
ld: warning: in /Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Release-iphoneos/lib/libqrencode.a, file was built for unsupported file format which is not the architecture being linked (armv7)
Undefined symbols:
"_QRcode_encodeString", referenced from:
_main in main.o
"_QRcode_free", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
2nd Problem:
第二个问题:
I try to build for iPhone-Device like this:
我尝试像这样为 iPhone 设备构建:
#### Build libqrencode for iPhone-Simulator
export DEVROOT=/Developer/Platforms/iPhoneSimulator.platform/Developer
export HOST=i386-apple-darwin10
export TARGET=i386-apple-darwin10
export PREFIX=$HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator
export AR=$DEVROOT/usr/bin/ar
export CPP=$DEVROOT/usr/bin/cpp
export CXXPP=$DEVROOT/usr/bin/cpp
export CXX=$DEVROOT/usr/bin/i686-apple-darwin10-llvm-g++-4.2
export LD=$DEVROOT/usr/bin/ld
export NM=$DEVROOT/usr/bin/nm
export RANLIB=$DEVROOT/usr/bin/ranlib
./configure --prefix=$PREFIX --host=$HOST --target=$TARGET --disable-shared --enable-static
makedepend
make
make install
But when I include it in my XCode-Project and try to use two simple Functions from that library I get the following error (actually same error-message like above):
但是当我将它包含在我的 XCode 项目中并尝试使用该库中的两个简单函数时,我收到以下错误(实际上与上面的错误消息相同):
Ld build/Debug-iphonesimulator/myQRencode.app/myQRencode normal i386
cd /Users/johnny/Desktop/MyProjects/myQRencode
setenv MACOSX_DEPLOYMENT_TARGET 10.6
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/iPhoneSimulator4.2.sdk -L/Users/johnny/Desktop/MyProjects/myQRencode/build/Debug-iphonesimulator -L/Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Debug-iphonesimulator/lib -F/Users/johnny/Desktop/MyProjects/myQRencode/build/Debug-iphonesimulator -filelist /Users/johnny/Desktop/MyProjects/myQRencode/build/myQRencode.build/Debug-iphonesimulator/myQRencode.build/Objects-normal/i386/myQRencode.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -lqrencode -o /Users/johnny/Desktop/MyProjects/myQRencode/build/Debug-iphonesimulator/myQRencode.app/myQRencode
ld: warning: in /Users/johnny/Desktop/MyProjects/myQRencode/libqrencode/Debug-iphonesimulator/lib/libqrencode.a, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols:
"_QRcode_encodeString", referenced from:
_main in main.o
"_QRcode_free", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Question 1:I also tried HOST and TARGET to be set to i686, but that didn't work either (same error message). Unfortunately I only have "i686-apple-darwin10-llvm-g++-4.2" and similar ones, but no "i386-xxxx....". Is this the problem or what settings did I choose wrong?
问题 1:我也尝试将 HOST 和 TARGET 设置为 i686,但这也不起作用(错误消息相同)。不幸的是,我只有“i686-apple-darwin10-llvm-g++-4.2”和类似的,但没有“i386-xxxx ....”。这是问题还是我选择了错误的设置?
Question 2:How do I check what target the library was built for? Is there a special command in bash? I remember when first time building libqrencode I needed to get libpng12. Until now I do not use any commands for creating PNGs but I did not rebuild it for the iPhone-Simulator nor the iPhone-Device. Will the libpng12-library still be linked by XCode and could this consequently be the source of error?
问题 2:如何检查库是为什么目标构建的?bash 中是否有特殊命令?我记得第一次构建 libqrencode 时,我需要获取 libpng12。到目前为止,我不使用任何命令来创建 PNG,但我没有为 iPhone 模拟器或 iPhone 设备重建它。libpng12-library 是否仍会被 XCode 链接,这是否会因此成为错误的来源?
Any help appreciated. Sorry for the long post, but I wanted to be as thorough as possible. Thans allready!!
任何帮助表示赞赏。很抱歉这篇长文章,但我想尽可能彻底。比已经!
Added Infos:
添加信息:
I managed to build libpng12for I386 using the following Configuration-Settings:
我设法使用以下配置设置为 I386构建libpng12:
#! /bin/bash
#### Build libpng12 for iPhone-Simulator
unset CPATH
unset C_INCLUDE_PATH
unset CPLUS_INCLUDE_PATH
unset OBJC_INCLUDE_PATH
unset LIBS
unset DYLD_FALLBACK_LIBRARY_PATH
unset DYLD_FALLBACK_FRAMEWORK_PATH
unset CPP
unset CXXPP
unset CC
unset CXX
unset LD
unset STRIP
unset CPPFLAGS
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
unset AS
unset ASCPP
export SDKVER="4.2"
export DEVROOT="/Developer/Platforms/iPhoneSimulator.platform/Developer"
export SDKROOT="$DEVROOT/SDKs/iPhoneSimulator$SDKVER.sdk"
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
export PKG_CONFIG_LIBDIR="$PKG_CONFIG_PATH"
export PREFIX="$HOME/Desktop/MyProjects/cocoa/libpng12/Debug-iphonesimulator"
export AS="$DEVROOT/usr/bin/as"
export ASCPP="$DEVROOT/usr/bin/as"
export AR="$DEVROOT/usr/bin/ar"
export RANLIB="$DEVROOT/usr/bin/ranlib"
export CPPFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -pipe -no-cpp-precomp -I$SDKROOT/usr/include -I$HOME/Desktop/MyProjects/cocoa/libpng12/Debug-iphonesimulator/include -I/usr/local/iphone-simulator-$SDKVER/include"
export CFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -arch i386 -pipe -no-cpp-precomp --sysroot='$SDKROOT' -isystem $SDKROOT/usr/include -isystem $HOME/Desktop/MyProjects/cocoa/libpng12/Debug-iphonesimulator/include -isystem /usr/local/iphone-simulator-$SDKVER/include -m32"
export CXXFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -arch i386 -pipe -no-cpp-precomp --sysroot='$SDKROOT' -isystem $SDKROOT/usr/include -isystem $HOME/Desktop/MyProjects/cocoa/libpng12/Debug-iphonesimulator/include -isystem /usr/local/iphone-simulator-$SDKVER/include"
export LDFLAGS="-miphoneos-version-min=$SDKVER -arch i386 --sysroot='$SDKROOT' -L$SDKROOT/usr/lib -L$HOME/Desktop/MyProjects/cocoa/libpng12/Debug-iphonesimulator/lib -L/usr/local/iphone-simulator-$SDKVER/lib"
export CPP="$DEVROOT/usr/bin/cpp"
export CXXCPP="$DEVROOT/usr/bin/cpp"
export CC="$DEVROOT/usr/bin/gcc-4.2"
export CXX="$DEVROOT/usr/bin/g++-4.2"
export LD="$DEVROOT/usr/bin/ld"
export STRIP="$DEVROOT/usr/bin/strip"
./configure --prefix="$PREFIX" --host="i386-apple-darwin9" --enable-static --disable-shared
But when I try using similar/same settings for libqrencodeI get following error:
但是当我尝试对libqrencode使用类似/相同的设置时,我收到以下错误:
make all-recursive
Making all in .
/bin/sh ./libtool --tag=CC --mode=link gcc -Wall -miphoneos-version-min=4.2 -std=c99 -arch i386 -pipe -no-cpp-precomp --sysroot='/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk' -isystem /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include -isystem /Users/johnny/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/include -isystem /usr/local/iphone-simulator-4.2/include -m32 -version-number 3:1:1 -miphoneos-version-min=4.2 -arch i386 --sysroot='/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk' -L/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/lib -L/Users/johnny/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/lib -L/usr/local/iphone-simulator-4.2/lib -m32 -o libqrencode.la -rpath /Users/johnny/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/lib qrencode.lo qrinput.lo bitstream.lo qrspec.lo rscode.lo split.lo mask.lo
libtool: link: rm -fr .libs/libqrencode.a
libtool: link: /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ar cru .libs/libqrencode.a qrencode.o qrinput.o bitstream.o qrspec.o rscode.o split.o mask.o
/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(bitstream.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(rscode.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(split.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
libtool: link: /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ranlib .libs/libqrencode.a
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(bitstream.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(rscode.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ranlib: archive member: .libs/libqrencode.a(split.o) cputype (16777223) does not match previous archive members cputype (7) (all members must match)
make[2]: *** [libqrencode.la] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
采纳答案by EliteTUM
OK, finally I have time to finish this Question. The problem was solved before, I just used a wrong include Path. The library can be successfully created using some code like this:
好的,我终于有时间完成这个问题了。之前问题解决了,我只是用了错误的include路径。可以使用如下代码成功创建该库:
#! /bin/bash
#### Build libqrencode for iPhone-Simulator
unset CPATH
unset C_INCLUDE_PATH
unset CPLUS_INCLUDE_PATH
unset OBJC_INCLUDE_PATH
unset LIBS
unset DYLD_FALLBACK_LIBRARY_PATH
unset DYLD_FALLBACK_FRAMEWORK_PATH
export SDKVER="4.2"
export DEVROOT="/Developer/Platforms/iPhoneSimulator.platform/Developer"
export SDKROOT="$DEVROOT/SDKs/iPhoneSimulator$SDKVER.sdk"
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
export PKG_CONFIG_LIBDIR="$PKG_CONFIG_PATH"
export PREFIX="$HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator"
export AS="$DEVROOT/usr/bin/as"
export ASCPP="$DEVROOT/usr/bin/as"
export AR="$DEVROOT/usr/bin/ar"
export RANLIB="$DEVROOT/usr/bin/ranlib"
export CPPFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -pipe -no-cpp-precomp -I$SDKROOT/usr/include -I$HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/include -I/usr/local/iphone-simulator-$SDKVER/include -m32"
export CFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -arch i386 -pipe -no-cpp-precomp --sysroot='$SDKROOT' -isystem $SDKROOT/usr/include -isystem $HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/include -isystem /usr/local/iphone-simulator-$SDKVER/include -m32"
export CXXFLAGS="-miphoneos-version-min=$SDKVER -std=c99 -arch i386 -pipe -no-cpp-precomp --sysroot='$SDKROOT' -isystem $SDKROOT/usr/include -isystem $HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/include -isystem /usr/local/iphone-simulator-$SDKVER/include -m32"
export LDFLAGS="-miphoneos-version-min=$SDKVER -arch i386 --sysroot='$SDKROOT' -L$SDKROOT/usr/lib -L$HOME/Desktop/MyProjects/cocoa/libqrencode/Debug-iphonesimulator/lib -L/usr/local/iphone-simulator-$SDKVER/lib -m32"
export CPP="$DEVROOT/usr/bin/cpp"
export CXXCPP="$DEVROOT/usr/bin/cpp"
export CC="$DEVROOT/usr/bin/gcc-4.2"
export CXX="$DEVROOT/usr/bin/g++-4.2"
export LD="$DEVROOT/usr/bin/ld"
export STRIP="$DEVROOT/usr/bin/strip"
./configure --prefix="$PREFIX" --host="i386-apple-darwin9" --enable-static --disable-shared
Thanks to DarkDust again for all your help!
再次感谢 DarkDust 的所有帮助!
回答by DarkDust
Sorry for not helping you with your compile problems, but it seems you have a much bigger issue: libqrencode is licensed under LGPL, and many consider it to be incompatible with Apple's terms(another article about GPL/LGPL being incompatible with Apple's terms). So you might put yourself at a legal risk by using libqrencode in your app.
很抱歉没有帮助您解决编译问题,但您似乎有一个更大的问题:libqrencode 在 LGPL 下获得许可,许多人认为它与 Apple 的条款不兼容(另一篇关于 GPL/LGPL 的文章与 Apple 的条款不兼容)。因此,在您的应用程序中使用 libqrencode 可能会使您面临法律风险。