Thread: How do you exclude libc using gcc?

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    How do you exclude libc using gcc?

    Hello. I searched the FAQ and the forums for "libc" and did not come across anything relevant.

    Perhaps someone knows of a flag to pass to gcc so that libc is not included with your code?

    I am aware that libc is the standard C library which most people use, but I am not using it in some of my programs, so, no need to include it in the executable if it isn't in use (just bloats it out).

    There must be some way to exclude libc from getting compiled in with your code.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Just man gcc
    Code:
           -nostartfiles
               Do not use the standard system startup files when linking.  The
               standard system libraries are used normally, unless -nostdlib or
               -nodefaultlibs is used.
    
           -nodefaultlibs
               Do not use the standard system libraries when linking.  Only the
               libraries you specify will be passed to the linker.  The standard
               startup files are used normally, unless -nostartfiles is used.  The
               compiler may generate calls to memcmp, memset, and memcpy for Sys-
               tem V (and ISO C) environments or to bcopy and bzero for BSD envi-
               ronments.  These entries are usually resolved by entries in libc.
               These entry points should be supplied through some other mechanism
               when this option is specified.
    
           -nostdlib
               Do not use the standard system startup files or libraries when
               linking.  No startup files and only the libraries you specify will
               be passed to the linker.  The compiler may generate calls to mem-
               cmp, memset, and memcpy for System V (and ISO C) environments or to
               bcopy and bzero for BSD environments.  These entries are usually
               resolved by entries in libc.  These entry points should be supplied
    through some other mechanism when this option is specified.
    
               One of the standard libraries bypassed by -nostdlib and -nodefault-
               libs is libgcc.a, a library of internal subroutines that GCC uses
               to overcome shortcomings of particular machines, or special needs
               for some languages.
               In most cases, you need libgcc.a even when you want to avoid other
               standard libraries.  In other words, when you specify -nostdlib or
               -nodefaultlibs you should usually specify -lgcc as well.  This
               ensures that you have no unresolved references to internal GCC
               library subroutines.  (For example, __main, used to ensure C++ con-
               structors will be called.)

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    23
    Thanks man!

    now I've got some another problem (the linker giving errors of undefined _mainCRTStartup) and I've tried both -D_mainCRTStartup=WinMain and -DmainCRTStartup=WinMain to no avail

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    WinMain? Why are you trying to compile a Windows application with GCC?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    23
    Quote Originally Posted by quzah
    WinMain? Why are you trying to compile a Windows application with GCC?

    Quzah.
    I am using MSYS and MingW on Windows to compile Windows applications using gcc. Reason?--because I want to be able to actually sell programs I make (legally) and because I cannot afford the high prices of, for example, Microsoft's Visual C++ Professional edition (required if you want to legally sell code you compile with it).

    I can use gcc to make commercial applications if I want to, plus expenses to me are $0

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    23
    Okay, I've gotten some success with the following.
    crtmaintest.c
    Code:
    #include <windows.h>
    int mainCRTStartup(int argc, char* argv[]){
        MessageBox(NULL,"Hello","hello",MB_OK);
        return 0;
    }
    then compile with

    gcc -nostdlib -mwindows -o crtmaintest.exe crtmaintest.c -luser32

    and then

    strip -s crtmaintest.exe

    This produces a smallest executable with only my own code in it and none of the libc and it links correctly and the resulting executable runs correctly. My only concern is maybe if I've gotten the signature of _mainCRTStartup incorrect. It won't catch as a compiletime error as it was a linker error anyhow and it won't matter in this simple program since I never use any of the arguments. I mean I know it will return an int either way (if it has a main-like signature or a WinMain-like signature) or even if it's something slightly different from either, chances are great..plus my example works correctly so obviously the zero is returned. I've searched for _mainCRTStartup on google, trying to see if I can find a signature for it (which parameters/variables it has passed in to it and return value) but I can't. I'm pretty sure the return value will be an int regardless but the input parameters are troubling me.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void __cdecl mainCRTStartup( void )
    {
        int mainret, argc;
    
        argc = _ConvertCommandLineToArgcArgv( );
    
        mainret = main( argc, _ppszArgv, 0 );
    
        ExitProcess(mainret);
    }
    Source: http://www.microsoft.com/msj/archive/S569.aspx

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    23
    ^^--Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Compilation Question
    By chacham15 in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 08:15 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  4. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 03:46 PM