Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gcc

g++

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.

like image 312
Kan Li Avatar asked Apr 18 '12 00:04

Kan Li


People also ask

What is gcc specs option?

What are GCC Specs? GCC specs are cars designed specifically for usage in GCC regions in order to adapt to the climate here. In practice, this means adding features like air filters, radiators and air conditioners.

What is a spec file?

Spec files are plain-text files that are used to construct spec strings. They consist of a sequence of directives separated by blank lines. The type of directive is determined by the first non-whitespace character on the line, which can be one of the following: % command.

What is flag in g ++ compiler?

(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would. This makes use of a debugger such as gdb much easier, since it will be able to refer to variable names that occur in the source code.


2 Answers

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

like image 118
Baruch Avatar answered Nov 07 '22 04:11

Baruch


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.

like image 27
Jonathan Leffler Avatar answered Nov 07 '22 04:11

Jonathan Leffler