Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:05:24+00:00 2026-06-03T21:05:24+00:00

I just learned about the 4k demo scene contest . It consists in creating

  • 0

I just learned about the 4k demo scene contest. It consists in creating a 4KB executable which renders a nice 3D scene. The cited demo was build for Windows, so I was wondering, how one could create 4KB OpenGL scenes on Linux.

A bare “hello world” already consumes 8KB:

$ cat ex.c
#include <stdio.h>

int main()
{
       printf("Hello world\n");
}
$ gcc -Os ex.c -o ex
$ ls -l ex
-rwxrwxr-x 1 cklein cklein 8374 2012-05-11 13:56 ex
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:05:26+00:00Added an answer on June 3, 2026 at 9:05 pm

    The main reason why with the standard settings you can’t make a small tool is that a lot of symbols and references to standard libraries are pulled into your binary. You must be explicit to to remove even that basic stuff.

    Here’s how I did it:

    http://phresnel.org/gpl/4k/ntropy2k7/

    Relevant Options:

    Mostly self-explaining:

    gcc main.c -o fourk0001 -Os -mfpmath=387 \
      -mfancy-math-387 -fmerge-all-constants -fsingle-precision-constant \
      -fno-math-errno -Wall -ldl -ffast-math -nostartfiles -nostdlib  \
      -fno-unroll-loops -fshort-double
    

    Massage:

    strip helps you get rid of unneeded symbols embedded in your binary:

    strip -R .note -R .comment -R .eh_frame -R .eh_frame_hdr -s fourk0001
    

    Code:

    You may have to tweak and trial and error a lot. Sometimes, a loop gives smaller code, sometimes a call, sometimes a force inlined function. In my code, e.g., instead of having a clean linked list that contains all flame transforms in fancy polymorphic style, I have a fixed array where each element is a big entity containing all parameters, used or unused, as a union of all flames as per Scott Draves flame paper.

    Your tricks won’t be portable, other versions of g++ might give suboptimal results.

    Note that with above parameters, you do not write a main() function, but rather a _start() function.

    Also note that using libraries is a bit different. Instead of linking SDL and standard library functions the classy, convenient way, you must do it manually. E.g.

    void *libSDL = dlopen( "libSDL.so", RTLD_LAZY );
    void *libC = dlopen( "libc.so", RTLD_LAZY );
    #if 1
        SDL_SetVideoMode_t sym_SDL_SetVideoMode = dlsym(libSDL, "SDL_SetVideoMode");
        g_sdlbuff = sym_SDL_SetVideoMode(WIDTH,HEIGHT,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
    #else
        ((SDL_SetVideoMode_t)dlsym(libSDL, "SDL_SetVideoMode"))(WIDTH,HEIGHT,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
    #endif
    
    
    //> need malloc, probably kinda craft (we only use it once :| )
    //> load some sdl cruft (cruft!)
    malloc_t sym_malloc = dlsym( libC, "malloc" );
    sym_rand   = dlsym( libC, "rand" );
    sym_srand  = dlsym( libC, "srand" );
    sym_SDL_Flip          = dlsym(libSDL, "SDL_Flip");
    sym_SDL_LockSurface   = dlsym(libSDL, "SDL_LockSurface");
    sym_SDL_UnlockSurface = dlsym(libSDL, "SDL_UnlockSurface");
    sym_SDL_MapRGB        = dlsym(libSDL, "SDL_MapRGB");
    

    And even though no assembler has to be harmed, your code might yield UB.


    edit:

    Oops, I lied about assembly.

    void _start() {
        ...
        asm( "int $0x80" :: "a"(1), "b"(42) );
    }
    

    this will make your program return 42.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned about list comprehension, which is a great fast way to get
I just learned about the AJAX Push Engine but it runs on Linux/Apache which
I just learned about how to include FxCop on a build. But it's slow
I have just learned about recursion in Python and have completed assignments, one of
So, I just learned about these recently and am about to put them into
I've just recently learned about friend class concept in C++ (I've googled around for
I just posted this question and learned about <see cref=> , however when i
I just learned about the XmlSerializer class in .Net. Before I had always parsed
I just learned about jquery's .makeArray and I am trying to use JSON.stringify to
I just learned about XLST on stackoverflow today (I love how in computers you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.