Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set GCC version for make in shell

I have two gcc (same applies to g++) versions installed. The newer one is the default one:

/usr/bin/gcc      # 4.9.2
/usr/bin/gcc-4.4  # 4.4.7

For my make command I want to use gcc-4.4 / g++-4.4.

I've tried these three variantes but none seems to work:

export CC="gcc-4.4"
export CPP="g++-4.4"

export CC=/usr/bin/gcc-4.4
export CPP=/usr/bin/g++-4.4

export gcc=/usr/bin/gcc-4.4
export g++=/usr/bin/g++-4.4

The Makefile defines:

# Compiler Options
CC       = gcc
CPP      = g++
LD       = g++

The compiler used by the Makefile is still 4.9.2. How can I use 4.4.7?

like image 324
Markus L Avatar asked Oct 04 '16 13:10

Markus L


People also ask

Which GCC does make use?

You can use make to compile your C and C++ programs by calling gcc or g++ in your makefile to do all the compilation and linking steps, allowing you to do all these steps with one simple command.

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. Save this answer.

Does GCC 5.4 support C ++ 17?

Compiler supportGCC has had complete support for C++17 language features since version 8. Clang 5 and later supports all C++17 language features.


1 Answers

GNU Make manual, 6.10 Variables from the Environment:

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the -e flag is specified, then values from the environment override assignments in the makefile. But this is not recommended practice.)

The recommended practice is to pass these variables on make command line:

$ make CC=gcc-4.4 CPP=g++-4.4 CXX=g++-4.4 LD=g++-4.4

A side note is that CXX is used for compiling C++ code, whereas CPP is for preprocessing. Either the author of the makefile confused CPP with CXX, or the makefile indeed uses CPP for generating dependencies, which has been unnecessary for the last decade or so. See this for more details.

like image 56
Maxim Egorushkin Avatar answered Oct 12 '22 09:10

Maxim Egorushkin