Compiling tcc with dietlibc

by Martin Monperrus

Tcc (aka Tiny C Compiler, or tinycc) is a very lightweight C compiler. Dietlibc is a lighweight libc (as opposed to GNU libc).

It is possible to compile tcc itself using dietlibc. First download the source code of tcc and install dietlibc (apt-get install dietlibc-dev).

You have to do the following.

In config.mak, modify the compiling and linking flags:

  CFLAGS=-I/usr/include/diet/ -D__dietlibc__ -DCONFIG_TCC_STATIC 
  LDFLAGS=-L/usr/lib/diet/lib/ -nostartfiles

CONFIG_TCC_STATIC says that dynamic loading (libdl) should not be used. However, there is a dependency to libdl at linking time (-ldl).

In `Makefile’:

crt0.o is the startup file which provides function _start as entry point. Function _start of dietlibc is in a file called start.o. We copy it to crt0.o (since this name is hard coded in tcc, see http://www.monperrus.net/martin/compiling-c-code-with-dietlibc-and-tcc):

  $ cp /usr/lib/diet/lib/start.o crt0.o

Then, simply build tcc:

  $ make

Results: Using dietlibc, the resulting tcc weighs 211k and libtcc1.a 16k. Using the GNU libc, it is respectively 583k and 52k.

Tagged as: