How can I get a static C compiler?

12,101

Solution 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.

Solution 2

You can also retrieve a precompiled version with static-get

static-get -x gcc
Share:
12,101

Related videos on Youtube

math4tots
Author by

math4tots

Updated on September 18, 2022

Comments

  • math4tots
    math4tots almost 2 years

    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.

    • ctrl-alt-delor
      ctrl-alt-delor over 7 years
      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).
  • math4tots
    math4tots over 12 years
    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"?
  • Kyle Jones
    Kyle Jones over 12 years
    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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    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.
  • Kyle Jones
    Kyle Jones over 12 years
    Yes, I understand that. Producing a staticly linked compiler is a necessary first step, and one that the question seems to be asking about.
  • OrangeDog
    OrangeDog almost 12 years
    @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.
  • Владислав Щербин
    Владислав Щербин over 10 years
    If using something huge but standard like libc, statically linking against everything might be a bad idea.
  • ctrl-alt-delor
    ctrl-alt-delor over 7 years
    @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.