8

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.

math4tots
  • 2,665
  • 8
  • 32
  • 42
  • 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 08:57

2 Answers2

3

You can also retrieve a precompiled version with static-get

static-get -x gcc
1

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.

Kyle Jones
  • 14,845
  • 3
  • 40
  • 51
  • 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 00: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 00: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 00: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
  • If using something *huge* but *standard* like `libc`, statically linking against *everything* [might be a bad idea](http://stackoverflow.com/a/816138/777586). – Anko - inactive in protest Nov 12 '13 at 21:47
  • @gilles I think that @kyleJones is suggesting using a compiler that can produce standalone binaries (`gcc -static`), to produce a stand alone `gcc`, by compiling its own source code. – ctrl-alt-delor Jan 19 '17 at 09:01