How do I configure and build ICU so I can link it to my iPhone app?
I'm maintaining an iPhone app that uses a SQLite database. Now I have to compile with ICU support enabled (SQLITE_ENABLE_ICU
). I've got the latest ICU source.
The configure
flags I'm using:
./configure --target=arm-apple-darwin --enable-static --disable-shared
After that, running gnumake
runs without errors.
Then I add the libraries to my Xcode project. But when I build, I get 50 lines of this:
Undefined symbols:
"_uregex_close_48", referenced from:
_icuRegexpDelete in libsqlite3-cerod.a(sqlite3_cerod.o)
"_ubrk_current_48", referenced from:
_icuNext in libsqlite3-cerod.a(sqlite3_cerod.o)
"_ucol_strcoll_48", referenced from:
_icuCollationColl in libsqlite3-cerod.a(sqlite3_cerod.o)
"_u_isspace_48", referenced from:
_icuRegexpFunc in libsqlite3-cerod.a(sqlite3_cerod.o)
"_utf8_countTrailBytes_48", referenced from:
_utf8_countTrailBytes_48$non_lazy_ptr in libsqlite3-cerod.a(sqlite3_cerod.o)
(maybe you meant: _utf8_countTrailBytes_48$non_lazy_ptr)
"_ubrk_next_48", referenced from:
_icuNext in libsqlite3-cerod.a(sqlite3_cerod.o)
Any idea what I'm doing wrong?
Edited to add:
When I add the libraries to the project (right-click on the project name, then Add Existing...), I get this:
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicudata.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicui18n.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicuio.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicule.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libiculx.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicutu.a, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /Users/eric.grunin/dev/iOS/icu/source/lib/libicuuc.a, file was built for unsupported file format which is not the architecture being linked (i386)
That's why I think I'm building the library incorrectly. It's as if it's saying:
- It can't tell what architecture the .a files are built for
- libsqlite3-cerod.a is built for i386
I don't understand either possibility, but I'm new to iPhone development.
Edited to add
I tried @Sergio Moura's solution, and got the error mentioned in my comment.
I tried @sergio's solution, which built. But I'm still getting the equivalent errors, starting with:
ld: warning: in /Users/eric.grunin/dev/iOS/icu/iosbuild/lib/libicudata.a, file was built for unsupported file format which is not the architecture being linked (i386)
Might I be telling Xcode the wrong thing? I'm right-clicking on the project name, then selecting "Add->Existing File", and choosing the six or seven .a
files from /icu/iosbuild/lib
. Is that the correct process?
Note:
@sergio is recommending configure --host=arm-apple-darwin
, @Sergio Moura is using configure --target=arm-apple-darwin
. Neither made a difference, alas.
Edit #2
Targeting the device (instead of the emulator) solved all but one of the link errors! Here's what's left:
Undefined symbols for architecture armv6:
"___sync_synchronize", referenced from:
_ucol_initUCA_48 in libicui18n.a(ucol_res.ao)
udata_getHashTable() in libicuuc.a(udata.ao)
_umtx_init_48 in libicuuc.a(umutex.ao)
_initCache in libicuuc.a(uresbund.ao)
icu_48::hasService() in libicui18n.a(coll.ao)
_ucol_initInverseUCA_48 in libicui18n.a(ucol_bld.ao)
icu_48::locale_set_default_internal(char const*)in libicuuc.a(locid.ao)
...
ld: symbol(s) not found for architecture armv6
This was preceded by a cascade of these warnings:
ld: warning: CPU_SUBTYPE_ARM_ALL subtype is deprecated: /Users/eric.grunin/dev/iOS/icu/iosbuild/lib/libicuuc.a(resbund.ao)
ld: warning: CPU_SUBTYPE_ARM_ALL subtype is deprecated: /Users/eric.grunin/dev/iOS/icu/iosbuild/lib/libicuuc.a(ustrfmt.ao)
Edit #3
@Stephen R. Loomis's suggestion that I change #define U_HAVE_GCC_ATOMICS
from 1
to 0
(in platform.h
) made no difference, alas. I also realized that the last line of the error (not found for architecture arm6
) didn't mean it would work for arm7
, it was only an fyi that this was a cross-compile. When I specified an arm7
build, it failed with the same messages. Alas.
Edit #4
Success!
Summary: @sergio's build flags were essentially correct. I added -DU_HAVE_GCC_ATOMICS=0 to the ios build's CFLAGS. The one thing I had been doing wrong was not realizing I needed to cross-compile the library to create a device build.
I haven't tried to repeat this for the simulator, but that's outside the scope of my question.
Special thanks to Steven R. Loomis for pitching in, and to Sergio Moura for getting things rolling.
re: sync synchronize: someone may be lying about the atomics or there's some gcc library needed. try
#define U_HAVE_GCC_ATOMICS 0
at the top of icu/source/common/unicode/uconfig.h ( note: ARM seems to have a weak memory model, so this change will result in more locking/unlocking than would otherwise be necessary, but still safe. )I used iOS SDK version 6.1, with clang and building against the c++11 standard library. I found that setting environment variables such as CXXFLAGS had no affect, and trying to pass them to 'configure' on the command line seemed to break it entirely. I ended up making a clang, clang++, and ld scripts that would allow me to pass in additional parameters. For example, my clang script:
The others are identical, but substitute clang++ and ld where appropriate, and use MORE_CXXFLAGS and MORE_LDFLAGS respectively.
Lastly, I made this script, which does a host build, a simulator build, and an iOS build. Note that the simulator build includes debug info, and the iOS version is -O2 optimized. It then lipos the libs (makes a universal binary) and copies them, along with the include folder, to a target specified by INSTALL_PATH. Lines 3-5 are used to configure the script. Place all 4 of these scripts in the same folder and execute this last one from the command line:
If you have the source, do you really need to link the library? Just add the sources to your XCode project and you should be good to go...
If you really want to build a library, I'd suggest you to create an XCode project for the library with the iPhone as your target, and link that library to your code, as your library is built to run in your MacOS computer (according to your error logs).
EDIT
To build it from the command line and assuming you're not using iOS 5 (because of your version of XCode), I've borrowed and adapted this set of instructions to properly setup the flags to configure and build correctly the binaries to your platform from here:
Please, properly set the IOS version on the first two instructions to the correct value of your environment.
If you're going to use iOS 5 SDK, you'll need to change the name of the compiler binaries since they've changed.
EDIT:
I can confirm that if you do, as Steven R. Loomis suggests:
set U_HAVE_GCC_ATOMICS to 0 in icu/source/common/unicode/platform.h
make distclean
sh cross_configure.sh (using my script, i.e., if you are using it)
the problem should be solved. Indeed, without doing this, the built libraries contain the offending undefined symbol:
After following the above suggestion, this is the result for the same command:
So, definitely, the offending symbol is not present in the binaries.
END EDIT.
Cross-compiling libicu for iOS requires two separate steps:
compiling libicu for your host (MacOS) in a build directory;
cross-compiling libicu for iOS by also specifying the cross-compile directory.
The reason why step 1 is necessary is that libicu will bootstrap itself a bit, i.e., it will compile some intermediate tools, which will be then used in the rest of the build process; those tools need to be ran on the host platform, so they are to be available.
Well, all in all, you can follow the steps (1. compile for the host):
Once this is done, it's time to cross-compile (2. compile for iOS):
Where
cross_configure_icu.sh
is a shell script similar to those proposed by Sergio Moura above, but customized for libicu and using the more advanced llvm compiler:In the above script (source),
ICU_PATH
is an absolute path because libicu configure so requires for thewith-cross-build
option. Again, check your values for the SDK and compilers, but this should be ok for 4.3.Finally, you should take into account that Apple has (half) rejected at least one app that was linked against libicu, because it uses reserved APIs. Have a look at this S.O. topic.
EDIT:
happy the hear that you could compile!
now, to the linking problem.
first of all, please check that the libicu libraries are in the correct format:
output should be (for any of the libs):
If this is fine, then next question: are you building for the simulator or for the device? simulator needs i386 libraries, device arm libraries... from the error message you show:
it seems to me that you are building against the simulator... for that you will need "normal" macos x libs...