0

I am trying to compile a simple "hello world" program using a complete and stable GCC 4.4.0. Every thing is OK when I try to compile a dynamic executable but when I try to compile the static executable one, I get error "undefined reference to printf". It's the same in every program I try to compile in static mode. It can not recognize libc functions. Even with "-nostdlib" or "-lc" it's the same! Also "-L ${LD_LIBRARY_PATH}" does not fix it.

./ccL10382.o: In function `main':
test.c:(.text+0x4): undefined reference to `printf'
collect2: ld returned 1 exit status

What should I do?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alireza_fn
  • 884
  • 1
  • 8
  • 21
  • with/without `-nostdlib` I got same error – alireza_fn Oct 20 '13 at 16:13
  • I guess -static option does not work for you, does it? `gcc -Wall -static -g -o test test.c` – Gooseman Oct 20 '13 at 16:34
  • `# gcc -Wall -static -g -o test test.c test.c:2: warning: return type of 'main' is not 'int' /system/android-gcc-4.4.0/sysroot/usr/lib/crtbegin_dynamic.o: In function `_start': bionic/libc/arch-arm/bionic/crtbegin_dynamic.S:(.text+0x10): undefined reference to `__libc_init' ./ccx11630.o: In function `main': /data/a/ts/test.c:2: undefined reference to `printf' collect2: ld returned 1 exit status` – alireza_fn Oct 20 '13 at 16:39
  • 2
    possible duplicate of [Static linking glibc](http://stackoverflow.com/questions/13187499/static-linking-glibc) – alk Oct 20 '13 at 17:49
  • @alk , I can not find any similarity between my question and the one in the link you mentioned ! – alireza_fn Oct 20 '13 at 19:06

1 Answers1

0

At the first, look into libc.a. Looks like it should be in /system/android-gcc-4.4.0/sysroot/usr/lib/. So you need to execute:

nm /system/android-gcc-4.4.0/sysroot/usr/lib/libc.a | grep printf

If printf is present, you have a problem with link parameters. It can be verified by adding flag -v to gcc command line. Check output carefully to know what the libc links exactly.