intelliproject logo

Location: Mobile Development - iPhone    License: The Intelliproject Open License (IPOL)

Compiling and using CURL on iPhone OS

Posted by Silviu Caragea

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.

Skill: Advanced

Posted: 17/03/2009

Views: 23562

Rating: 4.71 /5

Popularity: 3.98

Sign Up to vote for this article

Introduction

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.

Compile libcurl as static library

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.

Creating the libcurl static libray in Xcode

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.

new_project.png

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 .

add_files.png

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

add_build_settings.png

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 .

Creating universal libraries with lipo on Mac OS X

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

Link curl in your projects

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.

link.png

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.

app.png

Conclusion

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.

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

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: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Posted by joe1123 at 09/04/2009 17:03
I'm confused about where in these steps you actually end up with a .a framework file to link into a project. If you could show that more clearly it'd be much appreciated as this is exactly what I'm looking for!
Posted by Silviu Caragea at 10/04/2009 11:23
"After you have completed all this steps you can compile your library for the iPhone Simulator (x86) and for your iPhone OS (armv6) ."
After compiling process your compiler will generate a static library(.a) which can be used in your projects.
Posted by Ken Sayward at 15/04/2009 10:54
Any hope of compiling cURL with SSL support?
Posted by Silviu Caragea at 15/04/2009 13:01
it's a little bit complicated but not impossible .. I've tried that but I give up .
Posted by matthew858 at 26/04/2009 19:01
How do we go about using this for authentication (ie. Twitter) in iPhone programming?
Posted by Silviu Caragea at 27/04/2009 10:31
As long you are not using a secure connection (https) You can login to any website.
If you want , you can take a look in CURL samples which comes with the libcurl source code .
There you can find examples on how to send data via POST.
Posted by Vincent Delort at 05/05/2009 09:14
Hi.
I don't understand, I followed every step here, and when I build I get a several thousand errors.

I tried curl in a terminal and it works well, so the error is not from curl itself.

I added in my project (cocoa static library) all the beforehand compiled files (and they look just like the ones from your own libcurl folder), typed -DHAVE_CONFIG_H - Ilibcurl/include -Ilibcurl/lib line in the appropriate box. And then when I try to build, a ton of errors.

Maybe I didn't really understand what you meant by "After you have completed all this steps you can compile your library for the iPhone Simulator (x86) and for your iPhone OS (armv6) ." Where and how shall I do that?


thanks
Posted by Silviu Caragea at 05/05/2009 11:09
Take a look in my attached static library project (Download libcurl XCode project for iPhone-2.8 Mb) and see if you have the same project structure (maybe you have skipped any files ).
Probably your project have not the same folder structure like mine and in this case you should change the -Ilibcurl/include and -Ilibcurl/lib paths (which are relative to your project folder ) to your libcurl source file path .
Also if you want to skip the compiling process you can download my attached static library and use it in your projects.

Posted by Adam at 06/05/2009 03:10
Hi Silviu,

Thanks a lot for this article! It really helped me out a few months ago when I needed to do this.

However, I have just started compiling my project using the iPhone OS 3.0b4 SDK and although the app still works, I get a whole bunch of warnings like the following...

libcurl(easy.o) object file '...(easy.o)' doesn't contain architecture information for armv6

Do you have any ideas about this?

Posted by Silviu Caragea at 06/05/2009 09:35
are you linking your project with the lipo libraries ?
If yes , try to use the original version of the library ... ,
When the iPhone 3.0 sdk will be up and ready for AppStore building I will update my article.
Posted by AlexCorvis at 01/06/2009 05:03
Silviu Caragea, Thank You!!! It's work!!!
Thank you!
Posted by Ankush Gupta at 12/06/2009 03:30
I need to develop SSH client in iPhone. Do libcurl can help or I need to go with libssh.
Posted by Silviu Caragea at 13/06/2009 11:14
It's useless to try to build a ssh client for iPhone based on CURL ,.
Try to compile openssh or any other ssh client for linux .
Posted by Manik Kakar at 14/01/2010 15:13
Silviu, I have downloaded the curlTest works great. However, when I create a new project and link the curl library with that project, I get "Not of desired architecture"
I am developing on SDK 3.1.2 and the curlTest compiles fine with that SDK just not when libcurl.a is linked to a new project.
Any idea?
Posted by Silviu Caragea at 16/01/2010 12:26
probably you are compiling on armv7 (iPhone3GS).
When I have created this project this architecture was not available .

All you must to do is to compile the library as FAT library (a fat library contain code machine for more arhitecture , in our case armv6 for iPohne 3G , armv7 for 3GS and i386 for simulator).
Posted by rhodesy22 at 01/05/2010 04:44
Thank you for the tutorial - it's proven very helpful. I'm coming across a problem though, when I compile in XCode I get

size of array '__curl_rule_01__' is negative in
libcurl/include/curl/curlrules.h:144:0

for simulator and iphone device.

I started off just using your build but it's an old version sadly and doesn't provide support for the USERNAME and PASSWORD options. I'd rather use them than add them in the URL.

Have you any suggestions? Thanks!
Posted by dcteris at 01/09/2010 19:09
Thanks, I want to have a try.
Posted by tuukka at 06/09/2010 07:36
Thanks for providing this framework! You're great!
Posted by Gregory James Ray at 30/09/2010 13:12
I also found the size of array '__curl_rule_01__' is negative compile time error. I was able to fix it by changing two defines in include/curl/curlbuild.h I changed both CURL_SIZEOF_LONG and CURL_SIZEOF_CURL_OFF_T from 8 to 4. I believe this is due to the fact that it is building for 64bit system on Snow Leopard but in reality we are deploying to a 32 bit platform. Hope this is helpful.
Posted by Quynh Bui at 16/11/2010 21:28
How to compile the library as FAT library (a fat library contain code machine for more architecture , in our case armv6 for iPhone 3G , armv7 for 3GS and i386 for simulator as well as iPhone 4)?
Posted by raviraj bhatane at 07/03/2011 12:53
When I tried running curlTest app on iOS4.2 simulator. It crashed and I got following message on console.

Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
close$UNIX2003 called from function Curl_getaddrinfo in image curlTest.
If you are encountering this problem running a simulator binary within gdb, make sure you 'set start-with-shell off' first.
Posted by Silviu Caragea at 08/03/2011 11:57
Since I wrote this article many iOS SDK had changed. I think this article was written for iOS SDK 2.2.1 :)). You can update the curl source code and follow my steps to compile a fresh build.
Posted by risma dewi at 29/05/2011 08:28
hi Silviu...
i have try all of this step, i tried this on IOS 4.2, but when i try to build in Xcode, that give me thousand errors. My OS is MacOSx 10.6.6

can you help me?

it really make me crazy for a week
Posted by wink alott at 29/06/2012 03:12
How do I build cURL with SSL support? with OpenSSL?
Posted by Paresh Vaghela at 13/07/2012 16:28
Hi Silviu...

I am running demo for iOS and run project in simulator, with textfiled = www.google.com and press download button it is generating below error.

"Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
close$UNIX2003 called from function Curl_getaddrinfo in image curlTest."
Posted by Silviu Caragea at 27/07/2012 19:34
Hello . This article is pretty old. Maybe it need adjust.

Sign up to post message on the article message board!