Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the gcc version in a Makefile?

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 ?

like image 784
Gene Vincent Avatar asked Mar 04 '11 00:03

Gene Vincent


People also ask

How do I find my gcc version in Makefile?

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.

How do you check gcc is there or not?

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.

Which gcc does make use?

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).


1 Answers

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 
like image 67
srgerg Avatar answered Oct 30 '22 19:10

srgerg