陈斌彬的技术博客

Stay foolish,stay hungry

How to Compile Libidn for the iPhone and OS X

Introduction

A precompiled fat binary, libidn.a, with support for the iPad, iPhone, iPod touch, iOS Simulator and OS X is included in xmppframework.

The libidn source code is included in the XMPP framework libidn directory. The code was obtained from the libidn project page.

Compiling for iOS

Expand the libidn source code from its tar or tgz format. Open the terminal, and cd to the expanded directory. It only takes 3 commands to compile the source. (Notice that the first command is one big long command.)

./configure --prefix=/usr/local/iphone --host=arm-apple-darwin --disable-shared CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin9-gcc-4.0.1 CFLAGS="-arch armv6 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -fmessage-length=0 -fvisibility=hidden -miphoneos-version-min=2.0 -gdwarf-2 -mthumb -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk" CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar

make

sudo make install

You’ll find the libraries in /usr/local/iphone/lib
Reference: http://discussions.apple.com/thread.jspa?threadID=1546383

Note: The above command may be a bit dated. Every time I update the command to reflect the latest compiler / iOS SDK, Apple goes and updates the SDK or compiler tools. But this should get you on the right track.

ArmV7

The following can be used to compile the library for armv7 using GCC 4.2 and targeting iPhone OS 3.1.2:

./configure --prefix=/usr/local/iphone --host=arm-apple-darwin --disable-shared CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 CFLAGS="-arch armv7 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -O0 -Wreturn-type -Wunused-variable -Wunused-value -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.2.sdk -fvisibility=hidden -gdwarf-2 -mthumb -miphoneos-version-min=3.1.2 " CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar

Another example for armv7 using llvm-gcc 4.2 and targeting iPhone OS 5.1 with SDK 6.0 using Xcode 4.5

./configure --host=arm-apple-darwin --disable-shared CC=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 CFLAGS="-arch armv7 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -O0 -Wreturn-type -Wunused-variable -Wunused-value -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk -fvisibility=hidden -gdwarf-2 -mthumb -miphoneos-version-min=5.1 " CPP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-cpp-4.2 AR=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
make clean
make
cp lib/.libs/libidn.a libidn-armv7.a

If you want to compile for armv7s just replace armv7 with armv7s and copy the lib as explained above

For iPhone Simulator 6.0 or 5.1:

./configure --host=i686-apple-darwin --disable-shared CC=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 CFLAGS="-arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -O0 -Wreturn-type -Wunused-variable -Wunused-value -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.0.sdk -fvisibility=hidden -gdwarf-2 -mthumb -miphoneos-version-min=5.1 " CPP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-cpp-4.2 AR=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ar
make clean
make
cp lib/.libs/libidn.a libidn-i386.a

After having the three libs you should put them together in a fat binary:

lipo -create libidn-armv7s.a libidn-armv7.a libidn-i386.a -output libidn.a

Compiling for OS X

X 86

To Download and Compile Libidn for OS X run the following commands from the terminal

mkdir libidn
cd libidn
curl -O http://ftp.gnu.org/gnu/libidn/libidn-1.25.tar.gz
tar xzvf libidn-1.25.tar.gz
cd libidn-1.25
./configure && make
cd ..
mkdir x86_64
cp libidn-1.25/lib/.libs/libidn.a x86_64/
rm -rf libidn-1.25/

Fat Binaries

You may wish to compile the library for multiple architectures and create a fat binary. This is helpful because then you can link to the same file whether you’re compiling for the desktop, simulator or iphone device.

Compile all the different versions you want, put them in the same folder, and then use a command like this:

lipo -create libidn_x86.a libidn_armv6.a libidn_armv7.a -output libidn.a