Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc to use newlib instead of glibc?

Tags:

gcc

newlib

I want to use newlib instead of glibc in order to compile small static binaries. (I do not intend to cross-compile as the binaries are to be used by the same computer.) I believe that I need to compile a separate gcc for this ?

I compiled gcc:

./configure --prefix=/home/myuser/Desktop/gcc-4.4.5 --libexecdir=/home/myuser/Desktop/gcc-4.4.5 --libdir=/home/myuser/Desktop/gcc-4.4.5 --with-gxx-include-dir=/home/myuser/Desktop/gcc-4.4.5 --enable-languages=c --enable-libmudflap --disable-multilib --disable-libssp --disable-nls --with-newlib --with-gnu-as --with-gnu-ld --with-system-zlib
make

It compiled without errors but now when I try to compile a simple Hello World! program it wants to use headers from /usr instead of the path I specified above. These are some of the errors:

In file included from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/stdio.h:34:21: error: stddef.h: No such file or directory
In file included from /usr/include/stdio.h:75,
                 from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:53:21: error: stdarg.h: No such file or directory
In file included from /usr/include/stdio.h:75,
                 from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before 'size_t'
/usr/include/libio.h:364: error: expected declaration specifiers or '...' before 'size_t'
/usr/include/libio.h:373: error: expected declaration specifiers or '...' before 'size_t'

What am I doing wrong ? Is compiling a new gcc necessary or can I use my existing gcc and use newlib instead of glibc ???

like image 303
Ian McCormick Avatar asked Apr 21 '11 19:04

Ian McCormick


People also ask

Does gcc depend on glibc?

Is it possible to compile a program using gcc without depending on glibc? Yes, there are alternative libc versions, such as Musl, uClibc, dietLibc, etc. See their documentation on how to use them. Your problem does not appear to be a GLIBC dependency, but rather a mismatch between your build and your target hosts.

What's the difference between gcc and glibc?

GCC is the C compiler. Glibc is the C library. However, isn't it an absolute necessity for a compiler and the standard library bundled together as a C implementation? For example, the C library contains ABI and compiler specific stuff like <limits.

How do I specify glibc?

To link a program with a test-install glibc, you need to specify the gcc setup. This is done by using the option "-b i486-linuxglibc2". If you are using a configure script, define the $CFLAGS and $LDFLAGS shell variables (by using env/setenv for csh/tcsh, or set/export for sh/bash/etc) before running configure.

What is Libc_nano?

The newlib-nano is an open source C library (libc) targeting embedded microcontrollers (MCU).


2 Answers

You shouldn't need to rebuild gcc for this; you just need to point your existing gcc at the right things (using -I, -L etc.) and tell it not pull in the usual system stuff (using -nostdlib).

The section entitled "Shared newlib" in the newlib README file has runes for building and linking against either shared or static newlib when compiled natively.

like image 171
Matthew Slattery Avatar answered Oct 03 '22 03:10

Matthew Slattery


I would strongly recommend using crosstool-ng to build your GCC toolchain. If you choose x86 followed by "bare metal" as the operating system, you can safely use newlib as a libc.

Note that newlib does not work with an operating system - that is standard things, like IO, won't work out of the box.

like image 38
Yann Ramin Avatar answered Oct 03 '22 05:10

Yann Ramin