c++ - 在 shell 中为 make 设置 GCC 版本

标签 c++ gcc makefile g++

我安装了两个 gcc(同样适用于 g++)版本。较新的是默认的:

/usr/bin/gcc      # 4.9.2
/usr/bin/gcc-4.4  # 4.4.7

对于我的 make 命令,我想使用 gcc-4.4/g++-4.4。

我已经尝试了这三种变体,但似乎都不起作用:

export CC="gcc-4.4"
export CPP="g++-4.4"

export CC=/usr/bin/gcc-4.4
export CPP=/usr/bin/g++-4.4

export gcc=/usr/bin/gcc-4.4
export g++=/usr/bin/g++-4.4

Makefile 定义:

# Compiler Options
CC       = gcc
CPP      = g++
LD       = g++

Makefile使用的编译器还是4.9.2。如何使用 4.4.7?

最佳答案

GNU Make manual, 6.10 Variables from the Environment :

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the -e flag is specified, then values from the environment override assignments in the makefile. But this is not recommended practice.)

推荐的做法是在 make 命令行上传递这些变量:

$ make CC=gcc-4.4 CPP=g++-4.4 CXX=g++-4.4 LD=g++-4.4

旁注是 CXX 用于编译 C++ 代码,而 CPP 用于预处理。 makefile 的作者将 CPPCXX 混淆了,或者 makefile 确实使用 CPP 来生成依赖项,这在过去十年中一直是不必要的或者。参见 this了解更多详情。

关于c++ - 在 shell 中为 make 设置 GCC 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854114/

相关文章:

c++ - 为什么 getline() 函数在这里不读取任何内容?

c++ - cin.get() 获取太多

c - 使用 gcc 时 undefined reference

java - Java 使用哪个版本的 Make?

c++ - 两种情况下wchar_t字符整数值的区别

c++ - 我们是否仍然需要单独定义静态成员,即使它们是在类定义中初始化的?

c - gcc mingw 在与程序集结合时给出垃圾输出

linux - 我可以在Linux中编辑GCC的打印输出吗?

makefile - GNU Make for 带有两个变量的循环

linux - Hyperledger Fabric SDK Go 安装错误。这里出了什么问题以及如何解决?