make is not using -std=c++11 option for g++

10,869

Solution 1

There is no "global makefile" and there is no way to change the default flags for all invocations of make (unless you edit the source code to GNU make and compile it yourself, which is a bad idea in this situation).

In your makefile(s), add the line:

CXXFLAGS += -std=c++11

Assuming you're using the built-in rules for compiling things, or that you're using the standard variables with your own rules, that will do what you need.

If that doesn't work we'll need to see your makefile or at least the rules you use to build your C++ source files (things like the -d output aren't useful here--that would be interesting if files weren't being built, that you thought should be or similar).

Solution 2

Setting a system-wide language for all your C++ projects isn't necessarily a good idea. Instead, define a Makefile that specifies any compiler options you'd like:

CXXFLAGS := -std=c++11 $(CXXFLAGS)

The CXXFLAGS are then passed to your compiler when compiling a C++ program (assuming you're using the default GNU Make rules).

If the Makefile lives in your current working directory, you can now run make target in order to compile a target.cpp file into a target executable. If the Makefile is in another directory, you must specify the path to it:

make -f path/to/your/Makefile target

If you want to add extra parameters just for one run, you can set an environment variable or a make variable on the command line:

# environment:
CXXFLAGS='-std=c++11' make target

# make variable:
make target CXXFLAGS='-std=c++11'

Any of these will cause the execution of g++ -std=c++11 target.cpp -o target or equivalent.

In theory you can edit your shell profile to export CXXFLAGS='-std=c++11' which will make that environment variable available to all programs you run. In practice, setting compiler options through environment variables tends to cause more problems than it solves.

Of all these solutions, just writing a normal Makefile is by far the easiest approach. That way, all of the build configuration is in one place and completely automated.

Share:
10,869
tonystark
Author by

tonystark

Updated on June 05, 2022

Comments

  • tonystark
    tonystark about 2 years

    I am trying to compile c++ files using make. But, it is not using -std=c++11 flag by default. Whenever I need to compile a program which uses c++11 specific features, I have to explicitly compile it using g++.

    So, I want to ask how can I have make automatically use the option -std=c++11 for all my c++ files on my system.

    If I need to change some global makefile for g++ , what is the location of the makefile on Linux Mint 18 and what needs to be changed or added?

    Or do I need to create a Makefile for myself?

    EDIT 1: I am invoking make like

    make myfile

    And there are only .cpp files and their binaries in the directory. I don't have any Makefile in the directory.

    EDIT 2: Here, myfile is the name of the c++ file which I want to compile.

    When I run make with the -d option, I get the following output (I can not paste all of the output as it is quite long and is exceeding the body size limit so, I am including the screenshots of the output). Image 1
    And this image(2) has some lines from the end.
    Image 2

    I intentionally made a change in the file "MagicalWord.cpp" so that make finds something to make!