How to Use Make and Compile as C99

How to compile a C project in C99 mode?

You have done this:

for (int i=0;i<10;i++) {

And you need to change it to this:

int i;
for (i=0;i<10;i++) {

Or, as the error says,

use option -std=c99 or -std=gnu99 to compile your code.

Update copied from Ryan Fox's answer:

gcc -std=c99 foo.c -o foo

Or, if you're using a standard makefile, add it to the CFLAGS variable.

Setting std=c99 flag in GCC

Instead of calling /usr/bin/gcc, use /usr/bin/c99. This is the Single-Unix-approved way of invoking a C99 compiler. On an Ubuntu system, this points to a script which invokes gcc after having added the -std=c99 flag, which is precisely what you want.

How enable c99 mode in gcc with terminal

Compile using:

gcc -std=c99 -o outputfile sourcefile.c

gcc --help lists some options, for a full list of options refer to the manuals. The different options for C dialect can be found the section "Options Controlling C Dialect" in any gcc version's manual (e.g., here).

As you are using make you can set the command line options for gcc using CFLAGS:

# sample makefile
CC = gcc
CFLAGS = -Wall -std=c99
OUTFILE = outputfile
OBJS = source.o
SRCS = source.c

$(OUTFILE): $(OBJS)
$(CC) $(CFLAGS) -o $(OUTFILE) $(OBJS)
$(OBJS): $(SRCS)
$(CC) $(CFLAGS) -c $(SRCS)

Addendum (added late 2016): C99 is getting kind of old by now, people looking at this answer might want to explore C11 instead.

Make tells to use std c99 even if I use it

Aha. I think I know what your problem is.

The commands in a Makefile must be indented with tab characters, not spaces. You may have to adjust your text editor's settings to make it use "real" tabs here.

(Using spaces will result in the results you've described, where the rules are read but the commands are ignored.)

use option -std=c99 or -std=gnu99 to compile your code

You have a line:

CFLAGS += -DBOARD_$(shell echo $(BOARD) | tr a-z A-Z)

You can add it to the end of this line so it reads:

CFLAGS += -DBOARD_$(shell echo $(BOARD) | tr a-z A-Z) -std=c99

Makefile compile error not applying -std=c99

-I -std=c99 says compiler to add directory named -std=c99 to include search paths. This is not what you wanted. Judging by presented fragment, $(BLDR_FREEGLUT_INCLUDE_PATH) is empty - while it shouldn't be. Fix that variable's value and things will go well.

Can one set std=c99 in GCC as a default?

I do not know the answer to this, but you should just use a makefile.

Very simple makefile:

CC = gcc
CFLAGS = -g -Wall -std=c99
OBJECTS = *.c #Source files
NAME = FILENAME #Desired filename
TODELETE = $(NAME) *.o # the *.o should be the same as the objects
LIBS = NECESSARY_LIBS #remove line if no external libraries needed

mt-collatz : $(OBJECTS)
<INSERT TAB>$(CC) $(CFLAGS) $(OBJECTS) -o $(NAME) $(LIBS)

.PHONY: clean
clean:
<INSERT TAB>rm -f $(TODELETE)

Type make to compile, make clean to remove compiled objects. Remove comments with # sign, and add a tab where it says INSERT TAB since I don't know how to do it on SO.

You don't actually have to remove the *.c and *.o in OBJECTS and TODELETE, but it will complain if you ever end up with more than one file with a main function.



Related Topics



Leave a reply



Submit