Recommended

2009-11-01

Tiny, self-contained C compiler using TCC + uClibc

This post presents how I played with a small C compiler and a small libc on Linux. I've combined TCC (the Tiny C Compiler) 0.9.25 and uClibc 0.9.26, compressed with UPX 3.03 to a tiny, self-contained C compiler for Linux i386. You don't need any external files (apart from the compiler executable) to compile (or interpret) C code, and the compiled executable will be self-contained as well.

Here is how download and use it for compilation on a Linux x86 or x86_64 system:

As you can see above, my version the compiler is less than 250k in size, and it doesn't need any library files (such as libc.so) for compilation (as indicated by the strace output above), and the compiled binary doesn't need library files either.

TCC is a very fast compiler (see the speed comparisons on it web site), it is versatile (it can compile the Linux kernel), but it doesn't optimize much: the code it produces is usually larger and slower than the output of a traditional, optimizing C compiler such as gcc -O2.

6 comments:

I tried unsuccessfully to get this working on a build of OpenWrt. Not sure what I've done wrong. It would be nice to understand what configuration I need to do to transfer the resultant tcc exe to an embedded system and make it work.

See here for the error.https://forum.openwrt.org/viewtopic.php?id=26000

It seems to work statically, however I have something wrong with ld, (the double slashes) and I cannot seem to find where in the script or wrapper (or the proper environment variable) that can fix this. I should note that I have also build the uclibc toolchain using buildroot, and have it stored in /usr/local/uclibc, but I cannot seem to get the uclibc ld to run instead of the (/usr/bin/ld) one.

Furthermore, by using my compiler instead of the wrapper, I am able to compile pts-tcc and it displays its help output, however when trying to use it to compile I get:

I -_love_ this - it's solved a big problem for me - trouble is I just found a bug in tcc*.25, that's fixed in '26. I went to try to modify your build script, but I don't have all the right versions of things... any chance of an update?