Configure GCC to add compile flags globally - gcc

Can I configure GCC to add some file globally, for every project? I want to make it temporarily and only with flags like: -fdiagnostics-color.

I don't understand why do you need it but you can do a wrapper:
which gcc - will print a patch to GCC (copy it to clipboard)
mkdir somedir; cd somedir
create file with name gcc
and add into it: full path to gcc(from clipboard) -fdiagnostics-color somefile.c $# this command will add -fdiagnostics-color somefile.c before every line that came to gcc.
chmod +x gcc - set execution rights to gcc wrapper
And finally
add path to your wrapper. export PATH=somedir:$PATH

You might read about GCC spec files and alter the spec file used by your particular version of gcc. But this is generally frowned upon.
The usual practice would be to use GNU make and add a CFLAGS += -fdiagnostics-color to your Makefile. BTW with a recent enough GCC this (adding -fdiagnostics-color flag) is not even necessary since (at least by setting your GCC_COLORS environment variable) the default is -fdiagnostics-color=auto

Related

Change default ARM gcc option to thumb

I want to change the behavior of the ARM toolchain arm-linux-gnueabi-gcc in my Linux machine, that the compiled code will be in Thumb mode as default - same as passing the -mthumb flag.
I came across this document, which under the section of --with-mode describes exactly what I try to achieve. However, I couldn't understand from their explanation how can I actually set this option.
Can anyone clarify this for me, or suggest another way to achieve my goal?
You can "mask" the executable file /usr/bin/arm-linux-gnueabi-gcc with your own script that is named the same inside /usr/local/bin.
Create a file /usr/local/bin/arm-linux-gnueabi-gcc
With the content
#!/bin/sh
/usr/bin/arm-linux-gnueabi-gcc -mthumb "$#"
Add executable permissions to /usr/local/bin/arm-linux-gnueabi-gcc
Because PATH should list /usr/local/bin directory before /usr/bin, when you type arm-linux-gnueabi-gcc without the path in your console, your script will chosen first and will execute the real arm-linux-gnueabi-gcc executable with the additional option.

Can I change gcc default options without using command-line?

I am plagued by the Gentoo bug #580414.
In short, the default options mislead configure into not detecting standard include files because some headers contain this code:
#if defined _FORTIFY_SOURCE && _FORTIFY_SOURCE > 0
# if !defined __OPTIMIZE__ || __OPTIMIZE__ <= 0
# warning _FORTIFY_SOURCE requires compiling with optimization (-O)
, and __OPTIMIZE__ is off by default and _FORTIFY_SOURCE is on by default, and the generated warning is perceived as an error, indicating that "stdint.h", "stdlib.h" and many others are absent. Compilation eventually fails and I cannot install programs or even upgrade the gcc itself.
Can I simply put something in environment vars or in the /etc directory to turn on -O or turn off _FORTIFY_SOURCE for every invocation of gcc without editing gentoo build scripts?
Tried in /etc/portage/make.conf
EPATCH_USER_EXCLUDE='*10_all_default-fortify-source*'
CFLAGS="-O2 -O -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
CFLAGS_FOR_BUILD="-O2 -O -U_FORTIFY_SOURCE"
without any improvement.
There is no such environment variable. CFLAGS in make.conf won't work because the build systems usually do something like this:
$(CC) $(CFLAGS) $(MY_HARDCODED_CFLAGS)
thus overwriting your flags.
But to mangle any argument passed to gcc, you can use the following workaround.
create a directory, eg. under /usr/local/bin/
create a script which will mangle its arguments as you wish and then passes them to gcc or "/usr/bin/" + basename(argv[0]) (beware of infinite recursion)
make this script executable
create symlinks to the script in that directory with names like gcc, cc, x86_64-pc-linux-gnu-gcc
put a bunch of lines like this into /etc/portage/bashrc:
the_dir="/usr/local/bin/THE_DIR"
if [[ "${PATH}" != *"${the_dir}"* ]] ; then
export PATH="${the_dir}:${PATH}"
fi
Also to save yourself from possible problems in the future, do not forget to put a note about this change somewhere. (As should be done with any workaround anyway.)
Just documenting the commands I actually used to resolve the issue.
mv /usr/bin/i686-pc-linux-gnu-gcc /usr/bin/i686-pc-linux-gnu-gcc.OLD
cat >/usr/bin/i686-pc-linux-gnu-gcc
/usr/bin/i686-pc-linux-gnu-gcc.OLD -O "$#"
ctrl+D
chmod +x /usr/bin/i686-pc-linux-gnu-gcc
cp /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnu-gcc /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnuu-gcc
mv /usr/bin/i686-pc-linux-gnu-g++ /usr/bin/i686-pc-linux-gnuu-g++
cat >/usr/bin/i686-pc-linux-gnu-g++
/usr/bin/i686-pc-linux-gnuu-g++ -O "$#"
ctrl+D
chmod +x /usr/bin/i686-pc-linux-gnu-g++
cp /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnu-g++ /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnuu-g++
cp /etc/env.d/gcc/i686-pc-linux-gnu-4.6.3.O /etc/env.d/gcc/i686-pc-linux-gnuu-4.6.3
The credit all goes to rindeal. Recap:
identify the binaries that get invoked as compilers
rename them
in their place, create shell scripts that prepend "-O"
create a gcc profile to appease gcc-config
emerge all the unwieldy packages!

Make CMake use gccfilter

GCCFilter is a neat perl script that allows to color the output of GCC and thus makes debugging much more fun and, more important, faster.
You can use GCCFilter with a CMake generated Makefile by calling
gccfilter -a -c make
However, this approach has some drawbacks: Delayed output of CMake status infos, no color in the CMake commands are the most obvious.
The question: Is there a way to write some CMake module that searches for gccfilter if the compiler is gcc, checks if, say COLOR_CXX is set (rather easy up to here) and then tells CMake to replace all calls to gcc by gccfilter -a -c gcc.
CMake offers the variable CMAKE_CXX_COMPILER, but changing this one will disallow CMake to find correct include paths and the like. Is there some variable we may change after the project() command that is prefixed before each call to gcc?
You can make CMake use gccfilter by pointing the RULE_LAUNCH_COMPILE property to a wrapper script which invokes gccfilter with the desired options.
Create an executable shell script named gccfilter_wrap in the outermost CMake project directory with the following contents:
#!/bin/sh
exec gccfilter -a -c "$#"
Be sure to set the file's executable bit. Then in your CMakeLists.txt, set the RULE_LAUNCH_COMPILE directory property before adding targets:
project (HelloWorld)
set_directory_properties(PROPERTIES RULE_LAUNCH_COMPILE
"${PROJECT_SOURCE_DIR}/gccfilter_wrap")
add_executable(HelloWorld HelloWorld.cpp)
The generated makefile rules will then prefix each compiler invocation with the gccfilter_wrap script. Alternatively the RULE_LAUNCH_COMPILE property can also be set as a target property or as global property.
The RULE_LAUNCH_COMPILE property only works for Makefile-based CMake generators.
Edit by Thilo
This is how I finally solved the problem - basically a rephrased version of this solution:
# GCCFilter, if appliciable
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCPP)
option(COLOR_GCC "Use GCCFilter to color compiler output messages" ON)
set(COLOR_GCC_OPTIONS "-c -r -w" CACHE STRING "Arguments that are passed to gccfilter when output coloring is switchend on. Defaults to -c -r -w.")
if(COLOR_GCC)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROJECT_SOURCE_DIR}/cmake/gccfilter ${COLOR_GCC_OPTIONS}")
endif()
endif()

Set gcc and g++ optimization flags permanently

I'm running a x86 kernel on an x64 machine. I would like to compile libraries for a i586 processor. During compilation, some libraries use i686 optimization, so want to set -mtunes=i586, -march=i586 and -O3 flags for all of libraries even if they explicitly declare something else in their makefiles.
Somehow I want to set compiler flags permanently...
Regardless of whether you should do this, here's the easiest way to do it:
Create a new file with the following contents:
#!/bin/sh
exec /usr/bin/gcc "$#" -O3 -mtunes=i586 -march=i586
Change /usr/bin/gcc to your actual compiler if that's not right on your system.
Save it as ~/bin/gcc.
Make the new script executable:
chmod +x ~/bin/gcc
Repeat to create another file for g++.
Add ~/bin to the start of your path:
export PATH=~/bin:$PATH
Compile your project. Whenever your new scripts are on the path they will override whatever the makefile says.
Hope that helps.
P.S. The best way to do it (rather than the easiest) would probably be to mess with the compiler's "specs" file, but it's much harder to explain and do.

Is there a way to make a c++ compiler flag as default

Just like we specify input flags in the settings of the project in Xcode
Can I make few flags like -O3 or -fopenmp as default flags in command line when I use Terminal.
So that I dont have to type them everytime I compile some c++ fies. Is there a file in the installed gcc or C++ that I can edit to make them default.
Please let me know
thanks
For situations like this you'd probably use a makefile if it's project specific (or other similar automated build management like scons or cmake).
If you want it always on the terminal, you can alias your command to always specify those options, i.e.
alias g++='g++ -O3 -fopenmp'
Note that you said 'terminal' so I assume this is a type of *nix. If that is the case you can also set this into your terminal profile, like ~/.bashrc if you use bash, or ~/.zshrc if you use zsh, etc.

Resources