Make Homebrew libraries available to gcc

Hello everyone, I am trying to make the Homebrew-installed version of gcc to permanently search for headers into /usr/local/Cellar/<some_subfolder>.

Basically I would like it to always have the option -I/usr/local/Cellar/<some_subfolder> active, without retyping the full paths every single time. I have already tried to modify CPLUS_INCLUDE_PATH, C_INCLUDE_PATH and also LC_LIBRARY_PATH and LIBRARY_PATH but without solving.

I would like to try to reinstall gcc from homebrew with some configuration flags (the version I have is just the “brew install gcc” one). Do you know if that is something possible?

You could brew extract the gcc formula to your own tap, then modify it to your taste and build it from source. However, that’s serious overkill for what you’re trying to do:


export CPATH=/usr/local/Cellar/<some_subfolder>

From the gcc man page:

CPATH specifies a list of directories to be searched as if specified with -I , but after any paths given with -I options on the command line. This environment variable is used regardless of which language is being preprocessed.

Note that most formulae with include files will have their include files linked into /usr/local/include, and their libraries into /usr/local/lib, so:

export CPATH=/usr/local/include LIBRARY_PATH=/usr/local/lib

should cover most of the formulae you’ll ever use. The exceptions are clearly marked as “keg-only” in their caveats, with clear instruction on how to include/link them. For instance:

$ brew info sqlite
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> /Users/aho/.bash_profile

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"