I'm playing around with chroot environments, and I'd like to have a portable C compiler so that I can easily set up some basic build-tools in each environment without having to move too many libraries around.

Gcc seems pretty bloaty for what I want to do, but if it's reasonably easy to build a static gcc I wouldn't mind a few dozen megabytes.

I am also looking at the Tiny C Compiler, which is smaller but still looks like it's got an impressive feature set. However, running ./configure --help in the source directory doesn't list any option for building tcc statically, and I'm not sure how it might be done otherwise.

  • To answer you secondary concern, memory usage, here are a few solutions: You can create shadow directories that only contain hard links to GCC; You can use docker containers (containers are more secure than chroot, and docker provides a union filesystem, that will allow you to save memory). – ctrl-alt-delor Jan 19 '17 at 8:57

Building a static binary should be as simple as running gcc with -static, or if ld is being called directly use -Bstatic. Try

CFLAGS=-static make

after running configure. If it fails, the results will be obvious, e.g. rafts of undefined references at link time.

  • 1
    I tried make CFLAGS=-static just now. Make prints some warning about dlopen, then when I try to run the executable in the chroot environment, I get /usr/lib/crt1.o .. crti,o .. crtn.o not found errors. Is that what you meant by "undefined references at link time"? – math4tots Feb 18 '12 at 0:56
  • The errors I expected would have been due to missing static versions of libraries, like crt1.o, but the linker would complain immediately. If you're seeing the errors at runtime, then the binary wasn't staticly linked. – Kyle Jones Feb 18 '12 at 0:59
  • I think you misunderstood the question: math4tots wants a C compiler that works as a standalone binary (or at least as an easily-identifiable set of files) not a C compiler that produces standalone binaries. – Gilles 'SO- stop being evil' Feb 18 '12 at 23:31
  • 1
    Yes, I understand that. Producing a staticly linked compiler is a necessary first step, and one that the question seems to be asking about. – Kyle Jones Feb 19 '12 at 0:44
  • 1
    @Gilles The best way to get a C compiler as a standalone binary is to build it from source using a complier you already have. – OrangeDog Aug 14 '12 at 15:36

You can also retrieve a precompiled version with static-get

static-get -x gcc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.