Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Why can the compiler not find some includes when building for ARM architecture?

I am trying to make use of a C library in an iPhone project. I am very green with iPhone development. My Library

I have been battling for days now to try and get this library build into a static library that I can use for both the simulator (i386) and ARM7.

Using the the library's include configuration and makefile I can build the library without issue. However if I edit the makefile to try and build this same library but target the armv7 architecture I get many errors. The errors seem to be reporting that some header files cannot be located.

So does the compiler attempt to look in different places for header files depending on the target architecture?

This is the make file that I have edited to attempt to build for the armv7:

 # $Id: Makefile.in 62 2005-03-09 21:11:53Z gyunaev $
CC = /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.2.1 
CFLAGS = -Wall -DIN_BUILDING_LIBIRC -O3 -DENABLE_THREADS -D_REENTRANT
AR=ar cr
RANLIB=ranlib
INCLUDES=-I../include


OBJS = libircclient.o

all:    lib

lib:    libircclient.a

install: lib
    -mkdir /usr/local/include
    -mkdir /usr/local/lib
    cp ../include/libircclient.h /usr/local/include/libircclient.h
    cp ../include/libirc_errors.h /usr/local/include/libirc_errors.h
    cp ../include/libirc_events.h  /usr/local/include/libirc_events.h 
    cp libircclient.a /usr/local/include/lib/libircclient.a

$(OBJS): utils.c dcc.c errors.c portable.c sockets.c colors.c

libircclient.a: $(OBJS)
    $(AR) libircclient.a $(OBJS)
    $(RANLIB) libircclient.a

clean:
    rm -f libircclient.a $(OBJS)

distclean: clean
    -rm -f Makefile

.c.o:
    @echo "Compiling  $<"
    @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<

Here is a sample of the compilation errors I am experiencing:

Compiling  libircclient.c
In file included from /usr/include/sys/_types.h:33,
             from /usr/include/_types.h:27,
             from /usr/include/stdio.h:64,
             from portable.c:18,
             from libircclient.c:15:
/usr/include/machine/_types.h:36:24: error: arm/_types.h: No such file or directory
In file included from /usr/include/_types.h:27,
             from /usr/include/stdio.h:64,
             from portable.c:18,
             from libircclient.c:15:
/usr/include/sys/_types.h:94: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__darwin_blkcnt_t’
/usr/include/sys/_types.h:95: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__darwin_blksize_t’
/usr/include/sys/_types.h:96: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__darwin_dev_t’

Am I going about this all wrong? Is editing the makefile dumb? :) How do you unix ninjas handle this situation? Some research has lead me to believe that I need to create a universal library...

Thanks!

like image 659
Nick Avatar asked May 04 '11 03:05

Nick


2 Answers

The problem is that the include file in /usr/include are for Mac OS X, not for the iPhone/iPad. If you want to get things to work you will have to add (at least) the flags that Kay mentioned in her comment...

A much easier way to go about this is to use XCode. Just create a new project (choose the Cocoa Touch Static Library) and add the source files. According to the Makefile you will need to add: utils.c dcc.c errors.c portable.c sockets.c colors.c

Hope this helps.

You might also check out IRCClient which is a Objective-C wrapper for the library you are using. No point reinventing the wheel!

ADDITIONAL INFORMATION 5/8/11

Turns out the Objective-C wrapper is aimed at Mac OS X not iOS

Here's what you need to do to get this to compile:

Get the sources fresh:

svn co https://libircclient.svn.sourceforge.net/svnroot/libircclient libircclient

Change into the libircclient/trunk/libircclient/src directory and run

./configure

Note: this is a slightly dirty trick. This is really configuring for Mac OS X but most of the times things are similar enough for this to work. This generates a file called config.h in include.

Fire up XCode and choose to create a new Cocoa Touch static library. When prompted create a new directory libircclient/trunk/libircclient/iOS and save the new project there.

Add the file libircclient.c from libircclient/trunk/libircclient/src to the project. It turns out that this file includes all the other C files.

Open your project settings and add ../../include to you search header path.

Build and enjoy!

like image 50
idz Avatar answered Sep 28 '22 06:09

idz


As Kay indicated in a comment, if you are trying to build from the command line (or via Make), you must specify the -isysroot $(SDKROOT) flag in order to get the correct headers (otherwise you pick up the host Mac OS X headers, which do not necessarily support ARM).

The easiest thing is to build with XCode, or find a version of the library designed to build for iOS, but if you must use an existing Make-driven build system, you can adapt it to build for iOS by setting:

TARGETSDK = /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk
CC = xcrun -sdk $(TARGETSDK) gcc
CFLAGS = -arch armv7 -isysroot $(TARGETSDK) ...

You will want to use similar xcrun commands for other tools used in your build.

like image 30
Stephen Canon Avatar answered Sep 28 '22 05:09

Stephen Canon