Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 10 Useful GCC Flags That I Use Regularly
Mithil Poojary
Mithil Poojary

Posted on

     

10 Useful GCC Flags That I Use Regularly

C++ was my first programming language. I have been learning and using it for a little less than 2 years now. I have been of the idealogy (which is silly) that if something can be done in c++, do it in c++. Yes, I am slowly moving towards other feasible solutions for certain use cases.
Here are my frequently used gcc flags.

1. -o

gcc example.c-o outfile
Enter fullscreen modeExit fullscreen mode

The default executable name isa.exe ora.out or something similar depending on the host machine. You can easily set the executable name to, for example,outfile in this case. Other file names are also affected, like object files, assembler files.

2. -Wall, -Wextra

Wall and Wextra turn on certain warning flags, that are almost certainly errors in my experience.

3. -O2

gcc optimizes code for compilation speeds by default. You can enable this flag and optimize the code for runtime. The compiler optimization step is crazy and can optimize the code to extents one cannot imagine! I have read somewhere the compiler optimized a for loop which was adding some element each iteration to a single multiplication statement!

4. -g

Produces debugging information. Helpful when debugging withgdb as variable names, function names are listed which otherwise are not.

5. -S

Your C source code is compiled down to assembly first. The assembly output is stored in a file with the help of the-S flag.

6. -E

Do preprocessing only, and output the entire source code. You include a lot of headers inside your code, all that does is copying the content of the header into the source. This outputs the preprocessed source code.

7. -Wfloat-equal

Directly comparing floats is a bad idea. If you execute:

floata=0.2f,b=0.2f;if(a+b!=0.4)printf("Not equal");
Enter fullscreen modeExit fullscreen mode

You will get the outputNot equal. This is because of precision issues. This can be caught by the compiler by using-Wfloat-equal flag. I will soon write a post explaining this behavior, and how to tackle it. For now, just understand it is bad.

8. -std=XXX

Set a language standard. Very useful when you strictly want features from a particular standard. For example,

-std=c++14-std=c++17-std=c99
Enter fullscreen modeExit fullscreen mode

9. -I

gcc-Iexample_dir_name example.c
Enter fullscreen modeExit fullscreen mode

By default, gcc looks in the current working directory. You can also include multiple other directory locations with-I flag.

10. -D

gcc-Dmacro_name=definition
Enter fullscreen modeExit fullscreen mode

Define a macro with a value. This is helpful when you want to run the same code in multiple ways such that the code depends on that macro, or maybe for debugging.

For a (very long) list of other such flags, refer to the manpage of gcc (man gcc).
Mention your top gcc flags down in the comments section!

| Cover Pic by Taras Shypka on Unsplash

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Mainly working with Python. Feels amazing to be an Open-Sourcerer!
  • Location
    Mumbai, India
  • Education
    B.E. Information Technology
  • Work
    Student
  • Joined

More fromMithil Poojary

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp