Set gcc and g++ optimization flags permanently - gcc

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.

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.

Configure GCC to add compile flags globally

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

Conditional linking in Makefile

In my Makefile, I want to link to a library only if it is installed on the machine. So, for example if the library is hwloc, I want to do the following:
xfoo : foo.o
if (hwloc installed)
gcc foo.o -o $# -lhwloc
else
gcc foo.o -o $#
Is there anyway to do something like this? i.e. Is it possible to check if a specific library is installed and use that as a condition in a Makefile?
Here's the wrong answer:
xfoo : foo.o
if (hwloc installed); then gcc foo.o -o $# -lhwloc; else gcc foo.o -o $#; fi
Commands executed from a Makefile do not have to be just simple, single commands. Anything that a shell can execute, can be invoked from a Makefile. Including an entire script, sandwiched into one line.
Here's the right answer:
However, the above approach is the wrong one. You will find that many free software packages do this kind of thing all the time: conditionally link in a library, if it's available.
But the way that it's done is by running a separate configure script, before running make. Go grab the source tarball to a random free software package, of your choosing, and read the installation instructions. They will all tell you to run the configure script first, before running make.
A crushing majority of free software packages use the GNU toolchain to create their build system -- the configure script, and the Makefile. The GNU toolchain consists of autoconf and automake tools (also libtool in many cases). Go Google these, for more information.
There are also a few other, less popular toolchains, but the GNU toolchain is the most frequently one used, for this sort of a thing. So, to do something along the lines of what you're trying to do, the way this gets typically done is:
In the configure.ac file:
AC_CHECK_LIB(hwloc,some_function_in_the_hwloc_library,[LINK_HWLOC=-lhwloc])
AC_SUBST(LINK_HWLOC)
In the Makefile.am file:
hwloc_LDADD=#LINK_HWLOC#
That's it. That's the way this is done the countless number of times most free software packages need to do this exact same thing. autoconf and automake will take care of writing the shell script and the makefile, that implements this.
I don't have access to a Linux machine at the moment so pardon me my answer will be untested.
I will respectfully disagree with both of my predecessors.
First, using autotools to amend an existing Makefile is a bad idea. Autotools are made to avoid worrying about creating a good Makefile in a simple use case. It's as if OP asked "How to change + to - in my Python script" and the answer was "write a shell script to modify the script, save it in temporary file and execute the file"
Second answer, why do something manually when it can be painlessly done automatically?
So, IMHO the correct answer is, this is the exact use case for $(wildcard):
xfoo: foo.o $(wildcard libhwloc.a)
gcc $(patsubst lib%.a, -l%, $^) -o $#
Note: the library is installed or not ahead of time but not to be made during the build.
If you don't want to get involved with the autotools/etc. for this (which while a reasonable solution is also reasonable to want to avoid for something this simple) and you don't want to have to play guessing games about where people may or may not have this hwloc library installed then the best you can do is to let people turn the feature on manually.
Use three (or four) make variables. USE_HWLOC, HWLOC_LDLIBS, HWLOC_CFLAGS and possibly HWLOC_LDFLAGS.
Then when USE_HWLOC is defined you link against the library and use the other three variables in case they have also been set.
ifdef USE_HWLOC
HWLOC:=-lhwloc
else
HWLOC:=
HWLOC_LDLIBS:=
HWLOC_LDFLAGS:=
HWLOC_CFLAGS:=
endif
xfoo : foo.o
gcc foo.o -o $# $(HWLOC_LDLIBS) $(HWLOC)

How to build a project without make (bare shell script)?

I heard that back in the old days (or maybe not so old), before the make utility was included in Unix, people used to write shell script to "make" and "install" their software.
Consider a project with: 2 source files main.c and util.c, a header util.h that uses the OpenGL library and needs to run on Ubuntu.
(Ubuntu and OpenGL are used just for the sake of being specific)
What would such a script actually need to do? Where can I find an example?
It's hard to imagine why anyone would want to revisit "the bad old days" before make, but it's actually not too difficult for a simple project. So given your particular example, a shell script to compile might look like this:
gcc -Wall -c main.c -o main.o -lglut -lglm
gcc -Wall -c util.c -o util.o -lglut -lglm
gcc -Wall main.o util.o -o main -lglut -lglm
What it does is to simply run through the entire "recipe" to build the project every time. The advantage is that, if the source code is all correct, it should result in an executable. The considerable disadvantages are that
if any step fails, so will subsequent steps that rely on the failed step
everything is rebuilt every time, wasting a lot of time
compile and link flags are all embedded into the script and hard to change
this approach has little hope of cross-platform compatibility
One can tinker with the basic shell script to improve on various aspects of this. Then, when that proves inadequate, one could write a program to do these things better. At that point, in essence, you will have re-invented make.

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