Programming Languages Supported by GCC(GCC 支持的编程语言)

GCC(GNU Compiler Collection)是一个集成的编程语言编译工具集,支持包括C、C++、Objective-C等在内的多种语言。本文详细介绍了GCC支持的语言、其结构组成及如何使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Using the GNU Compiler Collection

1. Programming Languages Supported by GCC

GCC 支持的编程语言

GCC stands for “GNU Compiler Collection”.

GCC 全称 “GNU Compiler Collection ( GNU 编译器工具集 )”

GCC is an integrated distribution of compilers for several major programming languages.

GCC 是数个主流编程语言的集成编译工具集

These languages currently include C, C++, Objective-C, Objective-C++, Fortran, Ada, Go, and BRIG (HSAIL).

当前支持的语言包括 C, C++, Objective-C, Objective-C++, Fortran, Ada, Go 和 BRIG (HSAIL).

The abbreviation GCC has multiple meanings in common use.

GCC 这个缩写通常有多种含义

The current ofcial meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools.

现在的官方含义是”GNU 编译器工具集”, 一般指完整的工具套件

The name historically stood for “GNU C Compiler”, and this usage is still common when
the emphasis is on compiling C programs.

历史上也称 GCC 为 “GNU C 编译器”, 当强调编译 C 程序时这种用法仍然是常见的

Finally, the name is also used when speaking
of the language-independent component of GCC: code shared among the compilers for all
supported languages.

最后一种含义,GCC 也被用于指代它的独立于语言的组件: 编译器所支持全部语言的共有代码部分

The language-independent component of GCC includes the majority of the optimizers,
as well as the “back ends” that generate machine code for various processors.

GCC 独立于语言的组件包括大多数的优化器以及为不同处理器生成机器码的”后端

The part of a compiler that is specifc to a particular language is called the “front end”.

编译器具体到一个特定的语言部分被称为 前端

In addition to the front ends that are integrated components of GCC, there are several
other front ends that are maintained separately.

此外, 前端 是 GCC 的集成组件,但是也有几个分别维护的 前端

These support languages such as Pascal, Mercury, and COBOL.

比如 Pascal, Mercury 和 COBOL 的前端

To use these, they must be built together with GCC proper.

使用的前提是它们必须与 GCC 一起构建

Most of the compilers for languages other than C have their own names.

除 C 语言以外的大部分其他语言编译器都有自己的名字

The C++ compiler is G++, the Ada compiler is GNAT, and so on.

比如 C++ 的编译器是 G++, Ada 的编译器是 GNAT 等等

When we talk about compiling one of those languages, we might refer to that compiler by its own name, or as GCC. Either is correct.

当我们谈论这些语言中的某一种语言编译的时候,我们可以提及该语言编译器自己的名字或者说 GCC 也是正确的

Historically, compilers for many languages, including C++ and Fortran, have been implemented as “preprocessors” which emit another high level language such as C.

历史上,有些编译器为许多语言,包括 C++ 和 Fortran, 实现了像 C 这种高级语言的预处理器

None of the compilers included in GCC are implemented this way;

但GCC中包含的编译器都没有这样实现;

they all generate machine code directly.

他们都是直接生成机器码

This sort of preprocessor should not be confused with the C preprocessor, which is an integral feature of the C, C++, Objective-C and Objective-C++ languages.

这种预处理器不应该与C的预处理器混淆,C的预处理器是C,C++,Objective-C 和 Objective-C++ 语言的一个完整特性。


关于对GCC的前端和后端的理解可查看上一篇文章中的 GCC Front End 部分
http://blog.youkuaiyun.com/letshi/article/details/70332292

<think>我们面对的问题是:在Ubuntu系统中安装或使用CUDA时,遇到编译错误“#error -- unsupported GNU version! gcc versions later than 8 are not supported!”(或类似,可能是其他版本号,如12)。这是因为CUDA对GCC版本有特定要求,过高版本的GCC可能导致不兼容。 根据引用内容,解决思路主要有两种: 1. 降低GCC版本(安装指定版本的GCC并切换) 2. 在编译时指定使用兼容的GCC版本(如果项目构建允许) 下面将详细说明解决方案: --- ### **问题原因** CUDA编译器(`nvcc`)对宿主机的GCC版本有严格限制。不同CUDA版本支持的最高GCC版本不同: - CUDA 10.x 及更早:最高支持GCC 7 - CUDA 11.0-11.4:最高支持GCC 9 - CUDA 11.5以上:支持GCC 10或更高(但具体需查看官方文档) - 注意:错误信息中的版本号(如8或12)取决于您安装的CUDA版本要求。 --- ### **解决方案** #### **方法1:安装并切换至兼容的GCC版本(推荐)** 此方法通过安装所需GCC版本,并使用`update-alternatives`管理多版本切换。 **步骤:** 1. **安装所需GCC版本**(以GCC 8为例): ```bash sudo apt install gcc-8 g++-8 ``` 2. **配置多版本GCC优先级**: ```bash sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 20 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 20 ``` > 数字`20`表示优先级(数字越大优先级越高) 3. **切换GCC版本**: ```bash sudo update-alternatives --config gcc sudo update-alternatives --config g++ ``` 在交互界面中选择对应数字(如`/usr/bin/gcc-8`) 4. **验证版本**: ```bash gcc --version # 应显示gcc-8 g++ --version # 应显示g++-8 ``` 5. **重新编译CUDA项目** 切换后重新运行编译命令,错误应消失。 #### **方法2:临时指定编译器(适用于CMake项目)** 在编译命令中直接指定兼容的GCC路径(无需全局切换)。 **步骤:** 1. 查找已安装的兼容GCC路径: ```bash which gcc-8 # 例如输出/usr/bin/gcc-8 ``` 2. 在CMake命令中指定编译器: ```bash cmake .. -DCMAKE_C_COMPILER=/usr/bin/gcc-8 -DCMAKE_CXX_COMPILER=/usr/bin/g++-8 ``` 或修改项目的`CMakeLists.txt`,添加: ```cmake set(CMAKE_C_COMPILER "/usr/bin/gcc-8") set(CMAKE_CXX_COMPILER "/usr/bin/g++-8") ``` 3. **重新编译项目** #### **方法3:源码安装GCC(当系统仓库无所需版本时)** > 适用于Ubuntu仓库未提供所需GCC版本的情况(如需要GCC 7) 1. **下载GCC源码**(以GCC 7.5.0为例): ```bash wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-7.5.0/gcc-7.5.0.tar.gz tar -xvf gcc-7.5.0.tar.gz cd gcc-7.5.0 ``` 2. **下载依赖项**: ```bash ./contrib/download_prerequisites ``` 3. **编译安装**: ```bash mkdir build && cd build ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib make -j$(nproc) # 并行编译加速 sudo make install ``` 4. **后续步骤同方法1**,使用`update-alternatives`管理版本。 --- ### **恢复原GCC版本** 完成CUDA相关编译后,可切换回原GCC版本: ```bash sudo update-alternatives --config gcc # 选择原版本(如gcc-11) sudo update-alternatives --config g++ # 选择原版本(如g++-11) ``` --- ### **注意事项** 1. **CUDA版本与GCC的兼容性** 务必查阅[CUDA官方文档](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html)确认您的CUDA版本支持GCC最高版本。 2. **长期方案** 若需长期使用特定GCC版本,建议在`~/.bashrc`中设置`CC`和`CXX`环境变量: ```bash export CC=/usr/bin/gcc-8 export CXX=/usr/bin/g++-8 ``` 3. **内核模块编译** 若涉及内核模块(如NVIDIA驱动),需确保内核头文件与当前内核版本匹配: ```bash sudo apt install linux-headers-$(uname -r) ``` --- ### **错误示例排查** 若执行`nvidia-smi`正常但编译仍报错,请检查: ```bash nvcc --version # 确认CUDA版本 gcc --version # 确认当前GCC版本 ``` 两者版本需满足兼容性矩阵[^2]。 §§相关问题§§ 1. 如何查看CUDA版本支持GCC最高版本? 2. 使用`update-alternatives`切换GCC后,系统更新会覆盖配置吗? 3. 如何永久锁定GCC版本防止被更新?</think>### 解决CUDA编译错误:`#error -- unsupported GNU version! gcc versions later than X are not supported!` 此错误表明您的CUDA版本与当前GCC编译器版本不兼容。以下是完整解决方案: --- ### **问题原因** 不同CUDA版本对GCC编译器有严格限制: | CUDA版本 | 支持的最高GCC版本 | |----------|------------------| | CUDA 11.x | GCC 9 | | CUDA 12.x | GCC 12 | | CUDA 10.x | GCC 8 | | CUDA 9.x | GCC 7 | 当系统GCC版本超过CUDA支持范围时,触发此错误[^2][^4]。 --- ### **解决方案** #### **方法1:安装兼容的GCC版本(推荐)** 1. **安装所需GCC版本**(以GCC 8为例): ```bash sudo apt install gcc-8 g++-8 ``` 2. **配置默认编译器**: ```bash sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 20 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 20 ``` 3. **切换编译器版本**: ```bash sudo update-alternatives --config gcc # 选择gcc-8的编号 sudo update-alternatives --config g++ # 选择g++-8的编号 ``` 4. **验证版本**: ```bash gcc --version # 应显示gcc-8.x ``` #### **方法2:编译时指定编译器(临时方案)** 在CMake命令中强制指定兼容版本: ```bash cmake .. \ -DCMAKE_C_COMPILER=$(which gcc-8) \ -DCMAKE_CXX_COMPILER=$(which g++-8) ``` 适用于临时编译场景[^5]。 #### **方法3:源码安装GCC(特殊需求)** 当系统仓库无所需版本时: ```bash wget https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-8.5.0/gcc-8.5.0.tar.gz tar -xvf gcc-8.5.0.tar.gz cd gcc-8.5.0 ./contrib/download_prerequisites mkdir build && cd build ../configure --enable-languages=c,c++ --disable-multilib make -j$(nproc) # 约30-60分钟 sudo make install ``` 安装后通过`update-alternatives`配置[^2]。 --- ### **版本管理技巧** 1. **查看已安装编译器**: ```bash update-alternatives --list gcc ``` 2. **多版本共存配置**: ```bash sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 70 ``` 数字越大优先级越高(70 > 50)[^3]。 3. **恢复默认高版本**: ```bash sudo update-alternatives --config gcc # 选择原高版本编号 ``` --- ### **验证解决** 重新编译CUDA项目,错误应消失。可通过NVCC检查: ```bash nvcc --version ``` 若仍报错,检查CUDA与GCC兼容性矩阵: ```bash /usr/local/cuda/include/crt/host_config.h ``` 查找类似内容: ```c #if __GNUC__ > 8 #error -- unsupported GNU version! gcc versions later than 8 are not supported! #endif ``` --- ### **预防措施** 1. **安装前检查兼容性**: ```bash gcc --version nvcc --version 2>/dev/null | grep release ``` 2. **使用容器化方案**: ```bash docker run --gpus all -it nvidia/cuda:11.8.0-base ``` 避免主机环境污染[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值