daltonfury42
daltonfury42

Reputation: 3744

How to configure GCC to show all warnings by default?

I think it will be good and not much bad if -Wall flag is switched on by default. How do I configure GCC like this?

Is there any drawbacks to this other than the fact that a lot of warnings will flood your terminal when you are compiling some large program from source?

Upvotes: 2

Views: 2154

Answers (3)

Colin Pitrat
Colin Pitrat

Reputation: 2092

As discussed in the comments (although it's not a direct answer to your question), using a Makefile has many benefits. It provides a place where to put your build commands, that will alway stay up to date if you only build with make. It also ease running tests at each build.

Writing tests is a good habit, even when you're just working on a small and unsignificant piece of code for homeworks. It allows you to spot some dumb mistakes that you would otherwise miss, and to be sure you don't break your existing code by modifying it (especially the last minute modification).

An example of such a Makefile (here I have nothing to build apart from the test because it's a header only component):

all:
    g++ -O2 -Wall -Werror -std=c++11 test_polynomial.cc -o test_polynomial -lgmp
    g++ -O2 -Wall -Werror -std=c++11 test_g2polynomial.cc -o test_g2polynomial
    ./test_polynomial --log_level=test_suite
    ./test_g2polynomial --log_level=test_suite

clean:
    rm -f test_polynomial test_g2polynomial

Note: The example is not a very good one as I don't even factorize the build options in CFLAGS. If I want to add a flag, I have to add it in both commands !

Another benefit is that you always run make to build, whatever the language, the dependencies or even the build system (when working on a project using scons or another build system, I still write a Makefile doing all the commands I do when building and testing !).

This allows my personal addition on it (but here we're completely off-topic): I have a build script named autobuild looping on make each time I write a file in vim. I code in screen and run autobuild in a small window at the bottom of my screen. This way, each change is built and tested as soon as I write the file.

Upvotes: 0

gzh
gzh

Reputation: 3596

Add these lines to your ~/.bashrc if you use bash as your shell.

alias gcc='gcc -Wall'

Update:

you can refer to this question on https://superuser.com/questions/519692/alias-gcc-gcc-fpermissive-or-modifying-configure-script

If you use make, you need to overwrite make's variables CC and CXX from within the .bashrc: export CC="gcc -wall" export CXX="g++ -wall"

Upvotes: 2

daltonfury42
daltonfury42

Reputation: 3744

juzzlin suggested that a good method would be to write a wrapper for gcc. Marc Glisse also suggested that writing one is the best way to achieve what I want. So that's just what I did.

I made a bash script that calls gcc for me:

#!/bin/sh

echo -n "Compiling $1..."



gcc -Wall -Werror -o $(basename $1 .c).out $1

a=$?

if [[ "$a" -eq 1 ]]; then
    echo "Failed!"

else 
    echo "Done." 
    echo "Executing:"
    ./$(basename $1 .c).out
fi

Then I copied the script to /usr/bin and made it executable:

sudo cp car /usr/bin
chmod +x /usr/bin/car

(The name of the script is car which stands for "Compile And Run") So whenever I want to compile a source file and run it, I will type:

car mysourcefile.c

Upvotes: 0

Related Questions