C++ makefile 中 CXX 的值从何而来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11394659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:06:35  来源:igfitidea点击:

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

c++cmakefile

提问by Ananke Leda

Code Snippet:

代码片段:

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

I know that CXXis 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 CXXcomes from?

我知道这CXX是一个变量(包含要调用的编译器命令),但我想知道这个变量来自哪里。该变量未在 makefile 中定义,也不是环境变量。谁能解释一下价值CXX从何而来?

采纳答案by Mihai Maruseac

Make has several predefined variablesamong which is CC. Initially, it is set at ccwhich is a symlink to the installed C compiler:

Make 有几个预定义的变量,其中包括CC. 最初,它被设置为cc已安装的 C 编译器的符号链接:

$ 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/nullto 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.

您可以使用make -p -f /dev/null获取所有隐式规则和变量的列表。我现在无法显示输出,因为我有一个非标准安装并且输出不是英文的。

回答by ArjunShankar

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

CXX 是 GNU make 中的隐式变量。还有其他人

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:

以下是有关隐式规则如何使用 CXX 的摘录:

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

编译的C ++程序
n.o从自动地进行n.ccn.cpp或者n.C与以下形式的配方
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c
我们鼓励您对 C++ 源文件使用后缀“.cc”而不是“.C”。

回答by Konrad Rudolph

Can anyone explain where the value of CXXcomes from?

谁能解释一下价值CXX从何而来?

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

像其他“魔法”变量 ( LD, RM, MAKE) 一样,它是由 make 在内部预定义的。

回答by Norswap

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).

这是一个用户可以覆盖的变量,它具有默认值g++(至少在 GNU Make 版本中)。仅此而已(它没有在某些文件或类似的东西中定义)。

Source: The GNU Make Manual

来源:GNU Make 手册