Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc permanently change c standard

Tags:

c

gcc

standards

c99

Is there a way of telling gcc to use the c99 standard when compiling c files as a default? I want to avoid giving it the -std=c99 parameter all the time. I assume I can do this by creating an alias in the .bashrc file, but seems to be rather inelegant.

like image 793
octavian Avatar asked Jan 11 '23 10:01

octavian


2 Answers

You may call c99 instead of gcc (wrapper for gcc, if it's available on your system) or try to modify your gcc spec file. More information here: http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Spec-Files.html

like image 178
jbgs Avatar answered Jan 17 '23 17:01

jbgs


Here's an unexpected answer. Use a Makefile!

Pros:

  • Simply type make to build
  • All options are automatically handled in the build process.
    While you're at it, go ahead and enable all warnings, as is good to do. See this.
  • Easy to scale up to multiple source files
  • Can handle multi-step builds involving different tools

Cons:

  • Another tool to learn, another thing to get wrong.

Consider this source:

#include <stdio.h>

int main() {
    printf("Hello!\n");
    int x = 4;
    printf("%d\n", x);
    return 0;
}

You could make a Makefile like this:
(Disclaimer, I don't actually know how to write them)

CC=gcc
CFLAGS=-Wall -pedantic -std=c99
LDFLAGS=
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

.PHONY: clean

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean:
    rm -f *.o $(EXECUTABLE)

And it builds for me.

Likewise, if you remove the -std=c99, it shouldn't be valid C89 code, and indeed, typing make brings up the build error.

like image 38
Prashant Kumar Avatar answered Jan 17 '23 18:01

Prashant Kumar