I would like to use some gcc warning switchs that aren't available in older gcc versions (eg. -Wtype-limits).
Is there an easy way to check the gcc version and only add those extra options if a recent gcc is used ?
I wouldn't say its easy, but you can use the shell function of GNU make to execute a shell command like gcc --version and then use the ifeq conditional expression to check the version number and set your CFLAGS variable appropriately. Show activity on this post. Show activity on this post.
In the Command Prompt window type “gcc” and hit enter. If the output says something like “gcc: fatal error: no input files”, that is good, and you pass the test.
make usually uses gcc to actually compile source files. As I said in a comment to the OP's question: javac is more than just a compiler and performs dependency checks based on built-in rules (which are simple due to Java's path/name vs. package/class correspondence).
I wouldn't say its easy, but you can use the shell
function of GNU make to execute a shell command like gcc --version
and then use the ifeq
conditional expression to check the version number and set your CFLAGS
variable appropriately.
Here's a quick example makefile:
CC = gcc GCCVERSION = $(shell gcc --version | grep ^gcc | sed 's/^.* //g') CFLAGS = -g ifeq "$(GCCVERSION)" "4.4.3" CFLAGS += -Wtype-limits endif all: $(CC) $(CFLAGS) prog.c -o prog
Edit: There is no ifgt
. However, you can use the shell expr
command to do a greater than comparison. Here's an example
CC = gcc GCCVERSIONGTEQ4 := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 4) CFLAGS = -g ifeq "$(GCCVERSIONGTEQ4)" "1" CFLAGS += -Wtype-limits endif all: $(CC) $(CFLAGS) prog.c -o prog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With