Firstly, we need the suitable Glibc version
My target Linux with kernel-2.6.9, and my host is Kernel-2.6.32, so I can use Glibc version 2.12.1
Infomation from the project Readme :
When working with Linux kernels, the GNU C Library version from
version 2.4 on is intended primarily for use with Linux kernel version
2.6.0 and later.
wget http://ftp.gnu.org/gnu/libc/glibc-2.12.1.tar.bz2
tar -xjf glibc-2.12.1.tar.bz2
mkdir glibc-2.12.1-build
cd glibc-2.12.1-build/
../glibc-2.12.1/configure --prefix=/usr/local/glibc-2.12.1 --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --enable-kernel=2.6.0 --enable-static-nss
You need add the --enable-static-nss option otherwise you may get the following Warnning when compile your program with this glibc:
warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Error1:
/home/kimi/soft/glibc-2.12.1-build/libc.a(nsswitch.o):(.data+0x8): undefined reference to `_nss_files_getaliasent_r'
/home/kimi/soft/glibc-2.12.1-build/libc.a(nsswitch.o):(.data+0x18): undefined reference to `_nss_files_endaliasent'
/home/kimi/soft/glibc-2.12.1-build/libc.a(nsswitch.o):(.data+0x28): undefined reference to `_nss_files_setaliasent'
/home/kimi/soft/glibc-2.12.1-build/libc.a(nsswitch.o):(.data+0x38): undefined reference to `_nss_files_getaliasbyname_r'
....
It's a common bug since glic-2.3 version,a patchcan fix it
patch -p1 < ../patchfile
make -j4
make install
Secondly, Use -static Option
gcc hello.c -o hello -L/usr/local/glibc-2.12.1/lib -static -nostdlib /usr/local/glibc-2.12.1/lib/crt1.o /usr/local/glibc-2.12.1/lib/crti.o -lm -lc -lgcc -lgcc_eh -lc /usr/local/glibc-2.12.1/lib/crtn.o
Reference
http://blog.csdn.net/force_eagle/article/details/6758045