Kan Li
Kan Li

Reputation: 8797

Add some flags by default to gcc (preferably using specs file)

I want to let my gcc always add some flags by default, is there a clean way to do this?

Basically I have some flags I pass every time I invoke gcc, for example (but not limited to) -g (so that it has debug information).

There are several workarounds but they are ugly:

  1. alias g++=..., but I don't like this approach;
  2. Write a script that wraps around the g++, similar to 1;
  3. ...

I would prefer just modifying the specs file so that everything is seamless.

Upvotes: 8

Views: 7064

Answers (2)

Baruch
Baruch

Reputation: 21508

Run strace gcc | grep specs to see where it is searching for the specs file, then go there and run gcc -dumpspecs > specs. You now have a specs file ready for editing. Use this reference: https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html

Upvotes: 15

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

The trouble with modifying the specs file is that you ever change your mind about some option, you can't cancel it from the command line; you have to go modify the specs file. You also have to remember to remodify the new specs file when you upgrade the compiler.

Also, how often do you run the compiler from the command line? I know that just about the only time I do that directly is when I need to hack a command line temporarily and I can't be bothered to work out how to fix the compiler options in the makefile, so I copy the compilation line from the output of make and then run the compiler. One trouble with alias is that it won't work inside a makefile. So, for me, the problem would reduce to ensuring that the compiler options are used in every makefile.

Upvotes: 1

Related Questions