getenv("cc") 返回 NULL ,为什么?

getenv("cc") is returning NULL , why?

我正在学习 C 并在 bash shell 中遇到 cc 即每当我 make 我的源文件时我都会看到这个命令。

于是开始理解,从gcc --help开始,但在帮助中找不到cc选项。于是开始上网,偶然发现了这个post。在第二个答案中,它说 cc 是一个环境变量,通常指向 Linux 系统上的 /usr/bin/gcc

我正在使用 Linux 发行版,当我从 getenv("cc") 打印 return 值时,我得到 NULL。为什么会这样?

引用 :

cc is a common name for a C compiler executable or driver. It is the default for the $(CC) make variable.


因为,正如 ref 所述:

If the specified name cannot be found in the environment of the calling process, a null pointer shall be returned.


此外,如果您在终端上 运行 env,您会看到 cc 吗?并不真地!这就是为什么你得到一个 NULL 指针。因为它似乎不是环境变量..


我相信你提到的 cc 在你的 makefile 中是这样的:

CC=gcc

某事usual in Makefiles

cc 通常不是环境变量。通常有一个符号 link /usr/bin/cc 指向 gcc.

"cc" 是一个 c 编译器(可执行),它没有作为环境变量公开...getenv 将打印 env variables