user7047949
user7047949

Reputation:

Compilation error option -std=c99 or -std=gnu99

I start in language C and I try to compile my program but I have this error.

error: ‘for’ loop initial declarations are only allowed in C99 mode

I would like to include the option -std=c99 in my Makefile but I do not know where to put it.

# Compilatore
CC=gcc
CFLAGS=-W -g -Wall $(OPTLEVEL)
BINFOLDER=./bin
SRCFOLDER=./src
OBJFOLDER=./obj

all: test

test: $(OBJFOLDER)/tas-data.o $(OBJFOLDER)/tas-fct.o $(OBJFOLDER)/tas-io.o $(OBJFOLDER)/tas-main.o
    $(CC) -o $(BINFOLDER)/tas-main  $(OBJFOLDER)/tas-data.o $(OBJFOLDER)/tas-fct.o $(OBJFOLDER)/tas-io.o $(OBJFOLDER)/tas-main.o

$(OBJFOLDER)/tas-main.o: $(SRCFOLDER)/tas-main.c $(SRCFOLDER)/tas-io.h  $(SRCFOLDER)/tas-fct.h $(SRCFOLDER)/tas-data.h
    $(CC) -o $(OBJFOLDER)/tas-main.o  -c $(SRCFOLDER)/tas-main.c

$(OBJFOLDER)/tas-data.o: $(SRCFOLDER)/tas-data.c $(SRCFOLDER)/tas-data.h
    $(CC) -o $(OBJFOLDER)/tas-data.o  -c $(SRCFOLDER)/tas-data.c 

$(OBJFOLDER)/tas-fct.o: $(SRCFOLDER)/tas-fct.c $(SRCFOLDER)/tas-fct.h $(SRCFOLDER)/tas-data.h
    $(CC) -o $(OBJFOLDER)/tas-fct.o -c $(SRCFOLDER)/tas-fct.c

$(OBJFOLDER)/tas-io.o: $(SRCFOLDER)/tas-io.c $(SRCFOLDER)/tas-io.h $(SRCFOLDER)/tas-data.h
    $(CC) -o $(OBJFOLDER)/tas-io.o  -c $(SRCFOLDER)/tas-io.c

clean:
    rm -f $(BINFOLDER)/tas-main $(OBJFOLDER)/*.o

cleanall: clean
    rm -rf $(BINFOLDER)/*

Upvotes: 2

Views: 4981

Answers (1)

Rishi
Rishi

Reputation: 1395

You should put it in the flags/options which you provide for compilation. As there are other options inserted in CFLAGS, you can put this option in CFLAGS.

CFLAGS=-W -g -std=c99 -Wall $(OPTLEVEL)

Use CFLAGS in your compilation commands in the Makefile like this:

$(CC) $(CFLAGS)-o $(OBJFOLDER)/tas-main.o  -c $(SRCFOLDER)/tas-main.c

EDIT

You Makefile will have to be modified this way:

# Compilatore
CC=gcc
CFLAGS=-Wextra -g -std=c99 -Wall $(OPTLEVEL)
BINFOLDER=./bin
SRCFOLDER=./src
OBJFOLDER=./obj

all: test

test: $(OBJFOLDER)/tas-data.o $(OBJFOLDER)/tas-fct.o $(OBJFOLDER)/tas-io.o $(OBJFOLDER)/tas-main.o
    $(CC) $(CFLAGS) -o $(BINFOLDER)/tas-main  $(OBJFOLDER)/tas-data.o $(OBJFOLDER)/tas-fct.o $(OBJFOLDER)/tas-io.o $(OBJFOLDER)/tas-main.o

$(OBJFOLDER)/tas-main.o: $(SRCFOLDER)/tas-main.c $(SRCFOLDER)/tas-io.h  $(SRCFOLDER)/tas-fct.h $(SRCFOLDER)/tas-data.h
    $(CC) $(CFLAGS) -o $(OBJFOLDER)/tas-main.o  -c $(SRCFOLDER)/tas-main.c

$(OBJFOLDER)/tas-data.o: $(SRCFOLDER)/tas-data.c $(SRCFOLDER)/tas-data.h
    $(CC) $(CFLAGS) -o $(OBJFOLDER)/tas-data.o  -c $(SRCFOLDER)/tas-data.c 

$(OBJFOLDER)/tas-fct.o: $(SRCFOLDER)/tas-fct.c $(SRCFOLDER)/tas-fct.h $(SRCFOLDER)/tas-data.h
    $(CC) $(CFLAGS) -o $(OBJFOLDER)/tas-fct.o -c $(SRCFOLDER)/tas-fct.c

$(OBJFOLDER)/tas-io.o: $(SRCFOLDER)/tas-io.c $(SRCFOLDER)/tas-io.h $(SRCFOLDER)/tas-data.h
    $(CC) $(CFLAGS) -o $(OBJFOLDER)/tas-io.o  -c $(SRCFOLDER)/tas-io.c

clean:
    rm -f $(BINFOLDER)/tas-main $(OBJFOLDER)/*.o

cleanall: clean
    rm -rf $(BINFOLDER)/*

Upvotes: 2

Related Questions