以samba讲解如何交叉编译开源库用于iphone平台

网上很多开源库,在iphone开发的时候,如果能够直接调用这些库,那么开发软件就很方便,而一般开源库都用automakefile管理工程,编译过程一般是

./configure 

 (运行configure:完了以后要修改一下include/config.h,加上 #define HAVE_IFACE_IFCONF 1)

make

make install

下面以samba讲解

首先设置编译环境

在终端上输入(红色为我的安装路径)



1>

export LDFLAGS="-L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib -L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/system"

2>

export PATH=$PATH:/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/


3> 可以先./configure --help 选择需要的配置

./configure --prefix=/Users/kanoutouyetao/samba-iphone  CC=arm-apple-darwin10-gcc-4.2.1 --enable-static --without-readline --with-libsmbclient --disable-cups -disable-iprintsamba_cv_CC_NEGATIVE_ENUM_VALUES=yesCPPFLAGS="-I/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/gcc/arm-apple-darwin10/4.2.1/include -I/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/include" --host=arm-apple-darwin10 --build=x86


然后这些在/Users/kanoutouyetao/samba-iphone里面有库和头文件,然后你就可以参考有一篇文章将库和头文件添加项目中,如果你只是做ios Device工程,那么上面的库够用,如果你还做应用,带界面iphone/ipad仿真器的,那么会遇见error


ld: warning: ignoring file /Users/kanoutouyetao/samba-iphone/lib/libsmbclient.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

Undefined symbols for architecture i386:

  "_smbc_unlink", referenced from:

      _main in main.o

ld: symbol(s) not found for architecture i386

collect2: ld returned 1 exit status

需要再次编译一个iPhoneSimulator.platform平台下i386架构库用于仿真库


也就是说你需要编译两个库,以个是在iphone/ipod等arm体系架构真机上运行的库arm6 arm7 上面编译的就是真机上运行的,仿真器iPhoneSimulator.platform需要i386

再次编译i386库,设置

1>

export LDFLAGS="-L/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/lib -L/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/lib/system"

2>

expor PATH=$PATH:"/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"

3>

 ./configure --prefix=/Users/kanoutouyetao/samba-iphone-Simulator CC="/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2" --enable-static --without-readline --with-libsmbclient  --disable-cups -disable-iprintsamba_cv_CC_NEGATIVE_ENUM_VALUES=yesCPPFLAGS="-I/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/include -I/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include"

但是会出现下面的编译错误:

checking build system type... i386-apple-darwin10.8.0

checking host system type... i386-apple-darwin10.8.0

checking target system type... i386-apple-darwin10.8.0

LIBREPLACE_LOCATION_CHECKS: START

LIBREPLACE_LOCATION_CHECKS: END

LIBREPLACE_CC_CHECKS: START

checking for gcc... /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 

checking whether the C compiler works... no

configure: error: in `/Users/kanoutouyetao/samba-3.5.11/source3':

configure: error: C compiler cannot create executables

See `config.log' for more details.

查看config.log

ld: warning: symbol dyld_stub_binder not found, normally in libSystem.dylib

Undefined symbols for architecture x86_64:

  "_exit", referenced from:

      start in crt1.10.6.o

ld: symbol(s) not found for architecture x86_64

collect2: ld returned 1 exit status

原来库是32位的,但是链接却发现是64位的,

 如何知道应用程序是64位还是32位的?可使用file命令查看.

bash-3.2# file crt1.10.6.o 

crt1.10.6.o: Mach-O universal binary with 4 architectures

crt1.10.6.o (for architecture armv6):Mach-O object arm

crt1.10.6.o (for architecture x86_64):Mach-O 64-bit object x86_64

crt1.10.6.o (for architecture i386):Mach-O object i386

crt1.10.6.o (for architecture ppc7400):Mach-O object ppc

发现库是有i386的,但是却现实没有链接上,在http://blog.yening.cn/2006/11/01/188.html文章中,发现答案,

 最关键的东西是需要告知gcc编译器编译的平台是 i386,CFLAGS 必须添加 -m32 参数.

 [关于gcc编译参数的官方文档]

./configure --prefix=/usr --cc="gcc -m32" --target="i386-linux"

参考上面。所以./configure加上-m32参数。如下

3>

./configure --prefix=/Users/kanoutouyetao/samba-iphone-Simulator CC="/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 -m32"   --enable-static --without-readline --with-libsmbclient  --disable-cups -disable-iprintsamba_cv_CC_NEGATIVE_ENUM_VALUES=yesCPPFLAGS="-I/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/include -I/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include"

  

然后编译通过。。。。Good Luck! 


http://blog.k-res.net/设计开发/cc/在mac-osx下编译用于ios的freetype静态库.html

你可能感兴趣的:(以samba讲解如何交叉编译开源库用于iphone平台)