Thread: The best gcc options? Just everyday use.

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    The best gcc options? Just everyday use.

    So one day I was brainstorming what to research next, and I wanted to know about gcc options to help me with my code for everyday (everycompile) use. So I was reading this thread, and I came across one of Salem's posts:
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2  hello.c
    I thought it was pretty cool, mainly because I didn't know what it was saying. So I decided to:
    Code:
    man gcc
    (this is a way in Linux to call for help files for a specific program)
    I found out that
    -W is for enabling warnings
    -Wall for all warnings
    -ansi for strict C semantics
    -pedantic required by -ansi
    -O2 for pretty good optimizations

    So my main question is, is this the preferred way to compile C code for everyday use?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well -W -Wall is redundant since -Wall covers all warnings.

    -ansi turns off certain GCC extentions that aren't ansi. It doesn't turn off certain features and if you use those features the program will still compile ok.

    -pedantic will warn / error on the features not turn off by -ansi

    as for -O2: I never run the optimization switch until I am done debugging my program. Since the structure of the program can be changed it can make it difficult to debug using a debugger such as gdb.

    I use the following for every day programming: -Wall -Werror -ansi -pedantic -g
    I use makefiles to control my projects, so I'll sometimes drop the -g and -ansi and -pedantic and put in optimization or some other switches.

    -g btw causes the debugging symbols to be included in the executable

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Thantos
    I use the following for every day programming: -Wall -Werror -ansi -pedantic -g
    Is -Werror redundent since -Wall covers all warnings?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nope, Werror doesn't turn on any extra warnings. It changes how they are viewed. Normally when you have a warning its just that a warning that you can ignore. Werror tells the compiler to treat warnings as errors. When you compile and recieve an error the compiler will not produce an executable.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I like these options! Except for one thing:
    I can't use C++ comments :(
    Should I get used to C style comments? I'll try my best :(

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To use the // comments you have to:
    A) Be programming in C++ (and then use g++)
    B) Be programming in C99 in which case you need to add -std=c99

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    -ansi and -pedantic prevent me from using C++ comments in C code. I guess that will actually do me better, down with C++ comments! I'm using gcc, so am I actually compiling C99?
    Last edited by Kleid-0; 12-27-2004 at 12:51 AM.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not unless you specify the -std=c99. Though I haven't tested it with -ansi and -pedantic.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The -O switch has the side-effect of making the "use before initialisation" warnings apparent.
    Code:
    int a;
    if ( a == 0 )  // oops, a not initialised
    Read the -Wuninitialized disclaimer in the manual.

    > I can't use C++ comments
    Well at a push, you could specify
    -Wno-comment
    which along with all your other -W options turn on all the warnings EXCEPT for that one.

    Another alternative is to specify
    -std=c99
    since C99 allows for // comments
    But I'd be a bit wary of using too many new C99 features since there aren't too many compilers which fully support C99.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    With -ansi and -std=c99, it fails. If you drop out the -ansi, it works fine.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The -O switch
    Is the -O the same as -O0 when there have been no other -O levels defined? If so then I'm adding -O

    Edit: Never mind, it appears its the same as -O1

    Hmmm I think I'll have to play with my Makefile to see if I can get it so I can compile with -O1 and without since I really don't use gdb that often
    Last edited by Thantos; 12-27-2004 at 01:09 AM.

  12. #12
    Quote Originally Posted by Kleid-0
    So my main question is, is this the preferred way to compile C code for everyday use?
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2  hello.c
    Sounds good to me, at last for standard C.

    Of course, if you use extensions, you must remove '-pedantic' and '-ansi'.
    Emmanuel Delahaye

    "C is a sharp tool"

  13. #13
    Quote Originally Posted by Kleid-0
    I like these options! Except for one thing:
    I can't use C++ comments
    Should I get used to C style comments? I'll try my best
    if you have gcc 3.x, replace -ansi by -std=c99
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 04:06 PM
  4. gcc configuration under linux advice needed
    By vart in forum Tech Board
    Replies: 9
    Last Post: 01-10-2007, 02:46 PM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 03:46 PM