Last active
March 1, 2025 18:31
-
-
Save aachyee/4ad91190323de96091ff23359d172415 to your computer and use it in GitHub Desktop.
Make system gcc/g++ the default c/c++ compiler for brew.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S bash -euECo pipefail | |
# based on https://gist.github.com/alghanmi/8853646 | |
# | |
# Make system gcc tools the default compiler for brew. | |
# brew installs gcc-4.8, but does not create symlinks for it to be used | |
# without the version number. | |
# This script: | |
# 1. Generate symlinks for all gcc tools installed with system package manager. | |
# 2. | |
# | |
# How to apply. | |
# curl -LkfgSs -O https://gist.github.com/aachyee/4ad91190323de96091ff23359d172415/raw/add_sys-gcc-link_to_brew-bin.sh | |
# bash -n ./add_sys-gcc-link_to_brew-bin.sh # option '-nv' for more info. | |
# shellcheck | |
# bash ./add_sys-gcc-link_to_brew-bin.sh | |
#Make sure brew is installed | |
#type -P brew &>/dev/null || { echo "brew is not installed."; exit 1; } | |
echo -n 'installed brew version: ' | |
brew --version | |
echo -n 'installed gcc version: ' | |
gcc -v | grep version | |
BREW_BASE="$(brew --prefix)" | |
BREW_BIN="${BREW_BASE}/bin" | |
BREW_SBIN="${BREW_BASE}/sbin" | |
PATH_WO_BREW_BINS=$(sed -E "/${BREW_BIN}|${BREW_SBIN}//g;s/::+/:/g;s/^:|:$//g"<<<${PATH}) | |
# GCC_VER=$(readlink -f "$(find ${PATH_WO_BREW//:/\/ } -maxdepth 1 -regextype posix-extended -iregex '.*/gcc$' | sort | tail -n1)" | grep -oE '[0-9.]+$') | |
GCC_VER=$(readlink -f $(which gcc)| grep -oE '[0-9.]+$') | |
GCC_CMDS='cc gcc c++ cpp g++ gcc-ar gcc-nm gcc-ranlib gcov gcov-dump gcov-tool lto-dump' | |
# 'gcc-# c++-# cpp-# g++-# gcc-ar-# gcc-nm-# gcc-ranlib-# gcov-# gcov-dump-# gcov-tool-# lto-dump-#' | |
#Generate Symlinks | |
cd $BREW_BIN | |
for cmd in $GCC_CMDS | |
do | |
LN_NAME=$(echo $app | sed 's/\(.*\)-4.8/\1/') | |
ln -s $cmd $LN_NAME | |
done | |
#Prepend brew's path to $PATH | |
#echo '#Making brew apps the first in your path' >> ~/.bash_profile | |
#echo 'PATH=$(brew --prefix)/bin:$PATH' >> ~/.bash_profile | |
echo 'add "PATH=$(brew --prefix)/bin:$PATH" to "~/.bash_profile". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment