8

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kan Li
  • 8,557
  • 8
  • 53
  • 93

2 Answers2

12

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

Baruch
  • 20,590
  • 28
  • 126
  • 201
  • 5
    You need to add 2>&1 just before the '|', since strace outputs to stderr. I tested the idea on my VM by adding a modified specs file at the location suggested from the strace output, and then running g++ -v on a dummy cpp, observing the expected options. – Joshua Chia Nov 14 '12 at 14:38
  • Can you show us an example? Eg. how to add a default linker option, that's there all the time? Or a C++ compiler option, or a C compiler option? – Calmarius Jan 08 '16 at 13:46
  • This spec file is quite complex on first look. With lots of % marks and {}s can you show us an example how to add a default options to linker, C compiler or C++ compiler? – Calmarius Jan 08 '16 at 13:48
  • @Gravis GCC reads the spec file (if it exists) and uses the values in it to override the default (built in) spec file. By dumping the whole default spec file and editing it, you effectively override the whole built-in spec file. – Baruch Jan 11 '17 at 22:17
  • @baruch You should put that in your answer and update the link to be non-version specific. – Gravis Jan 12 '17 at 02:42
2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    There are legit use cases for this beyond calling it from the command line. You might want to teach the GCC driver where to find an alternative libc, for example ... or alternative headers. Or add "default" headers. – 0xC0000022L Sep 21 '15 at 17:27