首页 > 其他分享 >在 macOS 中用 gcc 替换 clang 作为 VSCode 编译器

在 macOS 中用 gcc 替换 clang 作为 VSCode 编译器

时间:2022-10-11 12:56:29浏览次数:73  
标签:opt macOS 12 VSCode gcc clang -- 编译器 homebrew

众所周知,macOS 下的默认 C/C++ 编译器是 clang/clang++ 而非 gcc/g++,尽管在大部分情况下或许难以察觉其中的区别,但偶尔我们会需要用到 gcc 中的一些函数等等,因此改用 VSCode & gcc/g++ 写代码也是值得考虑的。

值得注意的是,尽管我们可以用gcc -v查看到 gcc 的信息,但是你会看到显示的信息其实是 Apple clang version 14.0.0 (clang-1400.0.29.102) ,用 whereis gcc 查询到的信息是 usr/bin/gcc ,这是因为从某种意义上讲, macOS 中默认的 gcc 只是 clang 的一个别名(顺便一提,clang 的位置是 usr/bin/clang)。

因此,首先用 Homebrew 下载一个真正的 gcc 编译器(暂且认为你已经有了 Homebrew):

brew install gcc

然后我们就可以查看真正的 gcc 信息了,我写这篇博客时 gcc 的版本是 12.2.0,用 gcc-12 -v 查询到的信息是:

Using built-in specs.
COLLECT_GCC=gcc-12
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.2.0/bin/../libexec/gcc/aarch64-apple-darwin21/12/lto-wrapper
Target: aarch64-apple-darwin21
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin21 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Homebrew GCC 12.2.0)

表明 gcc-12 已经安装好了,我们可以用 whereis gcc-12 查询 gcc-12 的位置,默认路径应该是/opt/homebrew/bin/gcc-12,同理 g++-12 的位置是 /opt/homebrew/bin/g++-12

假设你在此前已经跟着 VSCode 官网的教程配置好了 tasks.jsonlaunch.json ,我们用这个路径替换 VSCode 中 tasks.jsoncommand 命令,

比如我想要用 g++ 编译 C++ 代码,那么原先的 "command": "/usr/bin/clang++", 被替换为 "command": "/opt/homebrew/bin/g++-12",

按照我浅薄的理解,至此,你就已经可以愉快的用 g++ 编译 VSCode 中的 C++ 代码了。

除此以外,如果我们还想让系统中的 gcc 这个词回归他的本意,而不是作为 clang 的别名,那么可以借助 alias 命令。

我使用的是 zsh,因此我在 .zshrc 中添加命令即可:

sudo vim ~/.zshrc

打开后,添加

alias gcc='gcc-12'
alias cc='gcc-12'
alias g++='g++-12'
alias c++='c++-12'

再保存退出即可,重启终端后就会生效。

现在我们再查看 gcc -v,就会显示与上文查询 gcc-12 一样的信息了,编译时也可以直接使用 gcc 了,例如:

g++ -o helloworld helloworld.cpp

就可以编译一些含有 gcc 独有函数的代码了(比如代码中调用了 __gcd())。

标签:opt,macOS,12,VSCode,gcc,clang,--,编译器,homebrew
From: https://www.cnblogs.com/emptysetvvvv/p/16778840.html

相关文章

  • VSCode 插件 vsix格式文件 离线安装
    场景 有些时候内网不能上网,则需要从共享目录直接安装下载好的vsix格式文件一、假设已经有了vsix离线文件(下载vsix暂不了解,后抽空补)二、文件放在vscode的安装目录......
  • vscode如何链接git远程仓库gitee或github
    vscode如何链接git远程仓库gitee或githubhttps://blog.csdn.net/G_C_H/article/details/1206732271.在GitHub上创建新的仓库2.生成SSH密钥开启GitBash命令行中输......
  • ESP32开发环境搭建 IDF3.3.5+VScode
    1、 软件准备:①ESP-IDF:包含ESP32API和用于操作工具链的脚本。②工具链msys32:用于编译ESP32应用程序。③编辑工具VisualStudioCode  注意:工具链和ESP-IDF需......
  • Win10 环境下 vscode 没法在终端使用 conda activate 命令来更换 Python 环境的解决方
    在vscode上激活conda镜像如报下面错误:CommandNotFoundError:Yourshellhasnotbeenproperlyconfiguredtouse'condaactivate'.Ifusing'condaactivate'fr......
  • VSCode安装教程详细简单版
    https://blog.csdn.net/weixin_43928112/article/details/125813377VSCode插件总结:(rust语言)1.rust-analyzer:语言服务器、自动补全、语法高亮等;2.CodeLLDB:debug功能;3.Ev......
  • vscode的pasteImage的配置
    "pasteImage.path":"${currentFileDir}/imgs/${currentFileName}","pasteImage.basePath":"${currentFileDir}/imgs","pasteImage.forceUnixStyleSeparator"......
  • vscode settings.json配置项
    安装通用插件1、Prettier-Codeformatter2、Vue3Snippets3、VueLanguageFeatures(Volar)安装项目依赖npminstalleslint-plugin-vue@typescrip......
  • vscode——如何在vscode中运行C/C++
    前言mingw-w64:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/内容安装mingw-w64下载地址x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z:x86_64-8.1......
  • vscode markdown WYSIWYG 所见即所得编辑和预览
    一直使用Typora编写markdown,随着vscode在工作中使用的越来越多,产生了一个想法:能不能在vscode中写markdown,减少软件的成本?可是vscode官方自带的的markdown体验却一般般,那......
  • vscode include<stdio.h>头文件红色的解决办法
    原因是标准库没识别到。前提需要安装c/c++插件第一步:点击下面win按钮,选择配置JSON第二步:打开c_cpp_properties.json,添加include路径,放在最上面......