stefano1
stefano1

Reputation: 171

Automatically select a C++11-compatible g++ version in the Makefile

The problem:

2 version of g++ installed on a computer running Ubuntu 12.04. They are the versions 4.6 and 5.2.

I have to compile a C++11 program using a Makefile. If I use g++ as compiler it calls automatically the version 4.6, it does not support c++11 so the compilation fails. I've followed a tutorial online, so that now if I call g++ it calls automatically the version 5.2 and now it works.

I find this solution not so good, since it works only on my PC. Is there a way to recognize in the Makefile if the default g++ version support C++11 and, in case not, switch to a more recent version?

Thank you!

Upvotes: 5

Views: 1763

Answers (6)

dlmeetei
dlmeetei

Reputation: 10391

You can always select which gcc to use while invoking make

make CXX=/gcc/path/of/your/choice

otherwise you can detect gcc version using

ifdef CXX
     GCC_VERSION = $(shell $(CXX) -dumpversion)                                                                                               
else
     GCC_VERSION = $(shell g++ -dumpversion)
endif

in Makefile and while using, you can test if your gcc is >=4.6

ifeq ($(shell expr $(GCC_VERSION) '>=' 4.6), 1)

UPDATE: newer gcc needs -dumpfullversion together (icx is the CC from Intel OneAPI)

$ icx -dumpversion
  14.0.0
$ gcc -dumpversion
  9    
$ icx -dumpfullversion -dumpversion
  14.0.0
$ gcc -dumpfullversion -dumpversion
  9.3.1

Upvotes: 5

Vorac
Vorac

Reputation: 9114

You can check in your source code the gcc version and abort compilation if you don't like it. Here is how it works:

/* Test for GCC > 4.6 */
#if !(__GNUC__ > 3 && __GNUC_MINOR__ > 6)
#error gcc above version 4.6 required!
#endif

Upvotes: 0

cauchi
cauchi

Reputation: 1543

This works for me:

cmake_minimum_required(VERSION 2.8.11)
project(test)

if (${CMAKE_CXX_COMPILER_VERSION} LESS 5.0)
    message(FATAL_ERROR "You need a version of gcc > 5.0")
endif (${CMAKE_CXX_COMPILER_VERSION} LESS 5.0)

add_executable(test test.cpp)

Upvotes: 0

Yam Marcovic
Yam Marcovic

Reputation: 8141

One very simple way is to use conditional statements in your makefile, and go for versions which you know are compatible, and only use the default gcc as a fallback. Here's a basic example:

CXX=g++

ifeq (/usr/bin/g++-4.9,$(wildcard /usr/bin/g++-4.9*))
    CXX=g++-4.9
# else if... (a list of known-to-be-ok versions)
endif

The other, more robust method, is to generate your makefile using a script that checks for capabilities using test compilations, kind of like what ./configure usually does. I really don't mean to recommend autotools, though.

Upvotes: 2

Galik
Galik

Reputation: 48625

The thing to do is build your Makefile to use as many implicit rules as possible. By default compilation uses various environment variables.

The variable $(CXX) is the C++ compiler command and defaults to g++ on Linux systems. So clanging CXX to a different compiler executable will change the compiler for all implicit compile commands.

When you write explicit rules use the same variable that the implicit rules use. So instead of this:

program: program.cpp
    g++ -o program program.cpp

Do this:

program: program.cpp
    $(CXX) -o program program.cpp

Other variables you should use are:

CPPFLAGS = -Iinclude
CXXFLAGS = -std=c++14 -g3 -O0

Those are for pre-processing flags CPPFLAGS and compiler flags CXXFLAGS and library linking flags LDLIBS.

Using the default environment variables allows the person compiling the project the freedom to control the compilation for their desired environment.

See the GNU make manual

Upvotes: 1

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136306

Is there a way to recognize in the Makefile if the default g++ version support C++11 and, in case not, switch to a more recent version?

You can certainly detect the version of the default compiler available in PATH in your makefile. However, where do you search for another version?

The standard approach is to let the user specify the C compiler through CC and C++ compiler through CXX make variables, e.g.: make CC=/path/to/my/gcc CXX=/path/to/my/g++.

Upvotes: 6

Related Questions