|
Location: Mobile Development - iPhone License: The Intelliproject Open License (IPOL) Compiling and using CURL on iPhone OSPosted by Silviu CarageaThe goal of this article is to describe how can you compile libcurl as static library for iPhone OS , and how can you use it in your iPhone projects. |
Skill: AdvancedPosted: 17/03/2009Views: 13886Rating: 4.71 /5Popularity: 3.98 |
| Sign Up to vote for this article |
The goal of this article is to describe how can you compile libcurl as static library for iPhone OS , and how can you use it in your iPhone projects.
iPhone OS runs on a armv6 processor architecture . Because of their power saving features, ARM(Advanced RISC Machine) CPUs are dominant in the mobile electronics market, where low power consumption is a critical design goal.
Using the steps described in this article you can compile any open source solution for UNIX, to use in your projects. Of course, depending on the situation, there may be additional operations which need to be performed.
1. Download the libcurl sources from http://curl.haxx.se/download.html . In this tutorial I'm using 7.16.3 library version.
2. Extract the archive content on the desktop. You can do this using Finder (Mac OS file system browser ) or using the following command line in terminal :
tar -xjf curl-7.16.3.tar.bz2
3. Open a terminal (If you have not done it before) and change the current directory to your libcurl path on your desktop (/Users/yourusername/Desktop/curl-7.16.3).
4. In order to see all available compiling options type ./configure --help in your terminal. To use third-party libraries on your iPhone projects you must compile them statically , because you cannot install any dynamic libraries in iPhone without jailbreak.
5. To compile libcurl statically you must type the following command line in your terminal:
./configure --disable-shared --without-ssl --without-libssh2 -without-ca-bundle
6. Now, after you have completed the configure process you can compile your library by typing make in terminal. You are doing this because when you will try to compile the library in Xcode you must add some compiling options in your project settings. Of course also you can see here which files are compiled and which are excluded from build ,In our case you will see that gcc will receive this parameters : -DHAVE_CONFIG_H -I../include -I../lib -I../src -I../lib -I../src -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.c;
7. Copy your source code files to another folder called libcurl .Do not copy all source files because some of them were not compiled (depending on your parameters passed to step 5 . For all files used in your compiling process ,you will see that gcc had generated an object file (.o),If a source code file have no object file associated means that this file is not included in your compiling process. You can take a look in my attachment to see which are the necessary source files which must be include in your Xcode project.
8. After you compile an application or a library you must assure that this is compiled statically, It's not a problem if it use dynamic libraries which are also installed on your iPhone like : libxml , sqlite, etc . You can check that using otool -L librayname command line. The otool command displays specified parts of object files or libraries. -L display the names and version numbers of the shared libraries that the object file uses.
In the following steps I will describe how can you compile your libraries in Xcode .First of all you must create a new "Static Library" project in Xcode . In order to complete this step go to: File - > New project → Static Library → Cocoa static library.

After you created a new project ,you must add your source code files in your project target.
Right click on your project target name (Targets->curl) ->Add->Existing Files . In the next dialog box select the path to your libcurl source code .

In order to compile libcurl with the same options like in the command line compiling, you must add additional informations to your project settings.Right click on your project target ->Get info .In "Build" tab search for OTHER_CFLAGS and add the following options : -DHAVE_CONFIG_H - Ilibcurl/include -Ilibcurl/lib

After you have completed all this steps you can compile your library for the iPhone Simulator (x86) and for your iPhone OS (armv6) .
Of course when you will use this library in your projects you must link them with the appropriated library architecture (x86 for simulator and armv6 for device). This is a little bit frustrating because you must change the linking library every time you change your building platform , but you can fix this issue following the steps described hereinafter .
Mac OS have a special tool called lipo .The lipo command creates or operates on universal (multi-architecture) files. It only ever produces one output file, and never alters the input file. With lipo you can create an universal library which contain one or more architecture versions (in our case armv6 and x86) .
To generate your lipo libraries (libcurl-release.a and libcurl-debug.a) compile your library from Xcode for both platforms (Device and Simulator) and execute the following command in your terminal (the path to the input libraries should be changed):
lipo -create /ipos-release/libcurl.a /ip-sim-release/libcurl.a -output libcurl-release.a
In order to link your applications with your curl library perform the following operation :Right click on Frameworks folder from your workspace ->Add->Existing library .In the next dialog box select your curl library from your computer.

In my attachments you have a little application which use libcurl to grab an URL content and the headers received from the server. Also you will find the lipo versions of my libcurl library (for debug and release) and the library Xcode project.

To use a third party library on iPhone you must compile it statically . Also there are some restriction that you need to consider . For example if you will compile libraries which are using the fork() system call which will spawn a new child process which is an identical process to the parent except that has a new system process ID , the application will exit because you do not have permission to use this function in your applications.
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Silviu Caragea
| Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject. He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy. His programming experience includes: - C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL) - Open Source libraries :CURL & Boost - HTML, CSS - Java (SE,ME) - JavaScript, Ajax, Google Web Toolkit (GWT) - Php, MySQL -Oracle, PL SQL - C# .NET -Objective C, IPhone SDK, Cocoa Location: |
Sign up to post message on the article message board!