How to use -nostdlib?

Brian Drummond
Thu May 14 08:45:00 GMT 2015

On Wed, 2015-05-13 at 22:19 -0500, Peng Yu wrote:
> I tried the following commands which are supposed to work, but didn't.
> Does anybody know what is wrong? Thanks.
> $ cat test.c
> #include <stdio.h>
> int main() {
>   printf("Hello World\n");
> }
> $ gcc -nostdlib -o test_nostdlib test.c
> /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to
> 0000000000400144
> /tmp/ccZBi5G9.o: In function `main':
> test.c:(.text+0xa): undefined reference to `puts'
> collect2: error: ld returned 1 exit status

It isn't supposed to work. If you look at the linked example, that one
doesn't call printf. Comment out the printf call and this should link as
expected. Restoring "puts" (used by printf) without using the standard
libraries is a more advanced subject.

-- Brian

More information about the Gcc-help mailing list