Skip to content

[fixed] ‘error invoking gcc driver’ (brew, macOS)

  • by
an image showing an error in emacs

after updating homebrew Emacs

Just let me get to ‘happy hacking!’, I beg you

tl;dr, “I just want it fixed so I can get on with actual work”, “save me trawling, give me something I can copy and paste”:

Add locations of gcc / libgccjit to LIBRARY_PATH in early-init.el (usually in ~/.emacs.d/), eg:

(setenv "LIBRARY_PATH" "/usr/local/Cellar/gcc/15.2.0:\
/usr/local/Cellar/libgccjit/15.2.0/lib/gcc/current/:\
/usr/local/Cellar/gcc/15.2.0/lib/gcc/current/gcc/x86_64-apple-darwin24/15")

Adapted from this comment by archit-rastogi. Note this isn’t quite a “just copy and paste” as your gcc version probably differs.

But the proof of the pudding for me was in the eating:

There is another comment with an ostensibly generic version further down for finding the library paths, but I didn’t test that- this was the latest shark attack in a long line of yaks that had been shaved, so I was by that point in the “work, PLEASE” mindset.

jimmeh commented:

(defun homebrew-gcc-paths ()
  "Return GCC library paths from Homebrew installations.
Detects paths for gcc and libgccjit packages to be used in LIBRARY_PATH."
  (let* ((paths '())
         (brew-bin (or (executable-find "brew")
                       (let ((arm-path "/opt/homebrew/bin/brew")
                             (intel-path "/usr/local/bin/brew"))
                         (cond
                          ((file-exists-p arm-path) arm-path)
                          ((file-exists-p intel-path) intel-path))))))

    (when brew-bin
      ;; Get gcc paths.
      (let* ((gcc-prefix (string-trim
                          (shell-command-to-string
                           (concat brew-bin " --prefix gcc"))))
             (gcc-lib-current (expand-file-name "lib/gcc/current" gcc-prefix)))
        (push gcc-lib-current paths)

        ;; Find apple-darwin directory.
        (let* ((default-directory gcc-lib-current)
               (arch-dirs (file-expand-wildcards "gcc/*-apple-darwin*/*[0-9]")))
          (when arch-dirs
            (push (expand-file-name
                   (car (sort arch-dirs #'string>)))
                  paths))))

      ;; Get libgccjit paths
      (let* ((jit-prefix (string-trim
                          (shell-command-to-string
                           (concat brew-bin " --prefix libgccjit"))))
             (jit-lib-current (expand-file-name "lib/gcc/current" jit-prefix)))
        (push jit-lib-current paths)))

    (nreverse paths)))

(defun setup-macos-native-comp-library-paths ()
  "Set up LIBRARY_PATH for native compilation on macOS.
Includes Homebrew GCC paths and CommandLineTools SDK libraries."
  (let* ((existing-paths (split-string (or (getenv "LIBRARY_PATH") "") ":" t))
         (gcc-paths (homebrew-gcc-paths))
         (clt-paths '("/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"))
         (unique-paths (delete-dups
                        (append existing-paths gcc-paths clt-paths))))

    (setenv "LIBRARY_PATH" (mapconcat #'identity unique-paths ":"))))

;; Set up library paths for native compilation on macOS.
(when (eq system-type 'darwin)
  (setup-macos-native-comp-library-paths))
a screenshot of a terminal showing a long-term system load of over 200

As a post-script, the image show just how much compiling emacs thrashes the system- sustained load of 100-200 for 30-40 mins!

Leave a Reply

Your email address will not be published. Required fields are marked *