Where does the value of CXX in a makefile come from?

C++CMakefile

C++ Problem Overview


Code Snippet:

target_test : test.cc 
    $(CXX) $(CPPFLAGS) $(CFLAGS) test.cc

I know that CXX is a variable (containing the compiler command to call), but I was wondering where this variable comes from. The variable is not defined in the makefile and is not an environment variable. Can anyone explain where the value of CXX comes from?

C++ Solutions


Solution 1 - C++

Make has several [predefined variables][1] among which is CC. Initially, it is set at cc which is a symlink to the installed C compiler:

$ readlink -f `which cc`
/usr/bin/gcc-4.6

Also:

$ readlink -f `which c++`
/usr/bin/g++-4.6

You can change it if you want.

You can use make -p -f /dev/null to get a list of all implicit rules and variables. I cannot show the output right now because I have a non-standard install and the output is not in English.

[1]: http://www.gnu.org/software/make/manual/make.html#Implicit-Variables "GNU Make Manual"

Solution 2 - C++

CXX is an implicit variable in GNU make. There are others too.

Not only that, these implicit variables get used in implicit rules.

Here is an extract relating to how CXX is used by an implicit rule:

> Compiling C++ programs
> n.o is made automatically from n.cc, n.cpp, or n.C with a recipe > of the form
> $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c.
> We encourage you to use the suffix ‘.cc’ for C++ source files instead of ‘.C’.

Solution 3 - C++

> Can anyone explain where the value of CXX comes from?

Like other “magic” variabes (LD, RM, MAKE), it’s predefined internally by make.

Solution 4 - C++

This is a variable that an user can override and which has for default value g++ (in the GNU Make version, at least). There is nothing more to it (it isn't defined in some file or stuff like that).

Source: The GNU Make Manual

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnanke LedaView Question on Stackoverflow
Solution 1 - C++Mihai MaruseacView Answer on Stackoverflow
Solution 2 - C++ArjunShankarView Answer on Stackoverflow
Solution 3 - C++Konrad RudolphView Answer on Stackoverflow
Solution 4 - C++NorswapView Answer on Stackoverflow