Skip to content
Advertisement

Any possibilities that makefile can use different compilers by flag and find variable gcc paths?

Are there any possibilities that makefile can use different compilers by flag and find variable gcc paths? System is Linux.

So the goal should be anything like to say in command line “make CC=…” and it should use one of two possible compilers and should find the path to the second one (first compiler is standard gcc) in a way kind of “automatically”. Is this possible?

For example:

First compiler:

CC := gcc

Second compiler first path:

CC := /path-to-compiler-1/gcc

Second compiler second path:

CC := /path-to-compiler-2/gcc

Unfortunately I have no clue how to realize that in an elegant way.

Edit: The background about the two different compilers is, that the first is the standard linux gcc and the second for mips optimized gcc. So I just want to compile in both ways, compiling for mips on embedded site and standard compilation on normal linux site if necessary. Or another possiblity would be to compile both executables at the same time. The mips compiler is listed under a path that begins in all cases with /home/user. Thereby the folder user is variable and maybe there is another superordinate folder. Concerning this the makefile should find the right path for to the mips gcc.

Advertisement

Answer

Do you mean this?

CC = $(if $(COMPILER),$(COMPILER),gcc)
CFLAGS = -std=c11 -pedantic -Wall

all:
    $(CC) $(CFLAGS) -o demo demo.c

clean:
    rm -f demo

You can launch your Makefile with:

make COMPILER=gcc

or with

make COMPILER=clang

and if you don’t provide any argument

make

the default is gcc

5 People found this is helpful
Advertisement