uclibc (or libuclibc, or µclibc ; pronounce you-see-lib-see, or maybe micro-lib-c) is a small GNU libc replacement for embedded systems. For more details about features etc., check
their site ; I will just explain here how to use it in a cross-compilation
ToolChain.
I will use Debian packages here, but the proposed solution runs on any platform. I will assume that you want to cross-compile for mips architecture (just replace mips with your target arch in the following text).
All the following indications can be run as a regular user, and preferably in a separate, empty directory.
- first, get the uclibc packages from the nearest Debian mirror. Look in the directory ftp://ftp.fr.debian.org/debian/pool/main/u/uclibc (use your mirror instead) ; there should be files named libuclibc0_X.Y.Z-A.B_mips.deb and libuclibc-dev_X.Y.Z-A.B_mips.deb. Fetch them (take care to get matching files, that is, with same version and release number).
- then, unpack them ; if you have dpkg, just run dpkg -x libuclibc0_X.Y.Z-A.B_mips.deb uclibc (and same thing for libuclibc-dev). That will create a uclibc directory and put the files of the packages in it. If you don't have dpkg, do mkdir uclibc ; ar x libuclibc0_X.Y.Z-A.B_mips.deb ; tar -C uclibc -zxf data.tar.gz ; rm -f debian-binary data.tar.gz control.tar.gz and the same thing for libuclibc-dev. Yes, Debian packages actually are ar archives (like .a libraries used for compilation), and you can extract them with regular tools (unlike rpm packages which need rpm2cpio).
- move uclibc/usr/lib to lib, and uclibc/usr/include to include. You can now delete the uclibc directory.
Now, to use uclibc when compiling, do the following :
mips-linux-gcc -Iinclude foobar.c -nostdlib lib/crt[0in].o -Llib -lc -o foobar. Explanations :
- -Iinclude : you have to use uclibc includes, because some functions are in fact #defines and don't resolve to the same real names in GNU libc and uclibc. If when compiling you see problems like "unresolved symbol __xstat", that might be it.
- -nostdlib : just prevents linking with the GNU libc.
- lib/crt[0in].o : there might be a cleaner way to include those files ; if you know, tell me ! Those files, as far as I understood, are responsible for libc initialization and cleanup.
- -Llib -lc : actually links against uclibc.
You might want to add
-static to compile statically (to remove dependancy to uclibc on the target platform), and
-s to automatically strip symbols (for a slightly smaller footprint).
Some useful notes : it seems that some very simple and trivial functions, like
atoi, weight in fact quite a lot in uclibc ; actually using
atoi in a small program bumped it size from 10k to 200k (when linked statically, of course ; dynamically there is no difference, but you need uclibc on your target, then).