Compile with -ansi -pedantic -Wall automatically with gcc

We must compile the C source code using gcc as follows:

gcc -ansi -pedantic -Wall program.c

I am wondering how I can “automate” this when I enter:

gcc program.c

It will automatically compile with three switches. Is it possible?

+3
source share
5 answers

You can also use implicit make rules, so you do not need to write a make file for each program. Make will automatically call the compiler if you say make fooa file exists in the current directory foo.c. To add flags for this, define a variable CFLAGSin your environment, for example. in bash add export CFLAGS="-Wall -pedantic -ansi"to .bashrc.

, make , C , , .

, make :

# Makefile
foo:foo.o bar.o

running make

gcc $CFLAGS -c -o foo.o foo.c
gcc $CFLAGS -c -o bar.o bar.c
gcc -o foo foo.o bar.o

.

+9

/ , makefile.

make , : make

+8
alias gcc="gcc -ansi -pedantic -Wall"

, @Brian, make , , , ​​ CMake SCons.

+3

makefile , .

make , .bashrc : alias gcc=gcc -ansi -pedantic -Wall.

+2

script, , make CFLAGS .

, /usr/bin/compile, script, $0, , . pedantic, fullwarn ..

script, - :

OLDCFLAGS=$CFLAGS
WHATAMI=$(basename $0)

case "$WHATAMI" in
    pedantic)
        export CFLAGS="-Wall -pedantic -ansi"
        make $@
        exit $?
    ;;
    c99)
        export CFLAGS="-std=c99 ... ... ..."
        ....

Then, to compile foo.c with additional naggy flags:

pedantic foo

This is convenient, as I said for one-time builds, for example, trying to compile code that someone posted in a question, or developing how to use the new library, etc.

For anything else, just use the makefile as others have said.

+2
source

Source: https://habr.com/ru/post/1746651/


All Articles